Skip to content

Commit d4d41eb

Browse files
committed
Add test for indeterminate state
1 parent 172543e commit d4d41eb

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,54 @@ describe('details-menu element', function() {
350350
})
351351
})
352352

353+
describe('with labels as menu item with indeterminate checkboxes', function() {
354+
beforeEach(function() {
355+
const container = document.createElement('div')
356+
container.innerHTML = `
357+
<details>
358+
<summary>Click</summary>
359+
<details-menu>
360+
<label tabindex="0" role="menuitemcheckbox" aria-checked="false"><input type="checkbox" name="robot"> Hubot</label>
361+
<label tabindex="0" role="menuitemcheckbox" aria-checked="true"><input type="checkbox" name="robot" checked> Bender</label>
362+
<label tabindex="0" role="menuitemcheckbox" aria-checked="false"><input type="checkbox" name="robot"> BB-8</label>
363+
</details-menu>
364+
</details>
365+
`
366+
document.body.append(container)
367+
})
368+
369+
afterEach(function() {
370+
document.body.innerHTML = ''
371+
})
372+
373+
it('manages checked state and fires events', function() {
374+
const details = document.querySelector('details')
375+
const summary = document.querySelector('summary')
376+
const item = details.querySelector('label')
377+
const input = item.querySelector('input')
378+
let eventCounter = 0
379+
document.addEventListener('details-menu-selected', () => eventCounter++, true)
380+
381+
input.indeterminate = true
382+
input.dispatchEvent(new Event('change', {bubbles: true}))
383+
384+
summary.dispatchEvent(new MouseEvent('click', {bubbles: true}))
385+
assert(details.open, 'menu opens')
386+
assert.equal(item.getAttribute('aria-checked'), 'mixed')
387+
388+
item.dispatchEvent(new MouseEvent('click', {bubbles: true}))
389+
assert(details.open, 'menu stays open')
390+
assert.equal(item.getAttribute('aria-checked'), 'true')
391+
assert.equal(details.querySelectorAll('[aria-checked="true"]').length, 2)
392+
393+
item.dispatchEvent(new MouseEvent('click', {bubbles: true}))
394+
assert.equal(item.getAttribute('aria-checked'), 'false')
395+
assert.equal(details.querySelectorAll('[aria-checked="true"]').length, 1)
396+
397+
assert.equal(eventCounter, 3, 'selected event is fired three times')
398+
})
399+
})
400+
353401
describe('with no valid menu items', function() {
354402
beforeEach(function() {
355403
const container = document.createElement('div')

0 commit comments

Comments
 (0)