forked from motiondivision/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.tsx
58 lines (54 loc) · 1.62 KB
/
jest.setup.tsx
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
import "jest-dom/extend-expect"
// Get fireEvent from the native testing library
// because @testing-library/react one switches out mouseEnter and mouseLeave
import { fireEvent, getByTestId } from "@testing-library/dom"
import { render as testRender, act } from "@testing-library/react"
import * as React from "react"
// Stub ResizeObserver
if (!(global as any).ResizeObserver) {
;(global as any).ResizeObserver = class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
} as any
}
export const click = (element: Element) =>
act(() => {
fireEvent.click(element)
})
export const mouseEnter = (element: Element) =>
act(() => {
fireEvent.mouseEnter(element)
})
export const mouseLeave = (element: Element) =>
act(() => {
fireEvent.mouseLeave(element)
})
export const mouseDown = (element: Element) =>
act(() => {
fireEvent.mouseDown(element)
})
export const mouseUp = (element: Element) =>
act(() => {
fireEvent.mouseUp(element)
})
export const focus = (element: HTMLElement, testId: string) =>
act(() => {
getByTestId(element, testId).focus()
})
export const blur = (element: HTMLElement, testId: string) =>
act(() => {
getByTestId(element, testId).blur()
})
export const render = (children: any) => {
const renderReturn = testRender(
<React.StrictMode>{children}</React.StrictMode>
)
return {
...renderReturn,
rerender: (children: any) =>
renderReturn.rerender(
<React.StrictMode>{children}</React.StrictMode>
),
}
}