Debian Codegraph @

a11y-plugin/tests/unit/announcements.test.ts

raw
1 import { describe, expect, it, vi } from 'vitest';
2 import {
3 Announcer,
4 PageAnnouncementOptions,
5 getPageAnnouncement
6 } from '../../src/announcements.js';
7
8 describe('announcer', () => {
9 describe('live region', () => {
10 it('creates an empty live region', () => {
11 const announcer = new Announcer();
12 const region = announcer.region;
13
14 expect(region).toBeInstanceOf(HTMLElement);
15 expect(region.id).toBe('swup-announcer');
16 expect(region.getAttribute('aria-live')).toBe('assertive');
17 expect(region.parentNode).toBe(document.body);
18 expect(region.textContent).toBe('');
19 });
20
21 it('creates an invisible live region', () => {
22 const announcer = new Announcer();
23 const region = announcer.region;
24
25 expect(region.style.position).toBe('absolute');
26 expect(region.style.top).toBe('0px');
27 expect(region.style.left).toBe('0px');
28 });
29
30 it('reuses existing live region', () => {
31 const existingRegion = document.createElement('div');
32 existingRegion.id = 'swup-announcer';
33 document.body.appendChild(existingRegion);
34
35 const announcer = new Announcer();
36 expect(announcer.region).toBe(existingRegion);
37 });
38 });
39
40 describe('announce', () => {
41 it('returns a promise', () => {
42 const announcer = new Announcer();
43 expect(announcer.announce('Hello')).toBeInstanceOf(Promise);
44 });
45
46 it('adds the announcement to the live region', async () => {
47 const announcer = new Announcer();
48 await announcer.announce('Hello');
49 expect(announcer.region.textContent).toBe('Hello');
50 });
51
52 it('modifies each announcement to make it unique', async () => {
53 const announcer = new Announcer();
54 const region = announcer.region;
55
56 await announcer.announce('Hello');
57 expect(region.textContent).toBe('Hello');
58 await announcer.announce('Hello');
59 expect(region.textContent).toBe('Hello.');
60 await announcer.announce('Hello');
61 expect(region.textContent).toBe('Hello');
62 });
63 });
64 });
65
66 describe('getPageAnnouncement', () => {
67 const announcements = {
68 visit: 'Loaded {title}',
69 url: 'page at {url}'
70 };
71
72 const multiLangAnnouncements = {
73 'en': {
74 visit: 'Loaded {title}',
75 url: 'page at {url}'
76 },
77 'de': {
78 visit: '{title} geladen',
79 url: 'Seite unter {url}'
80 },
81 '*': {
82 visit: '{title}',
83 url: '{url}'
84 }
85 };
86
87 const defaults: PageAnnouncementOptions = { headingSelector: 'h1', announcements };
88
89 describe('headings', () => {
90 it('gets heading title', () => {
91 document.body.innerHTML = '<h1>Title</h1>';
92 const announcement = getPageAnnouncement(defaults);
93 expect(announcement).toBe('Loaded Title');
94 });
95
96 it('prefers heading label', () => {
97 document.body.innerHTML = '<h1 aria-label="Label">Title</h1>';
98 const announcement = getPageAnnouncement(defaults);
99 expect(announcement).toBe('Loaded Label');
100 });
101
102 it('uses heading selector', () => {
103 document.body.innerHTML = '<h2>Section</h2><h1>Title</h1>';
104 const announcement = getPageAnnouncement(defaults);
105 expect(announcement).toBe('Loaded Title');
106 });
107
108 it('makes heading selector configurable', () => {
109 document.body.innerHTML = '<h1>Title</h1><h2>Section</h2>';
110 const announcement = getPageAnnouncement({ ...defaults, headingSelector: 'h2' });
111 expect(announcement).toBe('Loaded Section');
112 });
113 });
114
115 describe('fallbacks', () => {
116 it('uses document title if no heading is found', () => {
117 document.title = 'Document';
118 document.body.innerHTML = '';
119 const announcement = getPageAnnouncement(defaults);
120 expect(announcement).toBe('Loaded Document');
121 });
122
123 it('uses url if no title is found', () => {
124 document.body.innerHTML = '';
125 document.title = '';
126 const announcement = getPageAnnouncement(defaults);
127 expect(announcement).toBe('Loaded page at /');
128 });
129
130 it('warns about missing heading', () => {
131 const warnMock = vi.spyOn(console, 'warn');
132 document.body.innerHTML = '';
133 getPageAnnouncement(defaults);
134 expect(warnMock).toHaveBeenCalledWith(
135 expect.stringContaining('No main heading (h1) found on new page')
136 );
137 });
138 });
139
140 describe('multi-language', () => {
141 const multiLangDefaults: PageAnnouncementOptions = {
142 headingSelector: 'h1',
143 announcements: multiLangAnnouncements
144 };
145
146 it('uses the current language for announcements', () => {
147 document.documentElement.lang = 'de';
148 document.body.innerHTML = '<h1>Page</h1>';
149 const announcement = getPageAnnouncement(multiLangDefaults);
150 expect(announcement).toBe('Page geladen');
151 });
152
153 it('falls back to default language', () => {
154 document.documentElement.lang = 'it';
155 document.body.innerHTML = '<h1>Page</h1>';
156 const announcement = getPageAnnouncement(multiLangDefaults);
157 expect(announcement).toBe('Page');
158 });
159
160 it('matches the language strictly', () => {
161 document.documentElement.lang = 'de-DE';
162 document.body.innerHTML = '<h1>Page</h1>';
163 const announcement = getPageAnnouncement(multiLangDefaults);
164 expect(announcement).toBe('Page');
165 });
166 });
167 });