|
| 1 | +import {describe, test, expect, afterEach} from 'vitest' |
| 2 | +import '../define' |
| 3 | +import {findOrCreateLiveRegion, getClosestLiveRegion} from '../query' |
| 4 | + |
| 5 | +afterEach(() => { |
| 6 | + document.body.innerHTML = '' |
| 7 | +}) |
| 8 | + |
| 9 | +describe('findOrCreateLiveRegion', () => { |
| 10 | + test('no live region', () => { |
| 11 | + expect(document.querySelector('live-region')).toBe(null) |
| 12 | + const liveRegion = findOrCreateLiveRegion() |
| 13 | + expect(liveRegion).toBeInTheDocument() |
| 14 | + expect(document.querySelector('live-region')).toBe(liveRegion) |
| 15 | + }) |
| 16 | + |
| 17 | + test('existing live region', () => { |
| 18 | + const liveRegion = document.createElement('live-region') |
| 19 | + document.body.appendChild(liveRegion) |
| 20 | + expect(findOrCreateLiveRegion()).toBe(liveRegion) |
| 21 | + }) |
| 22 | + |
| 23 | + test('in dialog with live region', () => { |
| 24 | + document.body.innerHTML = ` |
| 25 | + <dialog> |
| 26 | + <div id="target"></div> |
| 27 | + <live-region id="local"></live-region> |
| 28 | + </dialog> |
| 29 | + <live-region id="global"></live-region> |
| 30 | + ` |
| 31 | + const liveRegion = findOrCreateLiveRegion(document.getElementById('target')!) |
| 32 | + expect(liveRegion).toBe(document.getElementById('local')) |
| 33 | + }) |
| 34 | + |
| 35 | + test('in dialog with no live region', () => { |
| 36 | + document.body.innerHTML = ` |
| 37 | + <dialog> |
| 38 | + <div id="target"></div> |
| 39 | + </dialog> |
| 40 | + <live-region id="global"></live-region> |
| 41 | + ` |
| 42 | + const dialog = document.querySelector('dialog')! |
| 43 | + expect(dialog.querySelector('live-region')).toBe(null) |
| 44 | + |
| 45 | + const liveRegion = findOrCreateLiveRegion(document.getElementById('target')!) |
| 46 | + expect(dialog).toContainElement(liveRegion) |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe('getClosestLiveRegion', () => { |
| 51 | + test('no live region', () => { |
| 52 | + const element = document.createElement('div') |
| 53 | + document.body.appendChild(element) |
| 54 | + expect(getClosestLiveRegion(element)).toBe(null) |
| 55 | + }) |
| 56 | + |
| 57 | + test('live region in document.body', () => { |
| 58 | + const element = document.createElement('div') |
| 59 | + document.body.appendChild(element) |
| 60 | + |
| 61 | + const liveRegion = document.createElement('live-region') |
| 62 | + document.body.appendChild(liveRegion) |
| 63 | + |
| 64 | + expect(getClosestLiveRegion(element)).toBe(liveRegion) |
| 65 | + }) |
| 66 | + |
| 67 | + test('live region as sibling', () => { |
| 68 | + document.body.innerHTML = ` |
| 69 | + <div> |
| 70 | + <div id="target"></div> |
| 71 | + <live-region id="sibling"></live-region> |
| 72 | + </div> |
| 73 | + <live-region id="global"></live-region> |
| 74 | + ` |
| 75 | + |
| 76 | + expect(getClosestLiveRegion(document.getElementById('target')!)).toBe(document.getElementById('sibling')) |
| 77 | + }) |
| 78 | + |
| 79 | + test('live region within dialog', () => { |
| 80 | + document.body.innerHTML = ` |
| 81 | + <dialog> |
| 82 | + <div id="target"></div> |
| 83 | + <live-region id="local"></live-region> |
| 84 | + </dialog> |
| 85 | + <live-region id="global"></live-region> |
| 86 | + ` |
| 87 | + expect(getClosestLiveRegion(document.getElementById('target')!)).toBe(document.getElementById('local')) |
| 88 | + }) |
| 89 | + |
| 90 | + test('no live region within dialog', () => { |
| 91 | + document.body.innerHTML = ` |
| 92 | + <dialog> |
| 93 | + <div id="target"></div> |
| 94 | + </dialog> |
| 95 | + <live-region id="global"></live-region> |
| 96 | + ` |
| 97 | + expect(getClosestLiveRegion(document.getElementById('target')!)).toBe(null) |
| 98 | + }) |
| 99 | +}) |
0 commit comments