|
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 | 24 | 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; |
23 |
| - |
24 |
| - const { rerender } = render(<ObsidianMarkdown {...props} markdown="Apple" />); |
| 25 | + const { rerender } = render(<ObsidianMarkdown {...required} markdown="Apple" delay={{ milliseconds: 500 }} />); |
25 | 26 | await vi.runAllTimersAsync();
|
26 |
| - rerender(<ObsidianMarkdown {...props} markdown="Banana" />); |
| 27 | + rerender(<ObsidianMarkdown {...required} markdown="Banana" delay={{ milliseconds: 500 }} />); |
27 | 28 | await vi.advanceTimersByTimeAsync(300);
|
28 |
| - rerender(<ObsidianMarkdown {...props} markdown="Banana" />); |
| 29 | + rerender(<ObsidianMarkdown {...required} markdown="Banana" delay={{ milliseconds: 500 }} />); |
29 | 30 | await vi.advanceTimersByTimeAsync(300);
|
30 | 31 |
|
31 |
| - const anySpanElement = expect.any(HTMLSpanElement); |
32 | 32 | expect(MarkdownRenderer.render).toHaveBeenCalledTimes(2);
|
33 | 33 | expect(MarkdownRenderer.render).toHaveBeenCalledWith(
|
34 |
| - props.app, |
| 34 | + required.app, |
35 | 35 | "Apple",
|
36 |
| - anySpanElement, |
37 |
| - props.sourcePath, |
38 |
| - props.component, |
| 36 | + expect.any(HTMLSpanElement), |
| 37 | + required.sourcePath, |
| 38 | + required.component, |
39 | 39 | );
|
40 | 40 | expect(MarkdownRenderer.render).toHaveBeenCalledWith(
|
41 |
| - props.app, |
| 41 | + required.app, |
42 | 42 | "Banana",
|
43 |
| - anySpanElement, |
44 |
| - props.sourcePath, |
45 |
| - props.component, |
| 43 | + expect.any(HTMLSpanElement), |
| 44 | + required.sourcePath, |
| 45 | + required.component, |
46 | 46 | );
|
47 | 47 | });
|
48 | 48 |
|
49 | 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" />); |
| 50 | + const { rerender } = render(<ObsidianMarkdown {...required} markdown="Apple" delay={{ milliseconds: 500 }} />); |
58 | 51 | await vi.runAllTimersAsync();
|
59 |
| - rerender(<ObsidianMarkdown {...props} markdown="Banana" />); |
| 52 | + rerender(<ObsidianMarkdown {...required} markdown="Banana" delay={{ milliseconds: 500 }} />); |
60 | 53 | await vi.advanceTimersByTimeAsync(300);
|
61 |
| - rerender(<ObsidianMarkdown {...props} markdown="Cherry" />); |
| 54 | + rerender(<ObsidianMarkdown {...required} markdown="Cherry" delay={{ milliseconds: 500 }} />); |
62 | 55 | await vi.advanceTimersByTimeAsync(300);
|
63 | 56 |
|
64 |
| - const anySpanElement = expect.any(HTMLSpanElement); |
65 | 57 | expect(MarkdownRenderer.render).toHaveBeenCalledTimes(1);
|
66 | 58 | expect(MarkdownRenderer.render).toHaveBeenCalledWith(
|
67 |
| - props.app, |
| 59 | + required.app, |
68 | 60 | "Apple",
|
69 |
| - anySpanElement, |
70 |
| - props.sourcePath, |
71 |
| - props.component, |
| 61 | + expect.any(HTMLSpanElement), |
| 62 | + required.sourcePath, |
| 63 | + required.component, |
72 | 64 | );
|
73 | 65 | expect(MarkdownRenderer.render).not.toHaveBeenCalledWith(
|
74 |
| - props.app, |
| 66 | + required.app, |
75 | 67 | "Banana",
|
76 |
| - anySpanElement, |
77 |
| - props.sourcePath, |
78 |
| - props.component, |
| 68 | + expect.any(HTMLSpanElement), |
| 69 | + required.sourcePath, |
| 70 | + required.component, |
79 | 71 | );
|
80 | 72 | expect(MarkdownRenderer.render).not.toHaveBeenCalledWith(
|
81 |
| - props.app, |
| 73 | + required.app, |
82 | 74 | "Cherry",
|
83 |
| - anySpanElement, |
84 |
| - props.sourcePath, |
85 |
| - props.component, |
| 75 | + expect.any(HTMLSpanElement), |
| 76 | + required.sourcePath, |
| 77 | + required.component, |
86 | 78 | );
|
87 | 79 | });
|
| 80 | + |
| 81 | + it("should not throw when default props get used", () => { |
| 82 | + expect(() => render(<ObsidianMarkdown {...required} />)).not.toThrow(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should throw when 'prop.delay' is invalid", () => { |
| 86 | + const invalid = "0" as DurationLike; |
| 87 | + expect(() => render(<ObsidianMarkdown {...required} delay={invalid} />)).toThrowErrorMatchingSnapshot(); |
| 88 | + }); |
88 | 89 | });
|
0 commit comments