|
| 1 | +import assert from 'node:assert' |
| 2 | +import { afterEach, beforeEach, describe, it } from 'node:test' |
| 3 | +import * as React from 'react' |
| 4 | + |
| 5 | +import { AriaLiveContainer } from '../../../src/ui/accessibility/aria-live-container' |
| 6 | +import { |
| 7 | + advanceTimersBy, |
| 8 | + enableTestTimers, |
| 9 | + resetTestTimers, |
| 10 | +} from '../../helpers/ui/timers' |
| 11 | +import { render } from '../../helpers/ui/render' |
| 12 | + |
| 13 | +describe('AriaLiveContainer', () => { |
| 14 | + beforeEach(() => { |
| 15 | + enableTestTimers(['Date', 'setTimeout']) |
| 16 | + }) |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + resetTestTimers() |
| 20 | + }) |
| 21 | + |
| 22 | + it('renders a polite live region with the provided id and message', () => { |
| 23 | + const view = render( |
| 24 | + <AriaLiveContainer id="branch-status" message="3 branches found" /> |
| 25 | + ) |
| 26 | + |
| 27 | + const container = view.container.querySelector('#branch-status.sr-only') |
| 28 | + |
| 29 | + assert.notEqual(container, null) |
| 30 | + assert.equal(container?.getAttribute('aria-live'), 'polite') |
| 31 | + assert.equal(container?.getAttribute('aria-atomic'), 'true') |
| 32 | + assert.equal(container?.textContent, '3 branches found') |
| 33 | + |
| 34 | + view.rerender(<AriaLiveContainer id="branch-status" message={null} />) |
| 35 | + |
| 36 | + assert.equal(container?.textContent, '') |
| 37 | + }) |
| 38 | + |
| 39 | + it('rebuilds the message after tracked user input changes', () => { |
| 40 | + const view = render( |
| 41 | + <AriaLiveContainer message="1 result" trackedUserInput="m" /> |
| 42 | + ) |
| 43 | + |
| 44 | + const container = view.container.querySelector('.sr-only') |
| 45 | + |
| 46 | + // Initial render toggles suffix from '' → '\u00A0\u00A0' |
| 47 | + assert.equal(container?.textContent, '1 result\u00A0\u00A0') |
| 48 | + |
| 49 | + view.rerender( |
| 50 | + <AriaLiveContainer message="1 result" trackedUserInput="ma" /> |
| 51 | + ) |
| 52 | + |
| 53 | + // Debounce hasn't fired yet, so the message is unchanged |
| 54 | + assert.equal(container?.textContent, '1 result\u00A0\u00A0') |
| 55 | + |
| 56 | + advanceTimersBy(1001) |
| 57 | + |
| 58 | + // After debounce, suffix toggles to '\u00A0' |
| 59 | + assert.equal(container?.textContent, '1 result\u00A0') |
| 60 | + }) |
| 61 | +}) |
0 commit comments