-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.helpers.ts
53 lines (44 loc) · 1.68 KB
/
test.helpers.ts
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
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import React from 'react'
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'
export * from '@testing-library/react'
const hasChildren = (node: any) => node && (node.children || (node.props && node.props.children))
const getChildren = (node: any) =>
node && node.children ? node.children : node.props && node.props.children
export const renderNodes = (reactNodes: any) => {
if (typeof reactNodes === 'string') {
return reactNodes
}
return Object.keys(reactNodes).map((key, i) => {
const child = reactNodes[key]
const isElement = React.isValidElement(child)
if (typeof child === 'string') {
return child
}
if (hasChildren(child)) {
const inner = renderNodes(getChildren(child)) as any
return React.cloneElement(child, { ...child.props, key: i }, inner)
}
if (typeof child === 'object' && !isElement) {
return Object.keys(child).reduce((str, childKey) => child[childKey], '')
}
return child
})
}
export const timezoneMock = function (locale: string, zone: string) {
const DateTimeFormat = Intl.DateTimeFormat
jest
.spyOn(global.Intl, 'DateTimeFormat')
.mockImplementation((l, options) => new DateTimeFormat(locale, { ...options, timeZone: zone }))
}
export const mockResponse = {
status: 200,
statusText: '',
headers: {},
config: {},
data: [],
}
export const mockedRecoilValue = useRecoilValue as jest.MockedFn<typeof useRecoilValue>
export const mockedRecoilState = useRecoilState as jest.MockedFn<typeof useRecoilState>
export const mockedSetRecoilState = useSetRecoilState as jest.MockedFn<typeof useSetRecoilState>