-
Notifications
You must be signed in to change notification settings - Fork 240
/
setup.ts
179 lines (159 loc) Β· 4.43 KB
/
setup.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
import {prepareDocument} from '../document/prepareDocument'
import {dispatchEvent, dispatchUIEvent} from '../event'
import {defaultKeyMap as defaultKeyboardMap} from '../keyboard/keyMap'
import {defaultKeyMap as defaultPointerMap} from '../pointer/keyMap'
import {Options, PointerEventsCheckLevel} from '../options'
import {
ApiLevel,
attachClipboardStubToView,
getDocumentFromNode,
setLevelRef,
wait,
} from '../utils'
import {System} from '../system'
import {userEventApi} from './api'
import {wrapAsync} from './wrapAsync'
import {DirectOptions} from './directApi'
/**
* Default options applied when API is called per `userEvent.anyApi()`
*/
const defaultOptionsDirect: Required<Options> = {
applyAccept: true,
autoModify: true,
delay: 0,
document: globalThis.document,
keyboardMap: defaultKeyboardMap,
pointerMap: defaultPointerMap,
pointerEventsCheck: PointerEventsCheckLevel.EachApiCall,
skipAutoClose: false,
skipClick: false,
skipHover: false,
writeToClipboard: false,
advanceTimers: () => Promise.resolve(),
}
/**
* Default options applied when API is called per `userEvent().anyApi()`
*/
const defaultOptionsSetup: Required<Options> = {
...defaultOptionsDirect,
writeToClipboard: true,
}
export type UserEventApi = typeof userEventApi
export type UserEvent = {
readonly setup: (...args: Parameters<typeof setupSub>) => UserEvent
} & {
readonly [k in keyof UserEventApi]: (
...args: Parameters<UserEventApi[k]>
) => ReturnType<UserEventApi[k]>
}
export type Instance = UserEventApi & {
config: Config
dispatchEvent: OmitThisParameter<typeof dispatchEvent>
dispatchUIEvent: OmitThisParameter<typeof dispatchUIEvent>
system: System
levelRefs: Record<number, object | undefined>
}
export type Config = Required<Options>
export function createConfig(
options: Options = {},
defaults: Required<Options> = defaultOptionsSetup,
node?: Node,
): Config {
const document = getDocument(options, node, defaults)
return {
...defaults,
...options,
document,
}
}
/**
* Start a "session" with userEvent.
* All APIs returned by this function share an input device state and a default configuration.
*/
export function setupMain(options: Options = {}) {
const config = createConfig(options)
prepareDocument(config.document)
const view =
config.document.defaultView ?? /* istanbul ignore next */ globalThis.window
attachClipboardStubToView(view)
return createInstance(config).api
}
/**
* Setup in direct call per `userEvent.anyApi()`
*/
export function setupDirect(
{
keyboardState,
pointerState,
...options
}: DirectOptions & {keyboardState?: System; pointerState?: System} = {}, // backward-compatibility
node?: Node,
) {
const config = createConfig(options, defaultOptionsDirect, node)
prepareDocument(config.document)
const system = pointerState ?? keyboardState ?? new System()
return {
api: createInstance(config, system).api,
system,
}
}
/**
* Create a set of callbacks with different default settings but the same state.
*/
export function setupSub(this: Instance, options: Options) {
return createInstance({...this.config, ...options}, this.system).api
}
function wrapAndBindImpl<
Args extends unknown[],
Impl extends (this: Instance, ...args: Args) => Promise<unknown>,
>(instance: Instance, impl: Impl) {
function method(...args: Args) {
setLevelRef(instance, ApiLevel.Call)
return wrapAsync(() =>
impl.apply(instance, args).then(async ret => {
await wait(instance.config)
return ret
}),
)
}
Object.defineProperty(method, 'name', {get: () => impl.name})
return method
}
export function createInstance(
config: Config,
system: System = new System(),
): {
instance: Instance
api: UserEvent
} {
const instance = {} as Instance
Object.assign(instance, {
config,
dispatchEvent: dispatchEvent.bind(instance),
dispatchUIEvent: dispatchUIEvent.bind(instance),
system,
levelRefs: {},
...userEventApi,
})
return {
instance,
api: {
...(Object.fromEntries(
Object.entries(userEventApi).map(([name, api]) => [
name,
wrapAndBindImpl(instance, api),
]),
) as UserEventApi),
setup: setupSub.bind(instance),
},
}
}
function getDocument(
options: Partial<Config>,
node: Node | undefined,
defaults: Required<Options>,
) {
return (
options.document ?? (node && getDocumentFromNode(node)) ?? defaults.document
)
}