-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: expand test suite for removeEventListeners
Added tests to verify the proper function of `removeEventListeners`, including the removal of multiple event listeners and validation checks. Included coverage for the following scenarios: - Removal of event listeners from a valid HTML element - Handling cases where the element or events array is not provided - Validation of event object properties within the array - Correct logging of errors and result return for invalid inputs The updated test suite now ensures comprehensive coverage of edge cases and proper error handling in the `removeEventListeners` function.
- Loading branch information
1 parent
2926ba3
commit d752b54
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/components/utilities/__tests__/remove-event-listeners.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { JSDOM } from 'jsdom' | ||
import { removeEventListeners } from '../remove-event-listeners' | ||
|
||
const dom = new JSDOM('<!DOCTYPE html>') | ||
global.window = dom.window | ||
global.document = window.document | ||
global.HTMLElement = window.HTMLElement | ||
global.MouseEvent = window.MouseEvent | ||
global.DocumentFragment = window.DocumentFragment | ||
|
||
describe('removeEventListeners', () => { | ||
it('should remove event listeners from a valid HTML element', () => { | ||
const element = document.createElement('div') | ||
const handleClick = jest.fn() | ||
element.addEventListener('click', handleClick) | ||
|
||
const events = [{ type: 'click', func: handleClick }] | ||
const result = removeEventListeners(element, events) | ||
|
||
expect(result).toBe(true) | ||
element.click() | ||
expect(handleClick).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should return false if the element is not provided', () => { | ||
const spyConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
const events = [{ type: 'click', func: () => {} }] | ||
|
||
const result = removeEventListeners(null, events) | ||
|
||
expect(result).toBe(false) | ||
expect(spyConsoleError).toHaveBeenCalledWith( | ||
'Cannot remove event listener! Check if the element or the events is valid.' | ||
) | ||
spyConsoleError.mockRestore() | ||
}) | ||
|
||
it('should return false if the events array is not provided', () => { | ||
const spyConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
const element = document.createElement('div') | ||
|
||
const result = removeEventListeners(element, null) | ||
|
||
expect(result).toBe(false) | ||
expect(spyConsoleError).toHaveBeenCalledWith( | ||
'Cannot remove event listener! Check if the element or the events is valid.' | ||
) | ||
spyConsoleError.mockRestore() | ||
}) | ||
|
||
it('should return false if any event object in the array is invalid', () => { | ||
const spyConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
const element = document.createElement('div') | ||
const events = [ | ||
{ type: 'click', func: () => {} }, | ||
{ type: 'mouseover', func: 'not a function' } | ||
] | ||
|
||
const result = removeEventListeners(element, events) | ||
|
||
expect(result).toBe(false) | ||
expect(spyConsoleError).toHaveBeenCalledWith( | ||
'Cannot remove event listener! Check if the element or the events is valid.' | ||
) | ||
spyConsoleError.mockRestore() | ||
}) | ||
|
||
it('should log an error message when parameters are invalid', () => { | ||
const spyConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
const result = removeEventListeners(null, null) | ||
|
||
expect(result).toBe(false) | ||
expect(spyConsoleError).toHaveBeenCalledWith( | ||
'Cannot remove event listener! Check if the element or the events is valid.' | ||
) | ||
spyConsoleError.mockRestore() | ||
}) | ||
}) |