|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +/** |
| 3 | + * @file useModalAwareAnnounce.test.tsx |
| 4 | + * @input Uses vitest, Testing Library, useModalAwareAnnounce, useAnnounce |
| 5 | + * @output Tests the route choice: the shared channel outside a modal, a |
| 6 | + * persistent region inside an open native modal dialog (spec:AST-086 FR14) |
| 7 | + * @position Core hook tests |
| 8 | + */ |
| 9 | +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; |
| 10 | +import {act, fireEvent, render, screen} from '@testing-library/react'; |
| 11 | +import {useRef} from 'react'; |
| 12 | +import {useModalAwareAnnounce} from './useModalAwareAnnounce'; |
| 13 | +import {__resetLiveRegionsForTest} from './useAnnounce'; |
| 14 | + |
| 15 | +function Owner({message, urgency}: {message: string; urgency?: 'assertive'}) { |
| 16 | + const ref = useRef<HTMLDivElement>(null); |
| 17 | + const announce = useModalAwareAnnounce(ref); |
| 18 | + return ( |
| 19 | + <div ref={ref}> |
| 20 | + <button type="button" onClick={() => announce(message, urgency)}> |
| 21 | + Say |
| 22 | + </button> |
| 23 | + </div> |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function WithdrawingOwner() { |
| 28 | + const ref = useRef<HTMLDivElement>(null); |
| 29 | + const announce = useModalAwareAnnounce(ref); |
| 30 | + return ( |
| 31 | + <div ref={ref}> |
| 32 | + <button type="button" onClick={() => announce('Added Design')}> |
| 33 | + Say |
| 34 | + </button> |
| 35 | + <button type="button" onClick={() => announce('')}> |
| 36 | + Withdraw |
| 37 | + </button> |
| 38 | + </div> |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +const shared = () => |
| 43 | + document.querySelector('[data-astryx-live-region="polite"]'); |
| 44 | +const sharedAssertive = () => |
| 45 | + document.querySelector('[data-astryx-live-region="assertive"]'); |
| 46 | + |
| 47 | +// jsdom has no showModal(), so an open dialog never matches `:modal`; stand |
| 48 | +// in for a dialog opened modally. |
| 49 | +function standInForModal() { |
| 50 | + const matches = Element.prototype.matches; |
| 51 | + vi.spyOn(Element.prototype, 'matches').mockImplementation(function ( |
| 52 | + this: Element, |
| 53 | + selector: string, |
| 54 | + ) { |
| 55 | + return selector === ':modal' |
| 56 | + ? this instanceof HTMLDialogElement && this.open |
| 57 | + : matches.call(this, selector); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +async function nextFrame() { |
| 62 | + await act( |
| 63 | + async () => |
| 64 | + new Promise<void>(resolve => requestAnimationFrame(() => resolve())), |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +beforeEach(() => { |
| 69 | + __resetLiveRegionsForTest(); |
| 70 | +}); |
| 71 | +afterEach(() => { |
| 72 | + vi.restoreAllMocks(); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('useModalAwareAnnounce', () => { |
| 76 | + it('uses the shared channel outside a modal', async () => { |
| 77 | + render(<Owner message="Added Design" />); |
| 78 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 79 | + await nextFrame(); |
| 80 | + expect(shared()).toHaveTextContent('Added Design'); |
| 81 | + expect( |
| 82 | + document.querySelector('[data-astryx-modal-live-region]'), |
| 83 | + ).toBeNull(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('uses the shared channel inside a dialog that is open but not modal', async () => { |
| 87 | + render( |
| 88 | + <dialog open> |
| 89 | + <Owner message="Added Design" /> |
| 90 | + </dialog>, |
| 91 | + ); |
| 92 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 93 | + await nextFrame(); |
| 94 | + expect(shared()).toHaveTextContent('Added Design'); |
| 95 | + expect( |
| 96 | + document.querySelector('[data-astryx-modal-live-region]'), |
| 97 | + ).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('announces inside an open modal dialog through a persistent polite region, not the shared channel (FR14)', async () => { |
| 101 | + standInForModal(); |
| 102 | + render( |
| 103 | + <dialog open> |
| 104 | + <Owner message="Added Design" /> |
| 105 | + </dialog>, |
| 106 | + ); |
| 107 | + const dialog = document.querySelector('dialog')!; |
| 108 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 109 | + // Inserted empty (FR3), filled a frame later. |
| 110 | + const region = dialog.querySelector( |
| 111 | + ':scope > [data-astryx-modal-live-region="polite"]', |
| 112 | + )!; |
| 113 | + expect(region).toBeInTheDocument(); |
| 114 | + expect(region).toHaveAttribute('role', 'status'); |
| 115 | + expect(region).toHaveAttribute('aria-live', 'polite'); |
| 116 | + expect(region).toHaveAttribute('aria-atomic', 'true'); |
| 117 | + expect(region).toHaveTextContent(''); |
| 118 | + await nextFrame(); |
| 119 | + expect(region).toHaveTextContent('Added Design'); |
| 120 | + expect(shared()?.textContent ?? '').toBe(''); |
| 121 | + // The region persists across messages (FR3) and repeats the same words |
| 122 | + // for a new occasion (FR6). |
| 123 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 124 | + expect(region).toHaveTextContent(''); |
| 125 | + await nextFrame(); |
| 126 | + expect(region).toHaveTextContent('Added Design'); |
| 127 | + expect( |
| 128 | + dialog.querySelectorAll('[data-astryx-modal-live-region="polite"]'), |
| 129 | + ).toHaveLength(1); |
| 130 | + }); |
| 131 | + |
| 132 | + it('keeps an assertive region apart from the polite one (FR5)', async () => { |
| 133 | + standInForModal(); |
| 134 | + render( |
| 135 | + <dialog open> |
| 136 | + <Owner message="Could not add" urgency="assertive" /> |
| 137 | + </dialog>, |
| 138 | + ); |
| 139 | + const dialog = document.querySelector('dialog')!; |
| 140 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 141 | + await nextFrame(); |
| 142 | + const region = dialog.querySelector( |
| 143 | + ':scope > [data-astryx-modal-live-region="assertive"]', |
| 144 | + )!; |
| 145 | + expect(region).toHaveAttribute('role', 'alert'); |
| 146 | + expect(region).toHaveTextContent('Could not add'); |
| 147 | + expect( |
| 148 | + dialog.querySelector('[data-astryx-modal-live-region="polite"]'), |
| 149 | + ).toBeNull(); |
| 150 | + expect(sharedAssertive()?.textContent ?? '').toBe(''); |
| 151 | + }); |
| 152 | + |
| 153 | + it("announce('') inside a modal withdraws the dialog region's pending and shown text", async () => { |
| 154 | + vi.useFakeTimers(); |
| 155 | + try { |
| 156 | + standInForModal(); |
| 157 | + render( |
| 158 | + <dialog open> |
| 159 | + <WithdrawingOwner /> |
| 160 | + </dialog>, |
| 161 | + ); |
| 162 | + const dialog = document.querySelector('dialog')!; |
| 163 | + // Pending write cancelled: nothing lands. |
| 164 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 165 | + fireEvent.click(screen.getByRole('button', {name: 'Withdraw'})); |
| 166 | + await act(async () => { |
| 167 | + await vi.advanceTimersByTimeAsync(50); |
| 168 | + }); |
| 169 | + const region = dialog.querySelector( |
| 170 | + '[data-astryx-modal-live-region="polite"]', |
| 171 | + )!; |
| 172 | + expect(region).toHaveTextContent(''); |
| 173 | + // Shown text cleared at once, not after the two-second delay. |
| 174 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 175 | + await act(async () => { |
| 176 | + await vi.advanceTimersByTimeAsync(50); |
| 177 | + }); |
| 178 | + expect(region).toHaveTextContent('Added Design'); |
| 179 | + fireEvent.click(screen.getByRole('button', {name: 'Withdraw'})); |
| 180 | + expect(region).toHaveTextContent(''); |
| 181 | + expect(region).toBeInTheDocument(); |
| 182 | + // The shared channel was never touched. |
| 183 | + expect(shared()?.textContent ?? '').toBe(''); |
| 184 | + } finally { |
| 185 | + vi.useRealTimers(); |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + it('clears the in-dialog region shortly after announcing', async () => { |
| 190 | + vi.useFakeTimers(); |
| 191 | + try { |
| 192 | + standInForModal(); |
| 193 | + render( |
| 194 | + <dialog open> |
| 195 | + <Owner message="Added Design" /> |
| 196 | + </dialog>, |
| 197 | + ); |
| 198 | + const dialog = document.querySelector('dialog')!; |
| 199 | + fireEvent.click(screen.getByRole('button', {name: 'Say'})); |
| 200 | + await act(async () => { |
| 201 | + await vi.advanceTimersByTimeAsync(20); |
| 202 | + }); |
| 203 | + const region = dialog.querySelector( |
| 204 | + '[data-astryx-modal-live-region="polite"]', |
| 205 | + )!; |
| 206 | + expect(region).toHaveTextContent('Added Design'); |
| 207 | + await act(async () => { |
| 208 | + await vi.advanceTimersByTimeAsync(2100); |
| 209 | + }); |
| 210 | + expect(region).toHaveTextContent(''); |
| 211 | + expect(region).toBeInTheDocument(); |
| 212 | + } finally { |
| 213 | + vi.useRealTimers(); |
| 214 | + } |
| 215 | + }); |
| 216 | +}); |
0 commit comments