Skip to content

Commit

Permalink
feat: add getAnnouncements API
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 2, 2021
1 parent 6da067b commit 402a7eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/to-be-announced.ts
Expand Up @@ -35,6 +35,13 @@ export function clearAnnouncements(): void {
announcements.clear();
}

/**
* Get all captured announcements. Part of public API.
*/
export function getAnnouncements(): Map<string, PolitenessSetting> {
return announcements;
}

/**
* Check whether given node should trigger announcement
* - Node should be inside live region
Expand Down
24 changes: 23 additions & 1 deletion test/to-be-announced.dom.test.ts
@@ -1,5 +1,5 @@
import '../src/index';
import { clearAnnouncements } from '../src/to-be-announced';
import { clearAnnouncements, getAnnouncements } from '../src/to-be-announced';
import { appendToRoot, POLITE_CASES, ASSERTIVE_CASES } from './utils';

POLITE_CASES.forEach(({ name, value, tag }) => {
Expand Down Expand Up @@ -259,3 +259,25 @@ test('should clear announcements during test when clearAnnouncements is called',
clearAnnouncements();
expect('Second').not.toBeAnnounced();
});

test('should return all announcements with politeness setting when getAnnouncements is called', () => {
const element = document.createElement('div');
appendToRoot(element);

element.setAttribute('role', 'status');
element.textContent = 'First status message';
element.textContent = 'Second status message';

element.setAttribute('role', 'alert');
element.textContent = 'First alert message';
element.textContent = 'Second alert message';

expect(getAnnouncements()).toMatchInlineSnapshot(`
Map {
"First status message" => "polite",
"Second status message" => "polite",
"First alert message" => "assertive",
"Second alert message" => "assertive",
}
`);
});

0 comments on commit 402a7eb

Please sign in to comment.