Skip to content

Commit

Permalink
test: expand test suite for removeEventListeners
Browse files Browse the repository at this point in the history
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
chessurisme committed Jul 31, 2024
1 parent 437f3d9 commit 9f569dd
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/components/utilities/__tests__/remove-event-listeners.test.js
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()
})
})

0 comments on commit 9f569dd

Please sign in to comment.