|
1 | 1 | import { render } from "@testing-library/preact";
|
2 |
| -import { Duration } from "luxon"; |
| 2 | +import { DurationLike } from "luxon"; |
| 3 | +import { OmitByValue } from "utility-types"; |
3 | 4 | import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
4 | 5 |
|
5 | 6 | import { App, Component, MarkdownRenderer } from "@/lib/obsidian/types";
|
6 | 7 |
|
7 |
| -import { ObsidianMarkdown } from "../obsidian"; |
| 8 | +import { ObsidianMarkdown, ObsidianMarkdownProps } from "../obsidian"; |
8 | 9 |
|
9 | 10 | vi.mock("@/lib/obsidian/types");
|
10 | 11 | afterEach(() => vi.restoreAllMocks());
|
11 | 12 |
|
12 | 13 | describe(`${ObsidianMarkdown.name}`, () => {
|
| 14 | + const required: OmitByValue<ObsidianMarkdownProps, undefined> = { |
| 15 | + app: new App(), |
| 16 | + component: new Component(), |
| 17 | + sourcePath: "/vault/diary.md", |
| 18 | + markdown: "Foo", |
| 19 | + }; |
| 20 | + |
13 | 21 | beforeAll(() => vi.useFakeTimers());
|
14 | 22 | afterAll(() => vi.useRealTimers());
|
15 | 23 |
|
16 |
| - it("should rerender when markdown changes stay stable for a while", async () => { |
17 |
| - const props = { |
18 |
| - app: new App(), |
19 |
| - component: new Component(), |
20 |
| - sourcePath: "/vault/diary.md", |
21 |
| - delay: Duration.fromMillis(500), |
22 |
| - } as const; |
| 24 | + it("should render eventually", async () => { |
| 25 | + render(<ObsidianMarkdown {...required} />); |
| 26 | + expect(MarkdownRenderer.render).not.toHaveBeenCalled(); |
| 27 | + await vi.runAllTimersAsync(); |
| 28 | + expect(MarkdownRenderer.render).toHaveBeenCalledTimes(1); |
| 29 | + }); |
23 | 30 |
|
24 |
| - const { rerender } = render(<ObsidianMarkdown {...props} markdown="Apple" />); |
| 31 | + it("should throw an error when 'props.delay' is invalid", () => { |
| 32 | + const invalid = "one hour" as DurationLike; |
| 33 | + expect(() => render(<ObsidianMarkdown {...required} delay={invalid} />)).toThrowErrorMatchingSnapshot(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should rerender when 'props.markdown' stops changing for a while", async () => { |
| 37 | + const { rerender } = render(<ObsidianMarkdown {...required} delay={500} markdown="Apple" />); |
25 | 38 | await vi.runAllTimersAsync();
|
26 |
| - rerender(<ObsidianMarkdown {...props} markdown="Banana" />); |
| 39 | + rerender(<ObsidianMarkdown {...required} delay={500} markdown="Banana" />); |
27 | 40 | await vi.advanceTimersByTimeAsync(300);
|
28 |
| - rerender(<ObsidianMarkdown {...props} markdown="Banana" />); |
| 41 | + rerender(<ObsidianMarkdown {...required} delay={500} markdown="Banana" />); |
29 | 42 | await vi.advanceTimersByTimeAsync(300);
|
30 | 43 |
|
31 |
| - const anySpanElement = expect.any(HTMLSpanElement); |
32 | 44 | expect(MarkdownRenderer.render).toHaveBeenCalledTimes(2);
|
33 | 45 | expect(MarkdownRenderer.render).toHaveBeenCalledWith(
|
34 |
| - props.app, |
| 46 | + required.app, |
35 | 47 | "Apple",
|
36 |
| - anySpanElement, |
37 |
| - props.sourcePath, |
38 |
| - props.component, |
| 48 | + expect.any(HTMLSpanElement), |
| 49 | + required.sourcePath, |
| 50 | + required.component, |
39 | 51 | );
|
40 | 52 | expect(MarkdownRenderer.render).toHaveBeenCalledWith(
|
41 |
| - props.app, |
| 53 | + required.app, |
42 | 54 | "Banana",
|
43 |
| - anySpanElement, |
44 |
| - props.sourcePath, |
45 |
| - props.component, |
| 55 | + expect.any(HTMLSpanElement), |
| 56 | + required.sourcePath, |
| 57 | + required.component, |
46 | 58 | );
|
47 | 59 | });
|
48 | 60 |
|
49 |
| - it("should not rerender when markdown changes too frequently", async () => { |
50 |
| - const props = { |
51 |
| - app: new App(), |
52 |
| - component: new Component(), |
53 |
| - sourcePath: "/vault/diary.md", |
54 |
| - delay: Duration.fromMillis(500), |
55 |
| - } as const; |
56 |
| - |
57 |
| - const { rerender } = render(<ObsidianMarkdown {...props} markdown="Apple" />); |
| 61 | + it("should not rerender when 'props.markdown' keeps changing", async () => { |
| 62 | + const { rerender } = render(<ObsidianMarkdown {...required} delay={500} markdown="Apple" />); |
58 | 63 | await vi.runAllTimersAsync();
|
59 |
| - rerender(<ObsidianMarkdown {...props} markdown="Banana" />); |
| 64 | + rerender(<ObsidianMarkdown {...required} delay={500} markdown="Banana" />); |
60 | 65 | await vi.advanceTimersByTimeAsync(300);
|
61 |
| - rerender(<ObsidianMarkdown {...props} markdown="Cherry" />); |
| 66 | + rerender(<ObsidianMarkdown {...required} delay={500} markdown="Cherry" />); |
62 | 67 | await vi.advanceTimersByTimeAsync(300);
|
63 | 68 |
|
64 |
| - const anySpanElement = expect.any(HTMLSpanElement); |
65 | 69 | expect(MarkdownRenderer.render).toHaveBeenCalledTimes(1);
|
66 | 70 | expect(MarkdownRenderer.render).toHaveBeenCalledWith(
|
67 |
| - props.app, |
| 71 | + required.app, |
68 | 72 | "Apple",
|
69 |
| - anySpanElement, |
70 |
| - props.sourcePath, |
71 |
| - props.component, |
| 73 | + expect.any(HTMLSpanElement), |
| 74 | + required.sourcePath, |
| 75 | + required.component, |
72 | 76 | );
|
73 | 77 | expect(MarkdownRenderer.render).not.toHaveBeenCalledWith(
|
74 |
| - props.app, |
| 78 | + required.app, |
75 | 79 | "Banana",
|
76 |
| - anySpanElement, |
77 |
| - props.sourcePath, |
78 |
| - props.component, |
| 80 | + expect.any(HTMLSpanElement), |
| 81 | + required.sourcePath, |
| 82 | + required.component, |
79 | 83 | );
|
80 | 84 | expect(MarkdownRenderer.render).not.toHaveBeenCalledWith(
|
81 |
| - props.app, |
| 85 | + required.app, |
82 | 86 | "Cherry",
|
83 |
| - anySpanElement, |
84 |
| - props.sourcePath, |
85 |
| - props.component, |
| 87 | + expect.any(HTMLSpanElement), |
| 88 | + required.sourcePath, |
| 89 | + required.component, |
86 | 90 | );
|
87 | 91 | });
|
88 | 92 | });
|
0 commit comments