Skip to content

Commit

Permalink
test: add unit tests for App class
Browse files Browse the repository at this point in the history
Added unit tests for the `App` class to verify correct element
sanitization and the creation of a `DocumentFragment` with sanitized
elements. Tests ensure that all sanitized elements are instances of
`HTMLElement` and that the created fragment contains only `HTMLElement`
nodes.
  • Loading branch information
chessurisme committed Jul 30, 2024
1 parent 2bb4033 commit 0fc6e71
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { JSDOM } from 'jsdom'
import { App } from '../app'

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('App', () => {
beforeEach(() => {
document.body.innerHTML = ''
})

it('should sanitize elements correctly', () => {
const appInstance = new App()

expect(appInstance.sanitizedElements.every((element) => element instanceof HTMLElement)).toBe(
true
)
})

it('should create and append sanitized elements to a DocumentFragment', () => {
const appInstance = new App()
const fragment = appInstance.create()

expect(fragment).toBeInstanceOf(DocumentFragment)
expect(Array.from(fragment.childNodes).every((node) => node instanceof HTMLElement)).toBe(true)
})
})

0 comments on commit 0fc6e71

Please sign in to comment.