-
Notifications
You must be signed in to change notification settings - Fork 239
/
index.ts
182 lines (156 loc) Β· 4.37 KB
/
index.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {getSpy} from './_mockApis'
import {getConfig} from '@testing-library/dom'
import userEvent from '#src'
import {type Instance, type UserEventApi} from '#src/setup/setup'
import {render} from '#testHelpers'
type ApiDeclarations = {
[api in keyof UserEventApi]: {
args?: unknown[]
elementArg?: number
elementHtml?: string
optionsArg?: number
}
}
const apiDeclarations: ApiDeclarations = {
clear: {elementArg: 0},
click: {
elementArg: 0,
},
copy: {},
cut: {},
dblClick: {
elementArg: 0,
},
hover: {
elementArg: 0,
},
unhover: {
elementArg: 0,
},
keyboard: {
args: ['foo'],
},
paste: {
args: ['foo'],
},
pointer: {
args: ['foo'],
},
selectOptions: {
args: [null, ['foo']],
elementArg: 0,
elementHtml: `<select multiple><option>foo</option></select>`,
},
deselectOptions: {
args: [null, ['foo']],
elementArg: 0,
elementHtml: `<select multiple><option>foo</option></select>`,
},
tab: {
optionsArg: 0,
},
tripleClick: {
elementArg: 0,
},
type: {
args: [null, 'foo'],
elementArg: 0,
optionsArg: 2,
},
upload: {
args: [null, new File(['foo'], 'foo.txt')],
elementArg: 0,
elementHtml: `<input type="file"/>`,
},
}
type ApiDeclarationsEntry<
k extends keyof ApiDeclarations = keyof ApiDeclarations,
> = [k, ApiDeclarations[k]]
const apiDeclarationsEntries = Object.entries(
apiDeclarations,
) as ApiDeclarationsEntry[]
const opt = Symbol('testOption')
declare module '#src/setup' {
export interface Config {
[opt]?: true
}
}
declare module '#src/options' {
export interface Options {
[opt]?: true
}
}
// eslint-disable-next-line @typescript-eslint/unbound-method
const realAsyncWrapper = getConfig().asyncWrapper
afterEach(() => {
getConfig().asyncWrapper = realAsyncWrapper
mocks.restoreAllMocks()
})
test.each(apiDeclarationsEntries)(
'call `%s` api on instance',
async (name, {args = [], elementArg, elementHtml = `<input/>`}) => {
const {element} = render<HTMLInputElement>(elementHtml)
if (elementArg !== undefined) {
args[elementArg] = element
}
const spy = getSpy(name)
const apis = userEvent.setup({[opt]: true})
expect(apis[name]).toHaveProperty('name', `mock-${name}`)
// Replace the asyncWrapper to make sure that a delayed state update happens inside of it
const stateUpdate = mocks.fn()
spy.mockImplementation(async function impl(
this: Instance,
...a: Parameters<typeof spy>
) {
const ret = spy.originalMockImplementation.apply(this, a)
void ret.then(() => setTimeout(stateUpdate))
return ret
} as UserEventApi[typeof name])
const asyncWrapper = mocks.fn(async (cb: () => Promise<unknown>) => {
stateUpdate.mockClear()
const ret = cb()
expect(stateUpdate).not.toBeCalled()
await ret
expect(stateUpdate).toBeCalled()
return ret
})
getConfig().asyncWrapper = asyncWrapper
await (apis[name] as Function)(...args)
expect(spy).toBeCalledTimes(1)
expect(spy.mock.lastCall?.this.config[opt]).toBe(true)
// Make sure the asyncWrapper mock has been used in the API call
expect(asyncWrapper).toBeCalled()
const subApis = apis.setup({})
await (subApis[name] as Function)(...args)
expect(spy).toBeCalledTimes(2)
expect(spy.mock.lastCall?.this.config[opt]).toBe(true)
expect(spy.mock.calls[1].this.system).toBe(spy.mock.calls[0].this.system)
},
)
test.each(apiDeclarationsEntries)(
'call `%s` api as directApi',
async (
name,
{args = [], elementArg, elementHtml = `<input/>`, optionsArg},
) => {
const {element} = render<HTMLInputElement>(elementHtml)
if (elementArg !== undefined) {
args[elementArg] = element
}
args.push({[opt]: true})
const internalArgs =
optionsArg === undefined ? args.slice(0, -1) : [...args]
const spy = getSpy(name)
await (userEvent[name] as Function)(...args)
expect(spy).toBeCalledTimes(1)
expect(spy).toBeCalledWith(...internalArgs)
if (!['clear', 'tab'].includes(name)) {
// TODO: add options param to these direct APIs
expect(spy.mock.lastCall?.this.config[opt]).toBe(true)
}
// options arg can be omitted
await (userEvent[name] as Function)(...args.slice(0, -1))
expect(spy).toBeCalledTimes(2)
expect(spy.mock.lastCall?.this.config[opt]).toBe(undefined)
},
)