generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
modals.test.js
268 lines (241 loc) · 9.92 KB
/
modals.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* eslint-disable no-underscore-dangle */
import { readFile, sendKeys } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { delay, waitForElement, waitForRemoval } from '../../helpers/waitfor.js';
document.body.innerHTML = await readFile({ path: './mocks/body.html' });
const {
default: init,
getModal,
getHashParams,
delayedModal,
sendAnalytics,
} = await import('../../../libs/blocks/modal/modal.js');
const satellite = { track: sinon.spy() };
describe('Modals', () => {
beforeEach(() => {
window._satellite = satellite;
window._satellite.track.called = false;
});
afterEach(() => {
sinon.restore();
});
it('Doesnt load modals on page load with no hash', async () => {
window.location.hash = '';
const modal = document.querySelector('.dialog-modal');
expect(modal).to.be.null;
});
it('Loads a modal on load with hash and closes when removed from hash', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
expect(document.getElementById('milo')).to.exist;
window.location.hash = '';
await waitForRemoval('#milo');
expect(document.getElementById('milo')).to.be.null;
});
it('Closes a modal on button click', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
const close = document.querySelector('.dialog-close');
expect(close.getAttribute('daa-ll')).to.equal('milo:modalClose:buttonClose');
close.click();
await waitForRemoval('#milo');
expect(window.location.hash).to.be.empty;
expect(document.getElementById('milo')).not.to.exist;
});
it('Closes a modal on click outside modal', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
const close = document.querySelector('.modal-curtain');
close.click();
await waitForRemoval('#milo');
expect(window.location.hash).to.be.empty;
expect(document.getElementById('milo')).not.to.exist;
});
it('Closes a modal on pressing Esc', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
await sendKeys({ press: 'Escape' });
await waitForRemoval('#milo');
expect(window.location.hash).to.be.empty;
expect(document.getElementById('milo')).not.to.exist;
});
it('Opens an inherited modal', async () => {
const meta = document.createElement('meta');
meta.name = '-otis';
meta.content = 'http://localhost:2000/test/blocks/modals/mocks/otis';
document.head.append(meta);
window.location.hash = '#otis';
await waitForElement('#otis');
expect(document.getElementById('otis')).to.exist;
window.location.hash = '';
await waitForRemoval('#otis');
expect(document.getElementById('otis')).not.to.exist;
});
it('Doesnt open a modal', async () => {
window.location.hash = '#notthere';
await delay(50);
expect(document.querySelector('.dialog-modal')).to.be.null;
window.location.hash = '';
});
it('Keeps the same modal if already open', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
getModal(document.getElementById('milo-modal-link'));
expect(document.getElementById('milo')).to.exist;
window.location.hash = '';
await waitForRemoval('#milo');
expect(document.getElementById('milo')).not.to.exist;
});
it('Gets the modal when explicitly init-ed', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
init(document.getElementById('milo-modal-link'));
expect(document.getElementById('milo')).to.exist;
window.location.hash = '';
await waitForRemoval('#milo');
expect(document.getElementById('milo')).not.to.exist;
await delay(5);
});
it('Locks focus when tabbing forward through tabbable elements', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
expect(document.getElementById('milo')).to.exist;
await delay(100);
expect(document.activeElement.getAttribute('id')).to.equal('milo-button-1');
await sendKeys({ press: 'Tab' });
expect(document.activeElement.getAttribute('id')).to.equal('milo-button-2');
await sendKeys({ press: 'Tab' });
expect(document.activeElement.classList.contains('dialog-close')).to.be.true;
await sendKeys({ press: 'Tab' });
expect(document.activeElement.getAttribute('id')).to.equal('milo-button-1');
window.location.hash = '';
await waitForRemoval('#milo');
expect(document.getElementById('milo')).not.to.exist;
await delay(5);
});
it('Locks focus when tabbing backward through tabbable elements', async () => {
window.location.hash = '#milo';
await waitForElement('#milo');
expect(document.getElementById('milo')).to.exist;
expect(document.activeElement.getAttribute('id')).to.equal('milo-button-1');
await sendKeys({ down: 'Shift' });
await sendKeys({ press: 'Tab' });
await sendKeys({ up: 'Shift' });
await delay(100);
expect(document.activeElement.classList.contains('dialog-close')).to.be.true;
await sendKeys({ down: 'Shift' });
await sendKeys({ press: 'Tab' });
await sendKeys({ up: 'Shift' });
expect(document.activeElement.getAttribute('id')).to.equal('milo-button-2');
window.location.hash = '';
await waitForRemoval('#milo');
expect(document.getElementById('milo')).not.to.exist;
});
it('Focuses on close when there are no other focusables', async () => {
const meta = document.createElement('meta');
meta.name = '-paragraph';
meta.content = 'http://localhost:2000/test/blocks/modals/mocks/paragraph';
document.head.append(meta);
window.location.hash = '#paragraph';
await waitForElement('#paragraph');
expect(document.getElementById('paragraph')).to.exist;
expect(document.activeElement.classList.contains('dialog-close')).to.be.true;
window.location.hash = '';
await waitForRemoval('#paragraph');
});
it('Focuses on a header when there are no other focusables', async () => {
const meta = document.createElement('meta');
meta.name = '-title';
meta.content = 'http://localhost:2000/test/blocks/modals/mocks/title';
document.head.append(meta);
window.location.hash = '#title';
await waitForElement('#title');
expect(document.getElementById('title')).to.exist;
expect(document.activeElement.getAttribute('id')).to.equal('test-title');
window.location.hash = '';
await waitForRemoval('#title');
});
it('does not error for a modal with a non-querySelector compliant hash', async () => {
window.location.hash = '#milo=&';
const hashChangeTriggered = new Promise((resolve) => {
window.addEventListener('hashchange', function onHashChange() {
window.removeEventListener('hashchange', onHashChange);
resolve();
});
});
window.location.hash = '';
// Test passing, means there was no error thrown
await hashChangeTriggered;
});
it('validates and returns proper hash parameters', () => {
expect(getHashParams()).to.deep.equal({});
expect(getHashParams('#delayed-modal:delay=0')).to.deep.equal({ hash: '#delayed-modal' });
expect(getHashParams('#delayed-modal:delay=1')).to.deep.equal({
delay: 1000,
hash: '#delayed-modal',
});
});
it('shows the modal with a delay, and remembers it was shown on this page', async () => {
window.sessionStorage.removeItem('shown:#delayed-modal');
const anchor = document.createElement('a');
anchor.setAttribute('data-modal-path', '/fragments/promos/fragments/cc-all-apps-promo-full-bleed-image');
anchor.setAttribute('data-modal-hash', '#delayed-modal:delay=1');
document.body.appendChild(anchor);
expect(delayedModal(anchor)).to.be.true;
await delay(1000);
const modal = await waitForElement('#delayed-modal');
expect(modal).to.be.not.null;
expect(document.querySelector('#delayed-modal').classList.contains('delayed-modal'));
expect(window.sessionStorage.getItem('shown:#delayed-modal').includes(window.location.pathname)).to.be.true;
expect(window._satellite.track.called).to.be.true;
window.sessionStorage.removeItem('shown:#delayed-modal');
modal.remove();
anchor.remove();
});
it('does not show the modal if it was shown on this page', async () => {
const el = document.createElement('a');
el.setAttribute('data-modal-hash', '#dm:delay=1');
window.sessionStorage.setItem('shown:#dm', window.location.pathname);
expect(delayedModal(el)).to.be.true;
await delay(1000);
expect(window._satellite.track.called).to.be.false;
const modal = document.querySelector('#dm');
expect(modal).to.not.exist;
window.sessionStorage.removeItem('shown:#dm');
el.remove();
});
it('restores the hash when the modal gets closed', async () => {
window.location.hash = '#category=pdf-esignatures&search=acro&types=desktop%2Cmobile';
window.location.hash = '#milo';
await waitForElement('#milo');
init(document.getElementById('milo-modal-link'));
const modal = document.getElementById('milo');
expect(modal).to.exist;
expect(window.location.hash).to.equal('#milo');
const close = document.querySelector('.dialog-close');
close.click();
expect(window.location.hash).to.equal('#category=pdf-esignatures&search=acro&types=desktop%2Cmobile');
window.location.hash = '';
});
});
describe('sendAnalytics', () => {
afterEach(() => {
sinon.restore();
});
it('satellite event not set, so must use event listener', async () => {
window._satellite = false;
sendAnalytics({});
window._satellite = satellite;
window._satellite.track.called = false;
const martechEvent = new Event('alloy_sendEvent');
dispatchEvent(martechEvent);
expect(window._satellite.track.called).to.be.true;
});
it('satellite event set, so can fire load event immediately', async () => {
window._satellite = satellite;
window._satellite.track.called = false;
sendAnalytics({});
expect(window._satellite.track.called).to.be.true;
});
});