Skip to content

feat: Allow null type for node on fireEvent and its shortcuts #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/__tests__/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,18 @@ test('throws a useful error message when firing events on non-existent nodes (sh
)
})

test('throws a useful error message when firing events on null', () => {
expect(() => fireEvent(null, new MouseEvent('click'))).toThrow(
'Unable to fire a "click" event - please provide a DOM element.',
)
})

test('throws a useful error message when firing events on null (shortcut)', () => {
expect(() => fireEvent.click(null)).toThrow(
'Unable to fire a "click" event - please provide a DOM element.',
)
})

test('throws a useful error message when firing non-events', () => {
expect(() => fireEvent(document.createElement('div'), undefined)).toThrow(
'Unable to fire an event - please provide an event object.',
Expand Down
4 changes: 4 additions & 0 deletions types/__tests__/type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export function eventTest() {
// Custom event
const customEvent = createEvent('customEvent', element)
fireEvent(element, customEvent)

// null returned by Document.getElementById()
const button = document.getElementById('my-button')
fireEvent.click(button)
}

export async function testWaitFors() {
Expand Down
8 changes: 4 additions & 4 deletions types/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,24 @@ export type EventType =
| 'online'

export type FireFunction = (
element: Document | Element | Window | Node,
element: Document | Element | Window | Node | null,
event: Event,
) => boolean
export type FireObject = {
[K in EventType]: (
element: Document | Element | Window | Node,
element: Document | Element | Window | Node | null,
options?: {},
) => boolean
}
export type CreateFunction = (
eventName: string,
node: Document | Element | Window | Node,
node: Document | Element | Window | Node | null,
init?: {},
options?: {EventType?: string; defaultInit?: {}},
) => Event
export type CreateObject = {
[K in EventType]: (
element: Document | Element | Window | Node,
element: Document | Element | Window | Node | null,
options?: {},
) => Event
}
Expand Down
Loading