-
Notifications
You must be signed in to change notification settings - Fork 78
/
toast.spec.ts
336 lines (266 loc) · 9.73 KB
/
toast.spec.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* @license
* Copyright 2019 Dynatrace LLC
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// tslint:disable no-lifecycle-call no-use-before-declare no-magic-numbers
// tslint:disable no-any max-file-line-count no-unbound-method use-component-selector
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component } from '@angular/core';
import {
ComponentFixture,
TestBed,
fakeAsync,
flush,
flushMicrotasks,
inject,
tick,
} from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import {
DT_TOAST_CHAR_LIMIT,
DT_TOAST_FADE_TIME,
DT_TOAST_MIN_DURATION,
DtToast,
DtToastModule,
} from '@dynatrace/barista-components/toast';
import {
createComponent,
dispatchFakeEvent,
} from '@dynatrace/barista-components/testing';
describe('DtToast', () => {
let dtToast: DtToast;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let fixture: ComponentFixture<TestComponent>;
const simpleMessage = 'Your changes have been saved!';
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [DtToastModule, NoopAnimationsModule],
declarations: [TestComponent],
}).compileComponents();
}));
beforeEach(inject(
[DtToast, OverlayContainer],
(toast: DtToast, oc: OverlayContainer) => {
dtToast = toast;
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
},
));
beforeEach(fakeAsync(() => {
fixture = createComponent(TestComponent);
fixture.detectChanges();
}));
afterEach(() => {
overlayContainer.ngOnDestroy();
});
it('should have the role of alert', () => {
dtToast.create(simpleMessage);
const containerElement = overlayContainerElement.querySelector(
'.dt-toast-container',
)!;
// Expected toast container to have role="alert"
expect(containerElement.getAttribute('role')).toBe('alert');
});
it('should open a simple message', () => {
dtToast.create(simpleMessage);
fixture.detectChanges();
const messageElement = overlayContainerElement.querySelector(
'.dt-toast-container',
)!;
expect(messageElement.textContent).toContain(simpleMessage);
});
it('should not create a toast with empty message', () => {
dtToast.create('');
fixture.detectChanges();
const messageElement = overlayContainerElement.querySelector(
'.dt-toast-container',
)!;
expect(messageElement).toBeNull();
});
it('should cut of the message if it exceeds the limit and limit duration', fakeAsync(() => {
const longMsg = new Array(DT_TOAST_CHAR_LIMIT + 10).map(() => '.').join();
const toastRef = dtToast.create(longMsg);
const afterDismissSpy = jest.fn();
toastRef!.afterDismissed().subscribe(afterDismissSpy);
fixture.detectChanges();
const messageElement = overlayContainerElement.querySelector(
'.dt-toast-container',
)!;
expect(messageElement.textContent!.length).toBe(DT_TOAST_CHAR_LIMIT);
tick(toastRef!.duration / 2);
expect(afterDismissSpy).not.toHaveBeenCalled();
tick(toastRef!.duration / 2);
fixture.detectChanges();
expect(afterDismissSpy).toHaveBeenCalled();
}));
it('should dismiss the toast and remove itself from the view', fakeAsync(() => {
const dismissCompleteSpy = jest.fn();
const toastRef = dtToast.create(simpleMessage);
fixture.detectChanges();
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
toastRef!
.afterDismissed()
.subscribe(() => {}, () => {}, dismissCompleteSpy);
toastRef!.dismiss();
fixture.detectChanges();
flush();
expect(dismissCompleteSpy).toHaveBeenCalled();
expect(overlayContainerElement.childElementCount).toBe(0);
}));
it('should be able to get dismissed through the service', fakeAsync(() => {
dtToast.create(simpleMessage);
fixture.detectChanges();
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
dtToast.dismiss();
fixture.detectChanges();
flush();
expect(overlayContainerElement.childElementCount).toBe(0);
}));
it('should set the animation state to enter on entry', () => {
const toastRef = dtToast.create(simpleMessage);
fixture.detectChanges();
expect(toastRef!.containerInstance._animationState).toBe('enter');
toastRef!.dismiss();
fixture.detectChanges();
expect(toastRef!.containerInstance._animationState).toBe('exit');
});
it('should set the animation state to complete on exit', () => {
const toastRef = dtToast.create(simpleMessage);
toastRef!.dismiss();
fixture.detectChanges();
expect(toastRef!.containerInstance._animationState).toBe('exit');
});
it(`should set the old toast animation state to exit and the new toast animation
state to enter on entry of new toast`, fakeAsync(() => {
const toastRef = dtToast.create(simpleMessage);
const dismissCompleteSpy = jest.fn();
fixture.detectChanges();
expect(toastRef!.containerInstance._animationState).toBe('enter');
const toastRef2 = dtToast.create(simpleMessage);
fixture.detectChanges();
toastRef!
.afterDismissed()
.subscribe(() => {}, () => {}, dismissCompleteSpy);
tick(DT_TOAST_FADE_TIME);
expect(toastRef2!.containerInstance._animationState).toBe('enter');
expect(dismissCompleteSpy).toHaveBeenCalled();
expect(toastRef!.containerInstance._animationState).toBe('exit');
tick(DT_TOAST_MIN_DURATION);
}));
it('should open a new toast after dismissing a previous toast', fakeAsync(() => {
let toastRef = dtToast.create(simpleMessage);
fixture.detectChanges();
toastRef!.dismiss();
fixture.detectChanges();
// Wait for the toast dismiss animation to finish.
flush();
toastRef = dtToast.create('test');
fixture.detectChanges();
// Wait for the toast open animation to finish.
tick(DT_TOAST_FADE_TIME);
expect(toastRef!.containerInstance._animationState).toBe('enter');
tick(DT_TOAST_MIN_DURATION);
}));
it('should remove past toasts when opening new toasts', fakeAsync(() => {
dtToast.create('First toast');
fixture.detectChanges();
tick();
dtToast.create('Second toast');
fixture.detectChanges();
tick();
dtToast.create('Third toast');
fixture.detectChanges();
tick();
expect(overlayContainerElement.textContent!.trim()).toBe('Third toast');
tick(DT_TOAST_MIN_DURATION);
}));
it('should remove toast if another is shown while its still animating open', fakeAsync(() => {
dtToast.create('First toast');
fixture.detectChanges();
dtToast.create('Second toast');
fixture.detectChanges();
tick();
expect(overlayContainerElement.textContent!.trim()).toBe('Second toast');
tick(DT_TOAST_MIN_DURATION);
}));
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
const toastRef = dtToast.create('short');
const afterDismissSpy = jest.fn();
toastRef!.afterDismissed().subscribe(afterDismissSpy);
fixture.detectChanges();
tick();
expect(afterDismissSpy).not.toHaveBeenCalled();
/** wait longer then the duration */
tick(toastRef!.duration * 2);
fixture.detectChanges();
tick();
expect(afterDismissSpy).toHaveBeenCalled();
}));
it('should clamp the duration to a minimum', fakeAsync(() => {
const toastRef = dtToast.create('1');
const afterDismissSpy = jest.fn();
toastRef!.afterDismissed().subscribe(afterDismissSpy);
fixture.detectChanges();
tick(DT_TOAST_MIN_DURATION / 2);
expect(afterDismissSpy).not.toHaveBeenCalled();
/** wait longer then the duration */
tick(DT_TOAST_MIN_DURATION / 2);
fixture.detectChanges();
tick();
expect(afterDismissSpy).toHaveBeenCalled();
}));
it('should clear the dismiss timeout when dismissed before timeout expiration', fakeAsync(() => {
const toastRef = dtToast.create(
'content message is very long and takes a long time',
);
setTimeout(() => {
dtToast.dismiss();
}, toastRef!.duration / 2);
tick(toastRef!.duration);
fixture.detectChanges();
tick();
expect(fixture.isStable()).toBe(true);
}));
it('should pause dismissing the toast when hovering', fakeAsync(() => {
const longMsg = new Array(DT_TOAST_CHAR_LIMIT + 10).map(() => '.').join();
const toastRef = dtToast.create(longMsg);
const afterDismissSpy = jest.fn();
toastRef!.afterDismissed().subscribe(afterDismissSpy);
fixture.detectChanges();
const messageElement = overlayContainerElement.querySelector(
'.dt-toast-container',
)!;
expect(messageElement.textContent!.length).toBe(DT_TOAST_CHAR_LIMIT);
tick(toastRef!.duration / 2);
dispatchFakeEvent(messageElement, 'mouseenter');
expect(afterDismissSpy).not.toHaveBeenCalled();
tick(toastRef!.duration);
fixture.detectChanges();
expect(afterDismissSpy).not.toHaveBeenCalled();
dispatchFakeEvent(messageElement, 'mouseleave');
fixture.detectChanges();
expect(afterDismissSpy).not.toHaveBeenCalled();
tick(toastRef!.duration / 2);
fixture.detectChanges();
flushMicrotasks();
expect(afterDismissSpy).toHaveBeenCalled();
}));
});
/** dummy component */
@Component({
selector: 'dt-test-component',
template: '',
})
class TestComponent {}