MCPcopy Create free account
hub / github.com/testing-library/user-event / dispatchEvent

Function dispatchEvent

src/event/dispatchEvent.ts:27–64  ·  view source on GitHub ↗
(
  this: Instance,
  target: Element,
  event: Event,
  preventDefault: boolean = false,
)

Source from the content-addressed store, hash-verified

25}
26
27export function dispatchEvent(
28 this: Instance,
29 target: Element,
30 event: Event,
31 preventDefault: boolean = false,
32) {
33 const type = event.type as EventType
34 const behaviorImplementation = preventDefault
35 ? () => {}
36 : (behavior[type] as BehaviorPlugin<EventType> | undefined)?.(
37 event,
38 target,
39 this,
40 )
41
42 if (behaviorImplementation) {
43 event.preventDefault()
44 let defaultPrevented = false
45 Object.defineProperty(event, 'defaultPrevented', {
46 get: () => defaultPrevented,
47 })
48 Object.defineProperty(event, 'preventDefault', {
49 value: () => {
50 defaultPrevented = event.cancelable
51 },
52 })
53
54 wrapEvent(() => target.dispatchEvent(event), target)
55
56 if (!defaultPrevented as boolean) {
57 behaviorImplementation()
58 }
59
60 return !defaultPrevented
61 }
62
63 return wrapEvent(() => target.dispatchEvent(event), target)
64}
65
66export function dispatchDOMEvent<K extends EventType>(
67 target: Element,

Callers

nothing calls this directly

Calls 1

wrapEventFunction · 0.90

Tested by

no test coverage detected