Skip to content

Commit 8132254

Browse files
committed
feat: add support for globalThis in event handling functions
1 parent e395d5b commit 8132254

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/__tests__/events.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,35 @@ test('fires events on Document', () => {
434434
document.removeEventListener('keydown', keyDownSpy)
435435
})
436436

437+
test('fires events on globalThis', () => {
438+
/* eslint-disable no-undef */
439+
const clickSpy = jest.fn()
440+
globalThis.addEventListener('click', clickSpy)
441+
fireEvent.click(globalThis)
442+
expect(clickSpy).toHaveBeenCalledTimes(1)
443+
globalThis.removeEventListener('click', clickSpy)
444+
/* eslint-enable no-undef */
445+
})
446+
447+
test('fires created events on globalThis', () => {
448+
/* eslint-disable no-undef */
449+
const spy = jest.fn()
450+
globalThis.addEventListener('click', spy)
451+
const event = createEvent.click(globalThis)
452+
fireEvent(globalThis, event)
453+
expect(spy).toHaveBeenCalledTimes(1)
454+
expect(spy).toHaveBeenCalledWith(event)
455+
globalThis.removeEventListener('click', spy)
456+
/* eslint-enable no-undef */
457+
})
458+
459+
test('creates events for globalThis', () => {
460+
/* eslint-disable no-undef */
461+
const event = createEvent.click(globalThis)
462+
expect(event).toBeInstanceOf(MouseEvent)
463+
/* eslint-enable no-undef */
464+
})
465+
437466
test('can create generic events', () => {
438467
const el = document.createElement('div')
439468
const eventName = 'my-custom-event'

types/__tests__/type-tests.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ export function eventTest() {
210210
}
211211
fireEvent.click(element.firstChild)
212212

213+
// GlobalThis
214+
fireEvent.click(globalThis)
215+
const globalThisEvent = createEvent('customEvent', globalThis)
216+
fireEvent(globalThis, globalThisEvent)
217+
213218
// Custom event
214219
const customEvent = createEvent('customEvent', element)
215220
fireEvent(element, customEvent)

types/events.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,24 @@ export type EventType =
9292
| 'pageShow'
9393

9494
export type FireFunction = (
95-
element: Document | Element | Window | Node,
95+
element: Document | Element | Window | Node | GlobalThis,
9696
event: Event,
9797
) => boolean
9898
export type FireObject = {
9999
[K in EventType]: (
100-
element: Document | Element | Window | Node,
100+
element: Document | Element | Window | Node | GlobalThis,
101101
options?: {},
102102
) => boolean
103103
}
104104
export type CreateFunction = (
105105
eventName: string,
106-
node: Document | Element | Window | Node,
106+
node: Document | Element | Window | Node | GlobalThis,
107107
init?: {},
108108
options?: {EventType?: string; defaultInit?: {}},
109109
) => Event
110110
export type CreateObject = {
111111
[K in EventType]: (
112-
element: Document | Element | Window | Node,
112+
element: Document | Element | Window | Node | GlobalThis,
113113
options?: {},
114114
) => Event
115115
}

0 commit comments

Comments
 (0)