| 1 |
import { vitest, describe, expect, it, beforeEach, afterEach, vi } from 'vitest'; |
| 2 |
import { setMedia } from 'mock-match-media'; |
| 3 |
|
| 4 |
import Swup, { Visit } from 'swup'; |
| 5 |
|
| 6 |
import SwupA11yPlugin from '../../src/index.js'; |
| 7 |
import { Announcer } from '../../src/announcements.js'; |
| 8 |
|
| 9 |
vitest.mock('../../src/announcements.js'); |
| 10 |
vitest.mock('../../src/focus.js'); |
| 11 |
|
| 12 |
describe('SwupA11yPlugin', () => { |
| 13 |
let swup: Swup; |
| 14 |
let plugin: SwupA11yPlugin; |
| 15 |
let visit: Visit; |
| 16 |
|
| 17 |
beforeEach(() => { |
| 18 |
document.body.innerHTML = '<h1>Test</h1>'; |
| 19 |
|
| 20 |
swup = new Swup(); |
| 21 |
plugin = new SwupA11yPlugin(); |
| 22 |
swup.use(plugin); |
| 23 |
|
| 24 |
// @ts-ignore - createVisit is marked internal |
| 25 |
visit = swup.createVisit({ url: '/' }); |
| 26 |
visit.to.document = new window.DOMParser().parseFromString( |
| 27 |
'<html><head></head><body></body></html>', |
| 28 |
'text/html' |
| 29 |
); |
| 30 |
}); |
| 31 |
|
| 32 |
afterEach(() => { |
| 33 |
swup.unuse(plugin); |
| 34 |
swup.destroy(); |
| 35 |
}); |
| 36 |
|
| 37 |
describe('hooks', () => { |
| 38 |
it('creates new hooks', async () => { |
| 39 |
expect(swup.hooks.exists('content:announce')).toBe(true); |
| 40 |
expect(swup.hooks.exists('content:focus')).toBe(true); |
| 41 |
}); |
| 42 |
}); |
| 43 |
|
| 44 |
describe('visit object', () => { |
| 45 |
it('adds an a11y key to the visit object', async () => { |
| 46 |
await swup.hooks.call('visit:start', visit, undefined); |
| 47 |
expect(visit).toMatchObject({ |
| 48 |
a11y: { |
| 49 |
announce: undefined, |
| 50 |
focus: 'body' |
| 51 |
} |
| 52 |
}); |
| 53 |
}); |
| 54 |
}); |
| 55 |
|
| 56 |
describe('busy', () => { |
| 57 |
it('marks page as busy during transitions', async () => { |
| 58 |
await swup.hooks.call('visit:start', visit, undefined); |
| 59 |
expect(document.documentElement.getAttribute('aria-busy')).toBe('true'); |
| 60 |
|
| 61 |
await swup.hooks.call('visit:end', visit, undefined); |
| 62 |
expect(document.documentElement.getAttribute('aria-busy')).toBeNull(); |
| 63 |
}); |
| 64 |
}); |
| 65 |
|
| 66 |
describe('reduced motion', () => { |
| 67 |
it('ignores animations when reduced motion is unset', async () => { |
| 68 |
await swup.hooks.call('visit:start', visit, undefined); |
| 69 |
expect(visit.animation).toMatchObject({ animate: true }); |
| 70 |
expect(visit.scroll).not.toHaveProperty('animate'); |
| 71 |
}); |
| 72 |
|
| 73 |
it('disables animations when reduced motion is preferred', async () => { |
| 74 |
setMedia({ 'prefers-reduced-motion': 'reduce' }); |
| 75 |
await swup.hooks.call('visit:start', visit, undefined); |
| 76 |
expect(visit.animation).toMatchObject({ animate: false }); |
| 77 |
expect(visit.scroll).toMatchObject({ animate: false }); |
| 78 |
}); |
| 79 |
|
| 80 |
it('ignores animations when reduced motion setting is disabled', async () => { |
| 81 |
setMedia({ 'prefers-reduced-motion': 'reduce' }); |
| 82 |
plugin.options.respectReducedMotion = false; |
| 83 |
await swup.hooks.call('visit:start', visit, undefined); |
| 84 |
expect(visit.animation).toMatchObject({ animate: true }); |
| 85 |
expect(visit.scroll).not.toHaveProperty('animate'); |
| 86 |
}); |
| 87 |
}); |
| 88 |
|
| 89 |
describe('content focus', async () => { |
| 90 |
let autofocusElementFound = false; |
| 91 |
const focus = await import('../../src/focus.js'); |
| 92 |
focus.focusAutofocusElement = vitest.fn().mockImplementation(() => autofocusElementFound); |
| 93 |
focus.focusElement = vitest.fn(); |
| 94 |
|
| 95 |
it('focuses element from visit:end hook', async () => { |
| 96 |
await swup.hooks.call('visit:start', visit, undefined); |
| 97 |
await swup.hooks.call('visit:end', visit, undefined); |
| 98 |
|
| 99 |
expect(focus.focusElement).toHaveBeenCalledWith(visit.a11y.focus); |
| 100 |
}); |
| 101 |
|
| 102 |
it('triggers content:focus hook from visit:end hook', async () => { |
| 103 |
let triggered = false; |
| 104 |
await swup.hooks.call('visit:start', visit, undefined); |
| 105 |
swup.hooks.on('content:focus', () => (triggered = true)); |
| 106 |
await swup.hooks.call('visit:end', visit, undefined); |
| 107 |
|
| 108 |
expect(triggered).toBe(true); |
| 109 |
}); |
| 110 |
|
| 111 |
it('focuses custom focus selector', async () => { |
| 112 |
await swup.hooks.call('visit:start', visit, undefined); |
| 113 |
visit.a11y.focus = 'main'; |
| 114 |
await swup.hooks.call('visit:end', visit, undefined); |
| 115 |
|
| 116 |
expect(focus.focusElement).toHaveBeenCalledWith('main'); |
| 117 |
}); |
| 118 |
|
| 119 |
it('ignores empty focus selector', async () => { |
| 120 |
await swup.hooks.call('visit:start', visit, undefined); |
| 121 |
visit.a11y.focus = ''; |
| 122 |
await swup.hooks.call('visit:end', visit, undefined); |
| 123 |
|
| 124 |
expect(focus.focusElement).not.toHaveBeenCalled(); |
| 125 |
}); |
| 126 |
|
| 127 |
it('does not autofocus from visit:end by default', async () => { |
| 128 |
await swup.hooks.call('visit:start', visit, undefined); |
| 129 |
await swup.hooks.call('visit:end', visit, undefined); |
| 130 |
|
| 131 |
expect(focus.focusAutofocusElement).not.toHaveBeenCalled(); |
| 132 |
}); |
| 133 |
|
| 134 |
it('autofocuses from visit:end hook if configured', async () => { |
| 135 |
plugin.options.autofocus = true; |
| 136 |
await swup.hooks.call('visit:start', visit, undefined); |
| 137 |
await swup.hooks.call('visit:end', visit, undefined); |
| 138 |
|
| 139 |
expect(focus.focusAutofocusElement).toHaveBeenCalled(); |
| 140 |
expect(focus.focusElement).toHaveBeenCalled(); |
| 141 |
}); |
| 142 |
|
| 143 |
it('skips normal focus if autofocus element was found', async () => { |
| 144 |
plugin.options.autofocus = true; |
| 145 |
autofocusElementFound = true; |
| 146 |
await swup.hooks.call('visit:start', visit, undefined); |
| 147 |
await swup.hooks.call('visit:end', visit, undefined); |
| 148 |
|
| 149 |
expect(focus.focusAutofocusElement).toHaveBeenCalled(); |
| 150 |
expect(focus.focusElement).not.toHaveBeenCalled(); |
| 151 |
}); |
| 152 |
}); |
| 153 |
|
| 154 |
describe('announcements', async () => { |
| 155 |
let announcerMock; |
| 156 |
const announcements = await import('../../src/announcements.js'); |
| 157 |
announcements.getPageAnnouncement = vitest.fn().mockImplementation(() => 'Hello'); |
| 158 |
|
| 159 |
beforeEach(() => { |
| 160 |
announcerMock = vi.spyOn(plugin.announcer, 'announce'); |
| 161 |
}); |
| 162 |
|
| 163 |
it('creates an announcer instance', async () => { |
| 164 |
expect(plugin.announcer).toBeInstanceOf(Announcer); |
| 165 |
}); |
| 166 |
|
| 167 |
it('adds an announce method to swup', async () => { |
| 168 |
expect(swup.announce).toBeInstanceOf(Function); |
| 169 |
await swup.announce!('Hello'); |
| 170 |
expect(announcerMock).toHaveBeenCalledWith('Hello'); |
| 171 |
}); |
| 172 |
|
| 173 |
it('announces content from visit:end hook', async () => { |
| 174 |
await swup.hooks.call('visit:start', visit, undefined); |
| 175 |
await swup.hooks.call('visit:end', visit, undefined); |
| 176 |
|
| 177 |
expect(announcerMock).toHaveBeenCalledWith( |
| 178 |
visit.a11y.announce, |
| 179 |
plugin.announcementDelay |
| 180 |
); |
| 181 |
}); |
| 182 |
|
| 183 |
it('triggers content:announce hook from visit:end hook', async () => { |
| 184 |
let triggered = false; |
| 185 |
await swup.hooks.call('visit:start', visit, undefined); |
| 186 |
swup.hooks.on('content:announce', () => (triggered = true)); |
| 187 |
await swup.hooks.call('visit:end', visit, undefined); |
| 188 |
|
| 189 |
expect(triggered).toBe(true); |
| 190 |
}); |
| 191 |
|
| 192 |
it('fills announcement with current page title', async () => { |
| 193 |
await swup.hooks.call('visit:start', visit, undefined); |
| 194 |
await swup.hooks.call('visit:end', visit, undefined); |
| 195 |
|
| 196 |
expect(announcements.getPageAnnouncement).toHaveBeenCalledWith({ |
| 197 |
headingSelector: plugin.options.headingSelector, |
| 198 |
announcements: plugin.options.announcements |
| 199 |
}); |
| 200 |
}); |
| 201 |
|
| 202 |
it('announces custom announcement string', async () => { |
| 203 |
await swup.hooks.call('visit:start', visit, undefined); |
| 204 |
visit.a11y.announce = 'Custom Message'; |
| 205 |
await swup.hooks.call('visit:end', visit, undefined); |
| 206 |
|
| 207 |
expect(announcerMock).toHaveBeenCalledWith('Custom Message', plugin.announcementDelay); |
| 208 |
}); |
| 209 |
|
| 210 |
it('ignores empty announcement string', async () => { |
| 211 |
await swup.hooks.call('visit:start', visit, undefined); |
| 212 |
visit.a11y.announce = ''; |
| 213 |
await swup.hooks.call('visit:end', visit, undefined); |
| 214 |
|
| 215 |
expect(announcerMock).not.toHaveBeenCalled(); |
| 216 |
}); |
| 217 |
}); |
| 218 |
|
| 219 |
describe('anchor focus', async () => { |
| 220 |
const focus = await import('../../src/focus.js'); |
| 221 |
focus.focusElement = vitest.fn(); |
| 222 |
|
| 223 |
it('focuses anchor target from scroll:anchor hook', async () => { |
| 224 |
document.body.innerHTML = '<a href="#target"></a><div id="target"></div>'; |
| 225 |
await swup.hooks.call('scroll:anchor', visit, { hash: '#target', options: {} }); |
| 226 |
|
| 227 |
expect(focus.focusElement).toHaveBeenCalledWith(expect.any(HTMLDivElement)); |
| 228 |
}); |
| 229 |
}); |
| 230 |
}); |