-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathhooks-17.test.tsx
More file actions
74 lines (60 loc) · 1.95 KB
/
hooks-17.test.tsx
File metadata and controls
74 lines (60 loc) · 1.95 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
import { render } from '@testing-library/react';
import * as React from 'react';
import { renderToString } from 'react-dom/server';
import useId, { resetUuid } from '../src/hooks/useId';
jest.mock('react', () => {
const react = jest.requireActual('react');
const clone = { ...react };
Object.defineProperty(clone, 'useId', {
get: () => null,
});
return clone;
});
describe('hooks-17', () => {
describe('useId', () => {
const Demo: React.FC<{ id?: string }> = ({ id }) => {
const mergedId = useId(id);
return <div id={mergedId} className="target" />;
};
function matchId(container: HTMLElement, id: string) {
const ele = container.querySelector('.target');
return expect(ele.id).toEqual(id);
}
it('fallback of React 17 or lower', () => {
const errorSpy = jest.spyOn(console, 'error');
const originEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
// SSR
const content = renderToString(
<React.StrictMode>
<Demo />
</React.StrictMode>,
);
expect(content).toContain('ssr-id');
// Hydrate
resetUuid();
const holder = document.createElement('div');
holder.innerHTML = content;
const { container } = render(
<React.StrictMode>
<Demo />
</React.StrictMode>,
{ hydrate: true, container: holder },
);
matchId(container, 'rc_unique_1');
errorSpy.mockRestore();
process.env.NODE_ENV = originEnv;
});
it('force use compat id in test env', () => {
const DemoForce: React.FC<{ id?: string }> = ({ id }) => {
const mergedId = useId(id, true);
return <div id={mergedId} className="target-force" />;
};
resetUuid();
const { container } = render(<DemoForce />);
const ele = container.querySelector('.target-force');
expect(ele.id).toBeTruthy();
expect(ele.id).not.toEqual('test-id');
});
});
});