Skip to content

Commit b3ea687

Browse files
committed
assert empty aria region
1 parent ab795fb commit b3ea687

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/clerk-js/src/ui/elements/__tests__/PlainInput.test.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('PlainInput', () => {
139139

140140
await userEvent.click(getByRole('button', { name: /set error/i }));
141141

142-
expect(await findByText(/Some Error/i)).toBeInTheDocument();
142+
expect(await findByText(/Some Error/i, { selector: '#error-firstname' })).toBeInTheDocument();
143143

144144
const input = getByLabelText(/some label/i);
145145
expect(input).toHaveAttribute('aria-invalid', 'true');
@@ -163,7 +163,7 @@ describe('PlainInput', () => {
163163
const { findByLabelText, findByText } = render(<Field actionLabel={'take action'} />, { wrapper });
164164

165165
fireEvent.focus(await findByLabelText(/some label/i));
166-
expect(await findByText(/some info/i)).toBeInTheDocument();
166+
expect(await findByText(/some info/i, { selector: '#firstname-info-feedback' })).toBeInTheDocument();
167167
});
168168

169169
it('with success feedback and aria-describedby', async () => {
@@ -178,7 +178,7 @@ describe('PlainInput', () => {
178178

179179
await userEvent.click(getByRole('button', { name: /set success/i }));
180180

181-
expect(await findByText(/Some Success/i)).toBeInTheDocument();
181+
expect(await findByText(/Some Success/i, { selector: '#firstname-success-feedback' })).toBeInTheDocument();
182182

183183
const input = getByLabelText(/some label/i);
184184
expect(input).toHaveAttribute('aria-invalid', 'false');
@@ -202,15 +202,15 @@ describe('PlainInput', () => {
202202

203203
// Start with error
204204
await userEvent.click(getByRole('button', { name: /set error/i }));
205-
expect(await findByText(/Some Error/i)).toBeInTheDocument();
205+
expect(await findByText(/Some Error/i, { selector: '#error-firstname' })).toBeInTheDocument();
206206

207207
let input = getByLabelText(/some label/i);
208208
expect(input).toHaveAttribute('aria-invalid', 'true');
209209
expect(input).toHaveAttribute('aria-describedby', 'error-firstname');
210210

211211
// Transition to success
212212
await userEvent.click(getByRole('button', { name: /set success/i }));
213-
expect(await findByText(/Some Success/i)).toBeInTheDocument();
213+
expect(await findByText(/Some Success/i, { selector: '#firstname-success-feedback' })).toBeInTheDocument();
214214

215215
input = getByLabelText(/some label/i);
216216
expect(input).toHaveAttribute('aria-invalid', 'false');
@@ -232,9 +232,24 @@ describe('PlainInput', () => {
232232

233233
const { getByRole, findByText, container } = render(<Field />, { wrapper });
234234

235+
// Pre-state: aria-live region exists and is empty
236+
const preRegions = container.querySelectorAll('[aria-live="polite"]');
237+
expect(preRegions.length).toBeGreaterThanOrEqual(1);
238+
const preSrOnly = Array.from(preRegions).find(el => {
239+
const style = window.getComputedStyle(el);
240+
return style.position === 'absolute' && style.width === '1px';
241+
});
242+
expect(preSrOnly).toBeDefined();
243+
expect(preSrOnly?.textContent ?? '').toMatch(/^\s*$/);
244+
245+
// Input is not in error and not described yet
246+
const inputEl = container.querySelector('input#firstname-field');
247+
expect(inputEl).toHaveAttribute('aria-invalid', 'false');
248+
expect(inputEl).not.toHaveAttribute('aria-describedby');
249+
235250
// Set error feedback
236251
await userEvent.click(getByRole('button', { name: /set error/i }));
237-
expect(await findByText(/Some Error/i)).toBeInTheDocument();
252+
expect(await findByText(/Some Error/i, { selector: '#error-firstname' })).toBeInTheDocument();
238253

239254
// Verify there's a screen-reader-only aria-live region with the error content
240255
const ariaLiveRegions = container.querySelectorAll('[aria-live="polite"]');
@@ -250,7 +265,7 @@ describe('PlainInput', () => {
250265

251266
// Transition to success
252267
await userEvent.click(getByRole('button', { name: /set success/i }));
253-
expect(await findByText(/Some Success/i)).toBeInTheDocument();
268+
expect(await findByText(/Some Success/i, { selector: '#firstname-success-feedback' })).toBeInTheDocument();
254269

255270
// Verify the screen reader only region updated its content
256271
expect(srOnlyRegion).toHaveTextContent(/Some Success/i);

0 commit comments

Comments
 (0)