Skip to content

Commit afcea40

Browse files
committed
test: markdown renderer
1 parent 0361b92 commit afcea40

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/lib/obsidian/__mocks__/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { vi } from "vitest";
2+
3+
export const App = vi.fn();
4+
export const Component = vi.fn();
5+
export const MarkdownRenderer = { render: vi.fn() };
6+
export const Plugin = vi.fn();

src/lib/obsidian/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { MarkdownRenderer, Plugin } from "obsidian";
2-
export type { App, Component, EventRef, MetadataCache, TAbstractFile, TFile } from "obsidian";
1+
export { App, Component, MarkdownRenderer, Plugin } from "obsidian";
2+
export type { EventRef, MetadataCache, TAbstractFile, TFile } from "obsidian";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { render, waitFor } from "@testing-library/preact";
2+
import { Duration } from "luxon";
3+
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
4+
5+
import { App, Component, MarkdownRenderer } from "@/lib/obsidian/types";
6+
7+
import { ObsidianMarkdown, ObsidianMarkdownProps } from "../obsidian";
8+
9+
vi.mock("@/lib/obsidian/types");
10+
11+
describe(`${ObsidianMarkdown.name}`, () => {
12+
const app = new App();
13+
const component = new Component();
14+
const markdown = "Hello World";
15+
const sourcePath = "/vault/diary.md";
16+
const tagName = "span";
17+
const delay = Duration.fromMillis(500);
18+
19+
beforeAll(() => vi.useFakeTimers());
20+
afterEach(() => vi.resetAllMocks());
21+
afterAll(() => vi.useRealTimers());
22+
23+
it("should invoke render with the provided props", async () => {
24+
const props: ObsidianMarkdownProps = { app, component, markdown, sourcePath, tagName, delay };
25+
26+
render(<ObsidianMarkdown {...props} />);
27+
28+
await vi.runAllTimersAsync();
29+
30+
await waitFor(() =>
31+
expect(MarkdownRenderer.render).toHaveBeenCalledWith(
32+
app,
33+
markdown,
34+
expect.any(HTMLSpanElement),
35+
sourcePath,
36+
component,
37+
),
38+
);
39+
});
40+
41+
it("should rerender after a delay when the markdown value changes", async () => {
42+
const delay = Duration.fromMillis(300);
43+
const props: ObsidianMarkdownProps = { markdown: "Hello, World!", app, component, sourcePath, tagName, delay };
44+
45+
const { rerender } = render(<ObsidianMarkdown {...props} />);
46+
await vi.runAllTimersAsync();
47+
48+
expect(MarkdownRenderer.render).toHaveBeenCalledTimes(1);
49+
50+
rerender(<ObsidianMarkdown {...{ ...props, markdown: "Hello, Planet" }} />);
51+
await vi.advanceTimersByTimeAsync(299);
52+
53+
expect(MarkdownRenderer.render).toHaveBeenCalledTimes(1);
54+
55+
rerender(<ObsidianMarkdown {...{ ...props, markdown: "Hello, Planet" }} />);
56+
await vi.advanceTimersByTimeAsync(1);
57+
58+
expect(MarkdownRenderer.render).toHaveBeenCalledTimes(2);
59+
});
60+
});

0 commit comments

Comments
 (0)