-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmarkdown.test.tsx
More file actions
110 lines (95 loc) · 3.44 KB
/
markdown.test.tsx
File metadata and controls
110 lines (95 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React from 'react';
import { act } from 'react-dom/test-utils';
import { cleanup, render } from '@testing-library/react';
import Markdown from '../markdown';
jest.mock('remark-gfm', () => () => {});
jest.mock('rehype-raw', () => () => {});
const markdown = `
# title
---
`;
describe('Test Chat Markdown', () => {
beforeEach(cleanup);
it('Match Snapshots', () => {
expect(render(<Markdown>{markdown}</Markdown>).asFragment()).toMatchSnapshot('default');
expect(
render(
<Markdown typing className="test">
{`\`inline code test \`
\`\`\`sql
SELECT * FROM table;
\`\`\`
`}
</Markdown>
).asFragment()
).toMatchSnapshot('typing');
});
it('Should call onMount', () => {
const fakeOnMount = jest.fn();
render(
<Markdown typing={false} onMount={fakeOnMount}>
# title
</Markdown>
);
expect(fakeOnMount).toBeCalled();
});
it('Should be a memoized component', () => {
function getContainer(container: HTMLElement) {
return container.querySelector('.dtc__aigc__markdown');
}
function isBlink(container: HTMLElement) {
return !!getContainer(container)?.classList.contains('dtc__aigc__markdown--blink');
}
const { container, rerender } = render(<Markdown typing={false}># title</Markdown>);
// Typing changed caused re-render
expect(isBlink(container)).toBeFalsy();
act(() => {
rerender(<Markdown typing># title</Markdown>);
});
expect(isBlink(container)).toBeTruthy();
// className changed caused re-render
expect(getContainer(container)?.classList.contains('jest')).toBeFalsy();
act(() => {
rerender(
<Markdown typing className="jest">
# title
</Markdown>
);
});
expect(getContainer(container)?.classList.contains('jest')).toBeTruthy();
// children changed caused re-render
expect(getContainer(container)?.firstChild?.textContent).toBe('title');
act(() => {
rerender(
<Markdown typing className="jest">
{'```sql\nSELECT * FROM table;\n```'}
</Markdown>
);
});
expect(getContainer(container)).toMatchSnapshot('code block');
const codeBlock = { convert: true };
// codeBlock properties changed caused re-render
act(() => {
rerender(
<Markdown typing className="jest" codeBlock={codeBlock}>
{'```sql\nSELECT * FROM table;\n```'}
</Markdown>
);
});
expect(getContainer(container)).toMatchSnapshot('code block with convert');
// others changed not caused re-render
act(() => [
rerender(
<Markdown
typing
className="jest"
codeBlock={codeBlock}
components={{ pre: ({ children }) => <div data-testid="pre">{children}</div> }}
>
{'```sql\nSELECT * FROM table;\n```'}
</Markdown>
),
]);
expect(getContainer(container)?.querySelector('[data-testid="pre"]')).toBeFalsy();
});
});