Skip to content

Commit

Permalink
test: add test cases for role/aria-live removals
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Sep 6, 2021
1 parent 8c6e002 commit 098a731
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/capture-announcements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,8 @@ function trimWhiteSpace(text: string): string | null {
const trimmed = text.trim().replace(WHITE_SPACE_REGEXP, ' ');
return trimmed.length > 0 ? trimmed : null;
}

/** Not part of public API, do not use */
export const __PrivateUnstableAPI = {
liveRegions,
};
45 changes: 45 additions & 0 deletions test/capture-announcements.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import CaptureAnnouncements from '../src';
import { __PrivateUnstableAPI } from '../src/capture-announcements';
import { appendToRoot, POLITE_CASES, ASSERTIVE_CASES } from './utils';

const { liveRegions } = __PrivateUnstableAPI;

POLITE_CASES.forEach(({ name, value, tag }) => {
const testName = name && value ? `[${name}="${value}"]` : `<${tag}>`;

Expand Down Expand Up @@ -372,3 +375,45 @@ describe('common', () => {
expect(onIncorrectStatusMessage).not.toHaveBeenCalled();
});
});

describe('element tracking', () => {
let element: HTMLElement;
let cleanup: undefined | ReturnType<typeof CaptureAnnouncements>;

afterEach(() => {
cleanup?.();
});

beforeEach(() => {
cleanup = CaptureAnnouncements({ onCapture: jest.fn() });
element = document.createElement('div');
});

test('element is removed from tracked elements when aria-live is removed', () => {
element.setAttribute('aria-live', 'polite');
appendToRoot(element);
element.textContent = 'Hello world';

expect(liveRegions.size).toBe(1);
expect(liveRegions.has(element)).toBe(true);

element.removeAttribute('aria-live');

expect(liveRegions.size).toBe(0);
expect(liveRegions.has(element)).toBe(false);
});

test('element is removed from tracked elements when role is removed', () => {
element.setAttribute('role', 'status');
appendToRoot(element);
element.textContent = 'Hello world';

expect(liveRegions.size).toBe(1);
expect(liveRegions.has(element)).toBe(true);

element.removeAttribute('role');

expect(liveRegions.size).toBe(0);
expect(liveRegions.has(element)).toBe(false);
});
});

0 comments on commit 098a731

Please sign in to comment.