forked from material-components/material-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog_test.ts
233 lines (207 loc) · 8.35 KB
/
dialog_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
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {html} from 'lit';
import {Environment} from '../testing/environment.js';
import {createTokenTests} from '../testing/tokens.js';
import {MdDialog} from './dialog.js';
import {DialogHarness} from './harness.js';
function isDocumentScrollingDisabled() {
return getComputedStyle(document.body).overflow === 'hidden';
}
interface DialogTestProps {
modeless?: boolean;
}
function getDialogTemplate(props?: DialogTestProps) {
return html`
<md-dialog
.modeless=${props?.modeless ?? false}
style="
--md-dialog-opening-transition-duration: 0ms;
--md-dialog-closing-transition-duration: 0ms;
">
<div class="content">Content
<input dialogFocus>
</div>
<button slot="footer" dialogAction="button">Close</button>
</md-dialog>`;
}
describe('<md-dialog>', () => {
const realTimeout = globalThis.setTimeout;
const env = new Environment();
function setClockEnabled(enable = false) {
const isEnabled = globalThis.setTimeout !== realTimeout;
if (isEnabled !== enable) {
if (enable) {
jasmine.clock().install();
} else {
jasmine.clock().uninstall();
}
}
}
async function setupTest(
props?: DialogTestProps, template = getDialogTemplate) {
const root = env.render(template(props));
await env.waitForStability();
setClockEnabled(false);
const dialog = root.querySelector<MdDialog>('md-dialog')!;
const harness = new DialogHarness(dialog);
const contentElement = root.querySelector<HTMLElement>('.content')!;
const focusElement = root.querySelector<HTMLElement>('[dialogFocus]')!;
return {harness, root, contentElement, focusElement};
}
afterEach(() => {
setClockEnabled(true);
});
describe('.styles', () => {
createTokenTests(MdDialog.styles);
});
describe('basic', () => {
it('initializes as an md-dialog', async () => {
const {harness} = await setupTest();
expect(harness.element).toBeInstanceOf(MdDialog);
expect(await harness.getInteractiveElement())
.toBeInstanceOf(HTMLDialogElement);
});
it('renders open state by setting open property', async () => {
const {harness} = await setupTest();
expect(await harness.isDialogVisible()).toBeFalse();
harness.element.open = true;
expect(await harness.isDialogVisible()).toBeTrue();
harness.element.open = false;
expect(await harness.isDialogVisible()).toBeFalse();
harness.element.open = true;
expect(await harness.isDialogVisible()).toBeTrue();
harness.element.open = false;
expect(await harness.isDialogVisible()).toBeFalse();
});
it('renders open state by calling show()/close()/toggleShow()',
async () => {
const {harness} = await setupTest();
harness.element.show();
expect(await harness.isDialogVisible()).toBeTrue();
harness.element.close();
expect(await harness.isDialogVisible()).toBeFalse();
harness.element.toggleShow();
expect(await harness.isDialogVisible()).toBeTrue();
harness.element.toggleShow();
expect(await harness.isDialogVisible()).toBeFalse();
});
it('renders scrim', async () => {
const {harness} = await setupTest();
expect(await harness.isScrimVisible()).toBeFalse();
harness.element.open = true;
expect(await harness.isScrimVisible()).toBeTrue();
harness.element.open = false;
expect(await harness.isScrimVisible()).toBeFalse();
});
it('prevents document scrolling when open', async () => {
const {harness} = await setupTest();
expect(isDocumentScrollingDisabled()).toBeFalse();
harness.element.open = true;
await harness.transitionComplete();
expect(isDocumentScrollingDisabled()).toBeTrue();
harness.element.open = false;
await harness.transitionComplete();
expect(isDocumentScrollingDisabled()).toBeFalse();
});
it('fires open/close events', async () => {
const {harness} = await setupTest();
const openingHandler = jasmine.createSpy('openingHandler');
const openedHandler = jasmine.createSpy('openedHandler');
const closingHandler = jasmine.createSpy('closingHandler');
const closedHandler = jasmine.createSpy('closedHandler');
harness.element.addEventListener('opening', openingHandler);
harness.element.addEventListener('opened', openedHandler);
harness.element.addEventListener('closing', closingHandler);
harness.element.addEventListener('closed', closedHandler);
harness.element.show();
await harness.transitionComplete();
expect(openingHandler).toHaveBeenCalledTimes(1);
expect(openedHandler).toHaveBeenCalledTimes(1);
expect(closingHandler).toHaveBeenCalledTimes(0);
expect(closedHandler).toHaveBeenCalledTimes(0);
harness.element.close('testing');
await harness.transitionComplete();
expect(openingHandler).toHaveBeenCalledTimes(1);
expect(openedHandler).toHaveBeenCalledTimes(1);
expect(closingHandler).toHaveBeenCalledTimes(1);
expect(closingHandler.calls.mostRecent().args[0].detail.action)
.toBe('testing');
expect(closedHandler).toHaveBeenCalledTimes(1);
expect(closedHandler.calls.mostRecent().args[0].detail.action)
.toBe('testing');
});
it('closes when element with action is clicked', async () => {
const {harness} = await setupTest();
harness.element.show();
await harness.transitionComplete();
const closedHandler = jasmine.createSpy('closedHandler');
harness.element.addEventListener('closed', closedHandler);
harness.element
.querySelector<HTMLButtonElement>('[dialogAction="button"]')!.click();
await harness.transitionComplete();
expect(harness.element.open).toBeFalse();
expect(closedHandler.calls.mostRecent().args[0].detail.action)
.toBe('button');
});
it('closes with click outside dialog', async () => {
const {harness, contentElement} = await setupTest();
harness.element.show();
contentElement.click();
await harness.transitionComplete();
expect(harness.element.open).toBeTrue();
const dialogElement = await harness.getInteractiveElement();
dialogElement.click();
await harness.transitionComplete();
expect(harness.element.open).toBeFalse();
});
it('focses element with focus attribute when shown and previously focused element when closed',
async () => {
const {harness, focusElement} = await setupTest();
const button = document.createElement('button');
document.body.append(button);
button.focus();
expect(document.activeElement).toBe(button);
harness.element.show();
await harness.transitionComplete();
expect(document.activeElement).toBe(focusElement);
harness.element.close();
await harness.transitionComplete();
expect(document.activeElement).toBe(button);
button.remove();
});
describe('modeless', () => {
it('does not render srcrim', async () => {
const {harness} = await setupTest({modeless: true});
expect(await harness.isScrimVisible()).toBeFalse();
harness.element.open = true;
expect(await harness.isScrimVisible()).toBeFalse();
harness.element.open = false;
expect(await harness.isScrimVisible()).toBeFalse();
});
it('does not close on external click', async () => {
const {harness} = await setupTest({modeless: true});
harness.element.show();
const dialogElement = await harness.getInteractiveElement();
dialogElement.click();
await harness.transitionComplete();
expect(harness.element.open).toBeTrue();
harness.element.close();
await harness.transitionComplete();
});
it('does not prevent document scrolling', async () => {
const {harness} = await setupTest({modeless: true});
expect(isDocumentScrollingDisabled()).toBeFalse();
harness.element.open = true;
await harness.transitionComplete();
expect(isDocumentScrollingDisabled()).toBeFalse();
harness.element.open = false;
await harness.transitionComplete();
expect(isDocumentScrollingDisabled()).toBeFalse();
});
});
});
});