Skip to content

Commit f4dc023

Browse files
committed
test: boolean-attr tests and DOM helper 🌅
- Add DOM.ts with document export - Add Constant.booleanAttrKeys test - Add Render tests for checked, disabled, fieldset, hidden, inert, readonly, selected
1 parent 04065a6 commit f4dc023

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

tests/Constant.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ Deno.test('Constant.allowedPropsKeys excludes type and children', () => {
2626
assertEquals(Constant.allowedPropsKeys.size, Constant.allowedNodeKeys.size - 2)
2727
})
2828

29+
Deno.test('Constant.booleanAttrKeys contains expected boolean attributes', () => {
30+
const expected = ['checked', 'disabled', 'readonly', 'selected', 'inert', 'hidden']
31+
assertEquals(Constant.booleanAttrKeys.size, expected.length)
32+
for (const key of expected) {
33+
assertEquals(Constant.booleanAttrKeys.has(key), true)
34+
}
35+
})
36+
2937
Deno.test('Constant.containerTags does not include void tags', () => {
3038
for (const tag of Constant.voidTags) {
3139
assertEquals(Constant.containerTags.includes(tag), false)

tests/DOM.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { parseHTML } from 'linkedom'
2+
3+
const { window } = parseHTML('<!DOCTYPE html><html><body></body></html>')
4+
const globalScope = globalThis as Record<string, unknown>
5+
globalScope['HTMLElement'] = window.HTMLElement
6+
globalScope['Element'] = window.Element
7+
globalScope['HTMLInputElement'] = window.HTMLInputElement
8+
globalScope['HTMLButtonElement'] = window.HTMLButtonElement
9+
globalScope['HTMLTextAreaElement'] = window.HTMLTextAreaElement
10+
globalScope['HTMLSelectElement'] = window.HTMLSelectElement
11+
globalScope['HTMLFieldSetElement'] = window.HTMLFieldSetElement
12+
globalScope['HTMLOptGroupElement'] = window.HTMLOptGroupElement
13+
globalScope['HTMLOptionElement'] = window.HTMLOptionElement
14+
15+
export const document = window.document

tests/Render.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { document } from './DOM.ts'
12
import { assertEquals } from '@std/assert'
23
import Create from '@app/Create.ts'
34
import Helper from '@app/Helper.ts'
@@ -28,6 +29,92 @@ function mockDocument(): Document {
2829
} as unknown as Document
2930
}
3031

32+
Deno.test('Render.applyAttrs sets checked false and removes attribute when attrs.checked is false', () => {
33+
const input = document.createElement('input')
34+
input.setAttribute('checked', 'checked')
35+
input.checked = true
36+
Render.applyAttrs(input, { checked: false })
37+
assertEquals(input.checked, false)
38+
assertEquals(input.getAttribute('checked'), null)
39+
})
40+
41+
Deno.test('Render.applyAttrs sets checked false when attrs.checked is string false', () => {
42+
const input = document.createElement('input')
43+
input.checked = true
44+
Render.applyAttrs(input, { checked: 'false' })
45+
assertEquals(input.checked, false)
46+
assertEquals(input.getAttribute('checked'), null)
47+
})
48+
49+
Deno.test('Render.applyAttrs sets checked true when attrs.checked is true', () => {
50+
const input = document.createElement('input')
51+
Render.applyAttrs(input, { checked: true })
52+
assertEquals(input.checked, true)
53+
})
54+
55+
Deno.test('Render.applyAttrs sets disabled false and removes attribute when attrs.disabled is false', () => {
56+
const button = document.createElement('button')
57+
button.setAttribute('disabled', 'disabled')
58+
button.disabled = true
59+
Render.applyAttrs(button, { disabled: false })
60+
assertEquals(button.disabled, false)
61+
assertEquals(button.getAttribute('disabled'), null)
62+
})
63+
64+
Deno.test('Render.applyAttrs sets disabled on fieldset', () => {
65+
const fieldset = document.createElement('fieldset')
66+
Render.applyAttrs(fieldset, { disabled: true })
67+
if ('disabled' in fieldset && typeof (fieldset as HTMLFieldSetElement).disabled === 'boolean') {
68+
assertEquals((fieldset as HTMLFieldSetElement).disabled, true)
69+
}
70+
Render.applyAttrs(fieldset, { disabled: false })
71+
if ('disabled' in fieldset && typeof (fieldset as HTMLFieldSetElement).disabled === 'boolean') {
72+
assertEquals((fieldset as HTMLFieldSetElement).disabled, false)
73+
}
74+
})
75+
76+
Deno.test('Render.applyAttrs sets disabled true when attrs.disabled is true', () => {
77+
const button = document.createElement('button')
78+
Render.applyAttrs(button, { disabled: true })
79+
assertEquals(button.disabled, true)
80+
})
81+
82+
Deno.test('Render.applyAttrs sets hidden false and removes hidden attribute on div', () => {
83+
const div = document.createElement('div')
84+
div.setAttribute('hidden', '')
85+
div.hidden = true
86+
Render.applyAttrs(div, { hidden: false })
87+
assertEquals(div.hidden, false)
88+
assertEquals(div.getAttribute('hidden'), null)
89+
})
90+
91+
Deno.test('Render.applyAttrs sets inert false and removes inert attribute on div', () => {
92+
const div = document.createElement('div')
93+
div.setAttribute('inert', '')
94+
div.inert = true
95+
Render.applyAttrs(div, { inert: false })
96+
assertEquals(div.inert, false)
97+
assertEquals(div.getAttribute('inert'), null)
98+
})
99+
100+
Deno.test('Render.applyAttrs sets readOnly false and removes readonly attribute on input', () => {
101+
const input = document.createElement('input')
102+
input.setAttribute('readonly', 'readonly')
103+
input.readOnly = true
104+
Render.applyAttrs(input, { readonly: false })
105+
assertEquals(input.readOnly, false)
106+
assertEquals(input.getAttribute('readonly'), null)
107+
})
108+
109+
Deno.test('Render.applyAttrs sets selected false and removes selected attribute on option', () => {
110+
const option = document.createElement('option')
111+
option.setAttribute('selected', 'selected')
112+
option.selected = true
113+
Render.applyAttrs(option, { selected: false })
114+
assertEquals(option.selected, false)
115+
assertEquals(option.getAttribute('selected'), null)
116+
})
117+
31118
Deno.test('Render.applyLayout does not override existing non-static position', () => {
32119
const style: Record<string, string> = { position: 'absolute' }
33120
const element = { style } as unknown as HTMLElement

0 commit comments

Comments
 (0)