-
Notifications
You must be signed in to change notification settings - Fork 205
/
overlay-trigger-extended.test.ts
289 lines (257 loc) · 9.35 KB
/
overlay-trigger-extended.test.ts
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { expect, html, nextFrame, oneEvent, waitUntil } from '@open-wc/testing';
import '@spectrum-web-components/overlay/overlay-trigger.js';
import { OverlayTrigger } from '@spectrum-web-components/overlay';
import '@spectrum-web-components/button/sp-button.js';
import { Button } from '@spectrum-web-components/button';
import '@spectrum-web-components/popover/sp-popover.js';
import { Popover } from '@spectrum-web-components/popover';
import '@spectrum-web-components/textfield/sp-textfield.js';
import '@spectrum-web-components/dialog/sp-dialog.js';
import { sendMouse } from '../../../test/plugins/browser.js';
import { fixture } from '../../../test/testing-helpers.js';
import { sendKeys } from '@web/test-runner-commands';
const initTest = async (
styles = html``
): Promise<{
overlayTrigger: OverlayTrigger;
button: Button;
popover: Popover;
}> => {
const test = await fixture<HTMLDivElement>(
html`
<div class="container">
<style>
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
</style>
${styles}
<overlay-trigger type="modal" id="trigger" placement="top">
<sp-button
id="outer-button"
variant="primary"
slot="trigger"
>
Show Popover
</sp-button>
<sp-popover
id="outer-popover"
slot="click-content"
direction="bottom"
tip
tabindex="0"
placement="top"
>
<sp-dialog no-divider>
This is the overlay content.
</sp-dialog>
</sp-popover>
</overlay-trigger>
</div>
`
);
await nextFrame();
await nextFrame();
await nextFrame();
await nextFrame();
await nextFrame();
await nextFrame();
return {
overlayTrigger: test.querySelector('overlay-trigger') as OverlayTrigger,
button: test.querySelector('sp-button') as Button,
popover: test.querySelector('sp-popover') as Popover,
};
};
describe('Overlay Trigger - extended', () => {
let overlayTrigger!: OverlayTrigger;
let button!: Button;
let popover!: Popover;
afterEach(async () => {
if (overlayTrigger.open) {
const closed = oneEvent(overlayTrigger, 'sp-closed');
overlayTrigger.open = undefined;
await closed;
}
});
it('manages `placement` on open', async () => {
({ overlayTrigger, button, popover } = await initTest());
expect(popover.placement).to.equal('top');
const open = oneEvent(overlayTrigger, 'sp-opened');
button.click();
await open;
expect(popover.placement).to.equal('bottom');
const close = oneEvent(overlayTrigger, 'sp-closed');
overlayTrigger.open = undefined;
await close;
expect(popover.placement).to.equal('top');
});
it('manages `placement` on scroll', async () => {
({ overlayTrigger, button, popover } = await initTest(html`
<style>
sp-button {
margin: 100vh 0;
transform: translateY(-100%);
}
</style>
`));
expect(popover.placement).to.equal('top');
const open = oneEvent(overlayTrigger, 'sp-opened');
button.click();
await open;
expect(popover.placement).to.equal('top');
button.scrollIntoView({
behavior: 'instant' as ScrollBehavior,
block: 'start',
});
await nextFrame();
await nextFrame();
await nextFrame();
await nextFrame();
expect(popover.placement).to.equal('bottom');
});
it('occludes content behind the overlay', async () => {
// currently fails most browsers in CI.
({ overlayTrigger, button, popover } = await initTest());
const textfield = document.createElement('sp-textfield');
overlayTrigger.insertAdjacentElement('afterend', textfield);
const textfieldRect = textfield.getBoundingClientRect();
expect(document.activeElement === textfield).to.be.false;
await sendMouse({
steps: [
{
type: 'click',
position: [textfieldRect.left + 5, textfieldRect.top + 5],
},
],
});
expect(
document.activeElement === textfield,
'clicking focuses the Textfield'
).to.be.true;
expect(popover.placement).to.equal('top');
const open = oneEvent(overlayTrigger, 'sp-opened');
await sendKeys({
press: 'Shift+Tab',
});
expect(document.activeElement === button, 'button focused').to.be.true;
await sendKeys({
press: 'Enter',
});
await open;
expect(overlayTrigger.type).to.equal('modal');
expect(overlayTrigger.open).to.equal('click');
expect(popover.placement).to.equal('bottom');
const close = oneEvent(overlayTrigger, 'sp-closed');
await sendMouse({
steps: [
{
type: 'click',
position: [textfieldRect.left + 5, textfieldRect.top + 5],
},
],
});
await close;
expect(overlayTrigger.open).to.be.undefined;
expect(
document.activeElement === textfield,
'closing does not focus the Textfield'
).to.be.false;
await sendMouse({
steps: [
{
type: 'click',
position: [
textfieldRect.left + textfieldRect.width / 2,
textfieldRect.top + textfieldRect.height / 2,
],
},
],
});
expect(
document.activeElement === textfield,
'the Textfield is focused again'
).to.be.true;
});
xit('occludes wheel interactions behind the overlay', async () => {
// currently fails for no reason in Firefox locally, and most browsers in CI.
({ overlayTrigger, button, popover } = await initTest());
const scrollingArea = document.createElement('div');
Object.assign(scrollingArea.style, {
width: '100px',
height: '100px',
overflow: 'auto',
});
const content = Array(100).fill(
'This is content within my box that will scroll.'
);
scrollingArea.textContent = content.join(' ');
document.body.append(scrollingArea);
await nextFrame();
const boundingRect = scrollingArea.getBoundingClientRect();
expect(scrollingArea.scrollTop).to.equal(0);
const distance = 1;
await sendMouse({
steps: [
{
type: 'move',
position: [
boundingRect.left + boundingRect.width / 2,
boundingRect.top + boundingRect.height / 2,
],
},
],
});
await sendMouse({
steps: [
{
type: 'wheel',
position: [0, distance],
},
],
});
// wait for scroll to complete
await waitUntil(
() => scrollingArea.scrollTop === distance,
`scroll went to ${distance}`
);
expect(scrollingArea.scrollTop).to.equal(distance);
expect(popover.placement).to.equal('top');
const open = oneEvent(overlayTrigger, 'sp-opened');
button.click();
await open;
expect(overlayTrigger.open).to.equal('click');
expect(popover.placement).to.equal('bottom');
expect(scrollingArea.scrollTop).to.equal(distance);
await sendMouse({
steps: [
{
type: 'wheel',
position: [0, -distance],
},
],
});
// Awaiting here points out that this always fails in Firefox
// and also was failing in WebKit without our knowing.
await nextFrame();
await nextFrame();
await nextFrame();
expect(
scrollingArea.scrollTop,
`scrollTop should be ${distance}.`
).to.equal(distance);
scrollingArea.remove();
});
});