Skip to content

Commit 42c20dd

Browse files
authoredJan 22, 2025··
bug: ensure Announce components work without pre-existing text node (#5568)
* bug: ensure mutation observer is triggered when content is added to Announce without pre-existing text. * Comment change out and add tests * Set childList: true * Remove unused * Create afraid-pianos-invent.md
1 parent 878b61f commit 42c20dd

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
 

‎.changeset/afraid-pianos-invent.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
bug: ensure Announce components work without pre-existing text node

‎packages/react/src/live-region/Announce.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export function Announce({
108108
observer.observe(container, {
109109
subtree: true,
110110
characterData: true,
111+
childList: true,
111112
})
112113

113114
return () => {

‎packages/react/src/live-region/__tests__/AriaAlert.test.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {render, screen} from '@testing-library/react'
22
import React from 'react'
33
import {AriaAlert} from '../AriaAlert'
4+
import {userEvent} from '@testing-library/user-event'
45
import {getLiveRegion} from '../../utils/testing'
56

67
describe('AriaAlert', () => {
@@ -46,4 +47,32 @@ describe('AriaAlert', () => {
4647
)
4748
expect(screen.getByTestId('container').tagName).toBe('SPAN')
4849
})
50+
51+
it('should update live-region element when AriaAlert goes from empty to populated', async () => {
52+
function TestComponent() {
53+
const [show, setShow] = React.useState(false)
54+
return (
55+
<>
56+
<AriaAlert>{show ? 'Failed to export data!' : null}</AriaAlert>
57+
<button
58+
type="button"
59+
onClick={() => {
60+
setShow(true)
61+
}}
62+
>
63+
Export data
64+
</button>
65+
</>
66+
)
67+
}
68+
const user = userEvent.setup()
69+
70+
render(<TestComponent />)
71+
72+
const liveRegion = getLiveRegion()
73+
expect(liveRegion.getMessage('assertive')).toBe('')
74+
75+
await user.click(screen.getByText('Export data'))
76+
expect(liveRegion.getMessage('assertive')).toBe('Failed to export data!')
77+
})
4978
})

‎packages/react/src/live-region/__tests__/AriaStatus.test.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,32 @@ describe('AriaStatus', () => {
8383
)
8484
expect(screen.getByTestId('container').tagName).toBe('SPAN')
8585
})
86+
87+
it('should update live-region element when AriaStatus goes from empty to populated', async () => {
88+
function TestComponent() {
89+
const [show, setShow] = React.useState(false)
90+
return (
91+
<>
92+
<AriaStatus>{show ? 'Export completed' : null}</AriaStatus>
93+
<button
94+
type="button"
95+
onClick={() => {
96+
setShow(true)
97+
}}
98+
>
99+
Export data
100+
</button>
101+
</>
102+
)
103+
}
104+
const user = userEvent.setup()
105+
106+
render(<TestComponent />)
107+
108+
const liveRegion = getLiveRegion()
109+
expect(liveRegion.getMessage('polite')).toBe('')
110+
111+
await user.click(screen.getByText('Export data'))
112+
expect(liveRegion.getMessage('polite')).toBe('Export completed')
113+
})
86114
})

0 commit comments

Comments
 (0)
Please sign in to comment.