Skip to content

Commit

Permalink
Use real timers for live region tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashbot committed Oct 18, 2024
1 parent a87d20b commit e70baf5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/internal/components/live-region/__tests__/live-region.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import InternalLiveRegion, {
assertive,
polite,
} from '../../../../../lib/components/internal/components/live-region/internal';
import { mockInnerText } from '../../../../internal/analytics/__tests__/mocks';

import styles from '../../../../../lib/components/internal/components/live-region/styles.css.js';

mockInnerText();
jest.mock<typeof import('../../../is-development')>('../../../is-development', () => ({
...jest.requireActual('../../../is-development'),
isTest: false,
}));

const renderLiveRegion = async (jsx: React.ReactElement) => {
const { container } = render(jsx);
await waitFor(() => expect(document.querySelector('[aria-live=polite]')));
jest.runAllTimers();

return {
source: container.querySelector(`.${styles.root}`),
Expand All @@ -24,9 +27,14 @@ const renderLiveRegion = async (jsx: React.ReactElement) => {
};
};

beforeEach(() => {
jest.useFakeTimers();
});

// The announcers persist throughout the lifecycle of the application.
// We need to reset them after each test.
afterEach(() => {
jest.clearAllTimers();
polite.reset();
assertive.reset();
});
Expand All @@ -43,6 +51,14 @@ describe('LiveRegion', () => {
expect(politeRegion).toHaveTextContent('Announcement');
});

it('does nothing when no message or children are provided', async () => {
const { source, politeRegion, assertiveRegion } = await renderLiveRegion(<InternalLiveRegion delay={0} />);

expect(source).toHaveTextContent('');
expect(politeRegion).toHaveTextContent('');
expect(assertiveRegion).toHaveTextContent('');
});

it('renders with a div by default', async () => {
const { source, politeRegion } = await renderLiveRegion(
<InternalLiveRegion delay={0}>Announcement</InternalLiveRegion>
Expand Down

0 comments on commit e70baf5

Please sign in to comment.