-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
overlay.spec.ts
579 lines (436 loc) · 18.7 KB
/
overlay.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import {async, ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core';
import {
ComponentPortal,
PortalModule,
TemplatePortal,
TemplatePortalDirective
} from '@angular/cdk/portal';
import {
Overlay,
OverlayContainer,
OverlayModule,
OverlayRef,
OverlayState,
PositionStrategy,
ScrollStrategy,
} from './index';
describe('Overlay', () => {
let overlay: Overlay;
let componentPortal: ComponentPortal<PizzaMsg>;
let templatePortal: TemplatePortal<any>;
let overlayContainerElement: HTMLElement;
let viewContainerFixture: ComponentFixture<TestComponentWithTemplatePortals>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [OverlayModule, PortalModule, OverlayTestModule],
providers: [{
provide: OverlayContainer,
useFactory: () => {
overlayContainerElement = document.createElement('div');
return {getContainerElement: () => overlayContainerElement};
}
}]
}).compileComponents();
}));
beforeEach(inject([Overlay], (o: Overlay) => {
overlay = o;
let fixture = TestBed.createComponent(TestComponentWithTemplatePortals);
fixture.detectChanges();
templatePortal = fixture.componentInstance.templatePortal;
componentPortal = new ComponentPortal(PizzaMsg, fixture.componentInstance.viewContainerRef);
viewContainerFixture = fixture;
}));
it('should load a component into an overlay', () => {
let overlayRef = overlay.create();
overlayRef.attach(componentPortal);
expect(overlayContainerElement.textContent).toContain('Pizza');
overlayRef.dispose();
expect(overlayContainerElement.childNodes.length).toBe(0);
expect(overlayContainerElement.textContent).toBe('');
});
it('should load a template portal into an overlay', () => {
let overlayRef = overlay.create();
overlayRef.attach(templatePortal);
expect(overlayContainerElement.textContent).toContain('Cake');
overlayRef.dispose();
expect(overlayContainerElement.childNodes.length).toBe(0);
expect(overlayContainerElement.textContent).toBe('');
});
it('should disable pointer events of the pane element if detached', () => {
let overlayRef = overlay.create();
let paneElement = overlayRef.overlayElement;
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
expect(paneElement.childNodes.length).not.toBe(0);
expect(paneElement.style.pointerEvents)
.toBe('auto', 'Expected the overlay pane to enable pointerEvents when attached.');
overlayRef.detach();
expect(paneElement.childNodes.length).toBe(0);
expect(paneElement.style.pointerEvents)
.toBe('none', 'Expected the overlay pane to disable pointerEvents when detached.');
});
it('should open multiple overlays', () => {
let pizzaOverlayRef = overlay.create();
pizzaOverlayRef.attach(componentPortal);
let cakeOverlayRef = overlay.create();
cakeOverlayRef.attach(templatePortal);
expect(overlayContainerElement.childNodes.length).toBe(2);
expect(overlayContainerElement.textContent).toContain('Pizza');
expect(overlayContainerElement.textContent).toContain('Cake');
pizzaOverlayRef.dispose();
expect(overlayContainerElement.childNodes.length).toBe(1);
expect(overlayContainerElement.textContent).toContain('Cake');
cakeOverlayRef.dispose();
expect(overlayContainerElement.childNodes.length).toBe(0);
expect(overlayContainerElement.textContent).toBe('');
});
it('should ensure that the most-recently-attached overlay is on top', () => {
let pizzaOverlayRef = overlay.create();
let cakeOverlayRef = overlay.create();
pizzaOverlayRef.attach(componentPortal);
cakeOverlayRef.attach(templatePortal);
expect(pizzaOverlayRef.overlayElement.nextSibling)
.toBeTruthy('Expected pizza to be on the bottom.');
expect(cakeOverlayRef.overlayElement.nextSibling)
.toBeFalsy('Expected cake to be on top.');
pizzaOverlayRef.dispose();
cakeOverlayRef.detach();
pizzaOverlayRef = overlay.create();
pizzaOverlayRef.attach(componentPortal);
cakeOverlayRef.attach(templatePortal);
expect(pizzaOverlayRef.overlayElement.nextSibling)
.toBeTruthy('Expected pizza to still be on the bottom.');
expect(cakeOverlayRef.overlayElement.nextSibling)
.toBeFalsy('Expected cake to still be on top.');
});
it('should set the direction', () => {
const state = new OverlayState();
state.direction = 'rtl';
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.getAttribute('dir')).toEqual('rtl');
});
it('should emit when an overlay is attached', () => {
let overlayRef = overlay.create();
let spy = jasmine.createSpy('attachments spy');
overlayRef.attachments().subscribe(spy);
overlayRef.attach(componentPortal);
expect(spy).toHaveBeenCalled();
});
it('should emit the attachment event after everything is added to the DOM', () => {
let state = new OverlayState();
state.hasBackdrop = true;
let overlayRef = overlay.create(state);
overlayRef.attachments().subscribe(() => {
expect(overlayContainerElement.querySelector('pizza'))
.toBeTruthy('Expected the overlay to have been attached.');
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop'))
.toBeTruthy('Expected the backdrop to have been attached.');
});
overlayRef.attach(componentPortal);
});
it('should emit when an overlay is detached', () => {
let overlayRef = overlay.create();
let spy = jasmine.createSpy('detachments spy');
overlayRef.detachments().subscribe(spy);
overlayRef.attach(componentPortal);
overlayRef.detach();
expect(spy).toHaveBeenCalled();
});
it('should emit the detachment event after the overlay is removed from the DOM', () => {
let overlayRef = overlay.create();
overlayRef.detachments().subscribe(() => {
expect(overlayContainerElement.querySelector('pizza'))
.toBeFalsy('Expected the overlay to have been detached.');
});
overlayRef.attach(componentPortal);
overlayRef.detach();
});
it('should emit and complete the observables when an overlay is disposed', () => {
let overlayRef = overlay.create();
let disposeSpy = jasmine.createSpy('dispose spy');
let attachCompleteSpy = jasmine.createSpy('attachCompleteSpy spy');
let detachCompleteSpy = jasmine.createSpy('detachCompleteSpy spy');
overlayRef.attachments().subscribe(undefined, undefined, attachCompleteSpy);
overlayRef.detachments().subscribe(disposeSpy, undefined, detachCompleteSpy);
overlayRef.attach(componentPortal);
overlayRef.dispose();
expect(disposeSpy).toHaveBeenCalled();
expect(attachCompleteSpy).toHaveBeenCalled();
expect(detachCompleteSpy).toHaveBeenCalled();
});
it('should complete the attachment observable before the detachment one', () => {
let overlayRef = overlay.create();
let callbackOrder: string[] = [];
overlayRef.attachments().subscribe(undefined, undefined, () => callbackOrder.push('attach'));
overlayRef.detachments().subscribe(undefined, undefined, () => callbackOrder.push('detach'));
overlayRef.attach(componentPortal);
overlayRef.dispose();
expect(callbackOrder).toEqual(['attach', 'detach']);
});
describe('positioning', () => {
let state: OverlayState;
beforeEach(() => {
state = new OverlayState();
});
it('should apply the positioning strategy', () => {
state.positionStrategy = new FakePositionStrategy();
overlay.create(state).attach(componentPortal);
expect(overlayContainerElement.querySelectorAll('.fake-positioned').length).toBe(1);
});
});
describe('size', () => {
let state: OverlayState;
beforeEach(() => {
state = new OverlayState();
});
it('should apply the width set in the config', () => {
state.width = 500;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('500px');
});
it('should support using other units if a string width is provided', () => {
state.width = '200%';
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('200%');
});
it('should apply the height set in the config', () => {
state.height = 500;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.height).toEqual('500px');
});
it('should support using other units if a string height is provided', () => {
state.height = '100vh';
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.height).toEqual('100vh');
});
it('should apply the min width set in the config', () => {
state.minWidth = 200;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.minWidth).toEqual('200px');
});
it('should apply the min height set in the config', () => {
state.minHeight = 500;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.minHeight).toEqual('500px');
});
it('should apply the max width set in the config', () => {
state.maxWidth = 200;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.maxWidth).toEqual('200px');
});
it('should apply the max height set in the config', () => {
state.maxHeight = 500;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.maxHeight).toEqual('500px');
});
it('should support zero widths and heights', () => {
state.width = 0;
state.height = 0;
overlay.create(state).attach(componentPortal);
const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('0px');
expect(pane.style.height).toEqual('0px');
});
});
describe('backdrop', () => {
let config: OverlayState;
beforeEach(() => {
config = new OverlayState();
config.hasBackdrop = true;
});
it('should create and destroy an overlay backdrop', () => {
let overlayRef = overlay.create(config);
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
expect(backdrop).toBeTruthy();
expect(backdrop.classList).not.toContain('cdk-overlay-backdrop-showing');
let backdropClickHandler = jasmine.createSpy('backdropClickHander');
overlayRef.backdropClick().subscribe(backdropClickHandler);
backdrop.click();
expect(backdropClickHandler).toHaveBeenCalled();
});
it('should complete the backdrop click stream once the overlay is destroyed', () => {
let overlayRef = overlay.create(config);
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
let completeHandler = jasmine.createSpy('backdrop complete handler');
overlayRef.backdropClick().subscribe(undefined, undefined, completeHandler);
overlayRef.dispose();
expect(completeHandler).toHaveBeenCalled();
});
it('should apply the default overlay backdrop class', () => {
let overlayRef = overlay.create(config);
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
expect(backdrop.classList).toContain('cdk-overlay-dark-backdrop');
});
it('should apply a custom overlay backdrop class', () => {
config.backdropClass = 'cdk-overlay-transparent-backdrop';
let overlayRef = overlay.create(config);
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
expect(backdrop.classList).toContain('cdk-overlay-transparent-backdrop');
});
it('should disable the pointer events of a backdrop that is being removed', () => {
let overlayRef = overlay.create(config);
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
expect(backdrop.style.pointerEvents).toBeFalsy();
overlayRef.detach();
expect(backdrop.style.pointerEvents).toBe('none');
});
it('should insert the backdrop before the overlay pane in the DOM order', () => {
let overlayRef = overlay.create(config);
overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
let pane = overlayContainerElement.querySelector('.cdk-overlay-pane');
let children = Array.prototype.slice.call(overlayContainerElement.children);
expect(children.indexOf(backdrop)).toBeGreaterThan(-1);
expect(children.indexOf(pane)).toBeGreaterThan(-1);
expect(children.indexOf(backdrop))
.toBeLessThan(children.indexOf(pane), 'Expected backdrop to be before the pane in the DOM');
});
});
describe('panelClass', () => {
it('should apply a custom overlay pane class', () => {
const config = new OverlayState();
config.panelClass = 'custom-panel-class';
overlay.create(config).attach(componentPortal);
viewContainerFixture.detectChanges();
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.classList).toContain('custom-panel-class');
});
it('should be able to apply multiple classes', () => {
const config = new OverlayState();
config.panelClass = ['custom-class-one', 'custom-class-two'];
overlay.create(config).attach(componentPortal);
viewContainerFixture.detectChanges();
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.classList).toContain('custom-class-one');
expect(pane.classList).toContain('custom-class-two');
});
});
describe('scroll strategy', () => {
let fakeScrollStrategy: FakeScrollStrategy;
let config: OverlayState;
let overlayRef: OverlayRef;
beforeEach(() => {
config = new OverlayState();
fakeScrollStrategy = config.scrollStrategy = new FakeScrollStrategy();
overlayRef = overlay.create(config);
});
it('should attach the overlay ref to the scroll strategy', () => {
expect(fakeScrollStrategy.overlayRef).toBe(overlayRef,
'Expected scroll strategy to have been attached to the current overlay ref.');
});
it('should enable the scroll strategy when the overlay is attached', () => {
overlayRef.attach(componentPortal);
expect(fakeScrollStrategy.isEnabled).toBe(true, 'Expected scroll strategy to be enabled.');
});
it('should disable the scroll strategy once the overlay is detached', () => {
overlayRef.attach(componentPortal);
expect(fakeScrollStrategy.isEnabled).toBe(true, 'Expected scroll strategy to be enabled.');
overlayRef.detach();
expect(fakeScrollStrategy.isEnabled).toBe(false, 'Expected scroll strategy to be disabled.');
});
it('should disable the scroll strategy when the overlay is destroyed', () => {
overlayRef.dispose();
expect(fakeScrollStrategy.isEnabled).toBe(false, 'Expected scroll strategy to be disabled.');
});
});
});
describe('OverlayContainer theming', () => {
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({ imports: [OverlayContainerThemingTestModule] });
TestBed.compileComponents();
}));
beforeEach(inject([OverlayContainer], (o: OverlayContainer) => {
overlayContainer = o;
overlayContainerElement = overlayContainer.getContainerElement();
}));
afterEach(() => {
overlayContainerElement.parentNode!.removeChild(overlayContainerElement);
});
it('should be able to set a theme on the overlay container', () => {
overlayContainer.themeClass = 'my-theme';
expect(overlayContainerElement.classList).toContain('my-theme');
});
it('should clear any previously-set themes when a new theme is set', () => {
overlayContainer.themeClass = 'initial-theme';
expect(overlayContainerElement.classList).toContain('initial-theme');
overlayContainer.themeClass = 'new-theme';
expect(overlayContainerElement.classList).not.toContain('initial-theme');
expect(overlayContainerElement.classList).toContain('new-theme');
});
it('should not throw when switching from a blank theme', () => {
overlayContainer.themeClass = '';
expect(() => overlayContainer.themeClass = 'new-theme').not.toThrow();
});
});
/** Simple component for testing ComponentPortal. */
@Component({
selector: 'pizza',
template: '<p>Pizza</p>'
})
class PizzaMsg { }
/** Test-bed component that contains a TempatePortal and an ElementRef. */
@Component({template: `<ng-template cdk-portal>Cake</ng-template>`})
class TestComponentWithTemplatePortals {
@ViewChild(TemplatePortalDirective) templatePortal: TemplatePortalDirective;
constructor(public viewContainerRef: ViewContainerRef) { }
}
// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
const TEST_COMPONENTS = [PizzaMsg, TestComponentWithTemplatePortals];
@NgModule({
imports: [OverlayModule, PortalModule],
exports: TEST_COMPONENTS,
declarations: TEST_COMPONENTS,
entryComponents: TEST_COMPONENTS,
})
class OverlayTestModule { }
/** Component for testing the overlay container theming. */
@NgModule({
imports: [OverlayModule, PortalModule],
})
class OverlayContainerThemingTestModule { }
class FakePositionStrategy implements PositionStrategy {
element: HTMLElement;
apply(): void {
this.element.classList.add('fake-positioned');
}
attach(overlayRef: OverlayRef) {
this.element = overlayRef.overlayElement;
}
dispose() {}
}
class FakeScrollStrategy implements ScrollStrategy {
isEnabled = false;
overlayRef: OverlayRef;
attach(overlayRef: OverlayRef) {
this.overlayRef = overlayRef;
}
enable() {
this.isEnabled = true;
}
disable() {
this.isEnabled = false;
}
}