-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjest.setup.ts
More file actions
47 lines (36 loc) · 1 KB
/
jest.setup.ts
File metadata and controls
47 lines (36 loc) · 1 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
import '@testing-library/jest-dom'
let callstacks: Record<string, string> = {}
let prevIds: string[] = []
let useIdMode: 'react19' | 'react18' = 'react19'
afterEach(() => {
callstacks = {}
prevIds = []
useIdMode = 'react19'
})
const react = jest.requireActual('react')
const generateId = () => {
if (useIdMode === 'react19') return react.useId()
const id1 = react.useId()
const id2 = react.useId()
if (prevIds.includes(id1)) return id2
prevIds.push(id1)
return id1
}
jest.mock('react', () => ({
...react,
useId: generateId,
}))
// React's `useId` gives new ids in the same callstack when a component tree is
// destroyed/unmounted. Call this to manually force ids to be recreated in tests
// to mimic React's behavior.
;(globalThis as any).clearUseIdEntry = (idNum: number) => {
const key = Object.keys(callstacks).find(
key => callstacks[key] === `:r${idNum}:`
)
if (key) {
delete callstacks[key]
}
}
;(globalThis as any).useReact18UseId = () => {
useIdMode = 'react18'
}