-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathdispatch-event.ts
More file actions
39 lines (35 loc) · 1015 Bytes
/
dispatch-event.ts
File metadata and controls
39 lines (35 loc) · 1015 Bytes
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
import type { ReactTestInstance } from 'react-test-renderer';
import act from '../../act';
import { getEventHandler } from '../../event-handler';
import { isElementMounted } from '../../helpers/component-tree';
import { formatElement } from '../../helpers/format-element';
import { logger } from '../../helpers/logger';
/**
* Basic dispatch event function used by User Event module.
*
* @param element element trigger event on
* @param eventName name of the event
* @param event event payload(s)
*/
export async function dispatchEvent(
element: ReactTestInstance,
eventName: string,
...event: unknown[]
) {
if (!isElementMounted(element)) {
return;
}
const handler = getEventHandler(element, eventName);
if (!handler) {
logger.warn(
`User Event: no event handler for "${eventName}" found on ${formatElement(element, {
compact: true,
})}`,
);
return;
}
// eslint-disable-next-line require-await
await act(async () => {
handler(...event);
});
}