-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathSafeTextMorph.test.tsx
More file actions
78 lines (64 loc) · 1.96 KB
/
SafeTextMorph.test.tsx
File metadata and controls
78 lines (64 loc) · 1.96 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
/**
* @jest-environment jsdom
*/
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import { SafeTextMorph } from './SafeTextMorph';
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
let shouldThrow = false;
jest.mock('torph/react', () => ({
TextMorph: ({ children, as: Tag = 'span', ...props }: any) => {
if (shouldThrow) {
throw new Error('TextMorph animation error');
}
// eslint-disable-next-line @typescript-eslint/no-require-imports
const ReactLib = require('react');
return ReactLib.createElement(Tag, props, children);
},
}));
let container: HTMLDivElement;
let root: ReturnType<typeof createRoot>;
beforeEach(() => {
shouldThrow = false;
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
act(() => {
root.unmount();
});
container.remove();
});
describe('SafeTextMorph', () => {
it('renders children string correctly', () => {
act(() => {
root.render(<SafeTextMorph>Hello World</SafeTextMorph>);
});
expect(container.textContent).toBe('Hello World');
});
it('renders fallback when TextMorph throws', () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
shouldThrow = true;
act(() => {
root.render(<SafeTextMorph>Fallback Text</SafeTextMorph>);
});
expect(container.textContent).toBe('Fallback Text');
const span = container.querySelector('span');
expect(span).not.toBeNull();
expect(consoleSpy).toHaveBeenCalledWith('TextMorph error:', expect.any(Error));
consoleSpy.mockRestore();
});
it('handles empty string', () => {
act(() => {
root.render(<SafeTextMorph>{''}</SafeTextMorph>);
});
expect(container.textContent).toBe('');
});
it('coerces number to string', () => {
act(() => {
root.render(<SafeTextMorph>{42}</SafeTextMorph>);
});
expect(container.textContent).toBe('42');
});
});