-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathdialog.spec.ts
1605 lines (1213 loc) · 51.7 KB
/
dialog.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
ComponentFixture,
fakeAsync,
flushMicrotasks,
inject,
TestBed,
tick,
flush,
} from '@angular/core/testing';
import {
ChangeDetectionStrategy,
Component,
Directive,
Inject,
Injector,
NgModule,
TemplateRef,
ViewChild,
ViewContainerRef
} from '@angular/core';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {Directionality} from '@angular/cdk/bidi';
import {MatDialogContainer} from './dialog-container';
import {OverlayContainer, ScrollStrategy, Overlay} from '@angular/cdk/overlay';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
import {A, ESCAPE} from '@angular/cdk/keycodes';
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
import {
MAT_DIALOG_DATA,
MatDialog,
MatDialogModule,
MatDialogRef,
MAT_DIALOG_DEFAULT_OPTIONS
} from './index';
import {Subject} from 'rxjs';
describe('MatDialog', () => {
let dialog: MatDialog;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let scrolledSubject = new Subject();
let testViewContainerRef: ViewContainerRef;
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
let mockLocation: SpyLocation;
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [MatDialogModule, DialogTestModule],
providers: [
{provide: Location, useClass: SpyLocation},
{provide: ScrollDispatcher, useFactory: () => ({
scrolled: () => scrolledSubject.asObservable()
})},
],
});
TestBed.compileComponents();
}));
beforeEach(inject([MatDialog, Location, OverlayContainer],
(d: MatDialog, l: Location, oc: OverlayContainer) => {
dialog = d;
mockLocation = l as SpyLocation;
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
}));
afterEach(() => {
overlayContainer.ngOnDestroy();
});
beforeEach(() => {
viewContainerFixture = TestBed.createComponent(ComponentWithChildViewContainer);
viewContainerFixture.detectChanges();
testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer;
});
it('should open a dialog with a component', () => {
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('Pizza');
expect(dialogRef.componentInstance instanceof PizzaMsg).toBe(true);
expect(dialogRef.componentInstance.dialogRef).toBe(dialogRef);
viewContainerFixture.detectChanges();
let dialogContainerElement = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(dialogContainerElement.getAttribute('role')).toBe('dialog');
});
it('should open a dialog with a template', () => {
const templateRefFixture = TestBed.createComponent(ComponentWithTemplateRef);
templateRefFixture.componentInstance.localValue = 'Bees';
templateRefFixture.detectChanges();
const data = {value: 'Knees'};
let dialogRef = dialog.open(templateRefFixture.componentInstance.templateRef, { data });
viewContainerFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('Cheese Bees Knees');
expect(templateRefFixture.componentInstance.dialogRef).toBe(dialogRef);
viewContainerFixture.detectChanges();
let dialogContainerElement = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(dialogContainerElement.getAttribute('role')).toBe('dialog');
dialogRef.close();
});
it('should emit when dialog opening animation is complete', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('afterOpen spy');
dialogRef.afterOpened().subscribe(spy);
viewContainerFixture.detectChanges();
// callback should not be called before animation is complete
expect(spy).not.toHaveBeenCalled();
flushMicrotasks();
expect(spy).toHaveBeenCalled();
}));
it('should use injector from viewContainerRef for DialogInjector', () => {
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
let dialogInjector = dialogRef.componentInstance.dialogInjector;
expect(dialogRef.componentInstance.dialogRef).toBe(dialogRef);
expect(dialogInjector.get<DirectiveWithViewContainer>(DirectiveWithViewContainer)).toBeTruthy(
'Expected the dialog component to be created with the injector from the viewContainerRef.'
);
});
it('should open a dialog with a component and no ViewContainerRef', () => {
let dialogRef = dialog.open(PizzaMsg);
viewContainerFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('Pizza');
expect(dialogRef.componentInstance instanceof PizzaMsg).toBe(true);
expect(dialogRef.componentInstance.dialogRef).toBe(dialogRef);
viewContainerFixture.detectChanges();
let dialogContainerElement = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(dialogContainerElement.getAttribute('role')).toBe('dialog');
});
it('should apply the configured role to the dialog element', () => {
dialog.open(PizzaMsg, { role: 'alertdialog' });
viewContainerFixture.detectChanges();
let dialogContainerElement = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(dialogContainerElement.getAttribute('role')).toBe('alertdialog');
});
it('should apply the specified `aria-describedby`', () => {
dialog.open(PizzaMsg, { ariaDescribedBy: 'description-element' });
viewContainerFixture.detectChanges();
let dialogContainerElement = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(dialogContainerElement.getAttribute('aria-describedby')).toBe('description-element');
});
it('should close a dialog and get back a result', fakeAsync(() => {
let dialogRef = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef });
let afterCloseCallback = jasmine.createSpy('afterClose callback');
dialogRef.afterClosed().subscribe(afterCloseCallback);
dialogRef.close('Charmander');
viewContainerFixture.detectChanges();
flush();
expect(afterCloseCallback).toHaveBeenCalledWith('Charmander');
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));
it('should dispatch the beforeClose and afterClose events when the ' +
'overlay is detached externally', fakeAsync(inject([Overlay], (overlay: Overlay) => {
const dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
scrollStrategy: overlay.scrollStrategies.close()
});
const beforeCloseCallback = jasmine.createSpy('beforeClosed callback');
const afterCloseCallback = jasmine.createSpy('afterClosed callback');
dialogRef.beforeClose().subscribe(beforeCloseCallback);
dialogRef.afterClosed().subscribe(afterCloseCallback);
scrolledSubject.next();
viewContainerFixture.detectChanges();
flush();
expect(beforeCloseCallback).toHaveBeenCalledTimes(1);
expect(afterCloseCallback).toHaveBeenCalledTimes(1);
})));
it('should close a dialog and get back a result before it is closed', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
flush();
viewContainerFixture.detectChanges();
// beforeClose should emit before dialog container is destroyed
const beforeCloseHandler = jasmine.createSpy('beforeClose callback').and.callFake(() => {
expect(overlayContainerElement.querySelector('mat-dialog-container'))
.not.toBeNull('dialog container exists when beforeClose is called');
});
dialogRef.beforeClosed().subscribe(beforeCloseHandler);
dialogRef.close('Bulbasaur');
viewContainerFixture.detectChanges();
flush();
expect(beforeCloseHandler).toHaveBeenCalledWith('Bulbasaur');
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));
it('should close a dialog via the escape key', fakeAsync(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));
it('should close from a ViewContainerRef with OnPush change detection', fakeAsync(() => {
const onPushFixture = TestBed.createComponent(ComponentWithOnPushViewContainer);
onPushFixture.detectChanges();
const dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: onPushFixture.componentInstance.viewContainerRef
});
flushMicrotasks();
onPushFixture.detectChanges();
flushMicrotasks();
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length)
.toBe(1, 'Expected one open dialog.');
dialogRef.close();
flushMicrotasks();
onPushFixture.detectChanges();
tick(500);
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length)
.toBe(0, 'Expected no open dialogs.');
}));
it('should close when clicking on the overlay backdrop', fakeAsync(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeFalsy();
}));
it('should emit the backdropClick stream when clicking on the overlay backdrop', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
const spy = jasmine.createSpy('backdropClick spy');
dialogRef.backdropClick().subscribe(spy);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
expect(spy).toHaveBeenCalledTimes(1);
viewContainerFixture.detectChanges();
flush();
// Additional clicks after the dialog has closed should not be emitted
backdrop.click();
expect(spy).toHaveBeenCalledTimes(1);
}));
it('should emit the keyboardEvent stream when key events target the overlay', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('keyboardEvent spy');
dialogRef.keydownEvents().subscribe(spy);
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
let container = overlayContainerElement.querySelector('mat-dialog-container') as HTMLElement;
dispatchKeyboardEvent(document.body, 'keydown', A);
dispatchKeyboardEvent(document.body, 'keydown', A, backdrop);
dispatchKeyboardEvent(document.body, 'keydown', A, container);
expect(spy).toHaveBeenCalledTimes(3);
}));
it('should notify the observers if a dialog has been opened', () => {
dialog.afterOpened.subscribe(ref => {
expect(dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
})).toBe(ref);
});
});
it('should notify the observers if all open dialogs have finished closing', fakeAsync(() => {
const ref1 = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef });
const ref2 = dialog.open(ContentElementDialog, { viewContainerRef: testViewContainerRef });
const spy = jasmine.createSpy('afterAllClosed spy');
dialog.afterAllClosed.subscribe(spy);
ref1.close();
viewContainerFixture.detectChanges();
flush();
expect(spy).not.toHaveBeenCalled();
ref2.close();
viewContainerFixture.detectChanges();
flush();
expect(spy).toHaveBeenCalled();
}));
it('should emit the afterAllClosed stream on subscribe if there are no open dialogs', () => {
const spy = jasmine.createSpy('afterAllClosed spy');
dialog.afterAllClosed.subscribe(spy);
expect(spy).toHaveBeenCalled();
});
it('should override the width of the overlay pane', () => {
dialog.open(PizzaMsg, {
width: '500px'
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.width).toBe('500px');
});
it('should override the height of the overlay pane', () => {
dialog.open(PizzaMsg, {
height: '100px'
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.height).toBe('100px');
});
it('should override the min-width of the overlay pane', () => {
dialog.open(PizzaMsg, {
minWidth: '500px'
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.minWidth).toBe('500px');
});
it('should override the max-width of the overlay pane', fakeAsync(() => {
let dialogRef = dialog.open(PizzaMsg);
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.maxWidth).toBe('80vw',
'Expected dialog to set a default max-width on overlay pane');
dialogRef.close();
tick(500);
viewContainerFixture.detectChanges();
flushMicrotasks();
dialogRef = dialog.open(PizzaMsg, {
maxWidth: '100px'
});
viewContainerFixture.detectChanges();
overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.maxWidth).toBe('100px');
}));
it('should override the min-height of the overlay pane', () => {
dialog.open(PizzaMsg, {
minHeight: '300px'
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.minHeight).toBe('300px');
});
it('should override the max-height of the overlay pane', () => {
dialog.open(PizzaMsg, {
maxHeight: '100px'
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.maxHeight).toBe('100px');
});
it('should override the top offset of the overlay pane', () => {
dialog.open(PizzaMsg, {
position: {
top: '100px'
}
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.marginTop).toBe('100px');
});
it('should override the bottom offset of the overlay pane', () => {
dialog.open(PizzaMsg, {
position: {
bottom: '200px'
}
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.marginBottom).toBe('200px');
});
it('should override the left offset of the overlay pane', () => {
dialog.open(PizzaMsg, {
position: {
left: '250px'
}
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.marginLeft).toBe('250px');
});
it('should override the right offset of the overlay pane', () => {
dialog.open(PizzaMsg, {
position: {
right: '125px'
}
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.marginRight).toBe('125px');
});
it('should allow for the position to be updated', () => {
let dialogRef = dialog.open(PizzaMsg, {
position: {
left: '250px'
}
});
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.marginLeft).toBe('250px');
dialogRef.updatePosition({ left: '500px' });
expect(overlayPane.style.marginLeft).toBe('500px');
});
it('should allow for the dimensions to be updated', () => {
let dialogRef = dialog.open(PizzaMsg, { width: '100px' });
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.width).toBe('100px');
dialogRef.updateSize('200px');
expect(overlayPane.style.width).toBe('200px');
});
it('should reset the overlay dimensions to their initial size', () => {
let dialogRef = dialog.open(PizzaMsg);
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(overlayPane.style.width).toBeFalsy();
expect(overlayPane.style.height).toBeFalsy();
dialogRef.updateSize('200px', '200px');
expect(overlayPane.style.width).toBe('200px');
expect(overlayPane.style.height).toBe('200px');
dialogRef.updateSize();
expect(overlayPane.style.width).toBeFalsy();
expect(overlayPane.style.height).toBeFalsy();
});
it('should allow setting the layout direction', () => {
dialog.open(PizzaMsg, { direction: 'rtl' });
viewContainerFixture.detectChanges();
let overlayPane = overlayContainerElement.querySelector('.cdk-global-overlay-wrapper')!;
expect(overlayPane.getAttribute('dir')).toBe('rtl');
});
it('should inject the correct layout direction in the component instance', () => {
const dialogRef = dialog.open(PizzaMsg, { direction: 'rtl' });
viewContainerFixture.detectChanges();
expect(dialogRef.componentInstance.directionality.value).toBe('rtl');
});
it('should fall back to injecting the global direction if none is passed by the config', () => {
const dialogRef = dialog.open(PizzaMsg, {});
viewContainerFixture.detectChanges();
expect(dialogRef.componentInstance.directionality.value).toBe('ltr');
});
it('should close all of the dialogs', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(3);
dialog.closeAll();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(0);
}));
it('should set the proper animation states', () => {
let dialogRef = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef });
let dialogContainer: MatDialogContainer =
viewContainerFixture.debugElement.query(By.directive(MatDialogContainer)).componentInstance;
expect(dialogContainer._state).toBe('enter');
dialogRef.close();
expect(dialogContainer._state).toBe('exit');
});
it('should close all dialogs when the user goes forwards/backwards in history', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(2);
mockLocation.simulateUrlPop('');
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(0);
}));
it('should close all open dialogs when the location hash changes', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(2);
mockLocation.simulateHashChange('');
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(0);
}));
it('should close all of the dialogs when the injectable is destroyed', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(3);
dialog.ngOnDestroy();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(0);
}));
it('should complete open and close streams when the injectable is destroyed', fakeAsync(() => {
const afterOpenedSpy = jasmine.createSpy('after opened spy');
const afterAllClosedSpy = jasmine.createSpy('after all closed spy');
const afterOpenedSubscription = dialog.afterOpened.subscribe({complete: afterOpenedSpy});
const afterAllClosedSubscription = dialog.afterAllClosed.subscribe({
complete: afterAllClosedSpy
});
dialog.ngOnDestroy();
expect(afterOpenedSpy).toHaveBeenCalled();
expect(afterAllClosedSpy).toHaveBeenCalled();
afterOpenedSubscription.unsubscribe();
afterAllClosedSubscription.unsubscribe();
}));
it('should allow the consumer to disable closing a dialog on navigation', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg, {closeOnNavigation: false});
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(2);
mockLocation.simulateUrlPop('');
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(1);
}));
it('should have the componentInstance available in the afterClosed callback', fakeAsync(() => {
let dialogRef = dialog.open(PizzaMsg);
let spy = jasmine.createSpy('afterClosed spy');
flushMicrotasks();
viewContainerFixture.detectChanges();
flushMicrotasks();
dialogRef.afterClosed().subscribe(() => {
spy();
expect(dialogRef.componentInstance).toBeTruthy('Expected component instance to be defined.');
});
dialogRef.close();
flushMicrotasks();
viewContainerFixture.detectChanges();
tick(500);
// Ensure that the callback actually fires.
expect(spy).toHaveBeenCalled();
}));
it('should be able to attach a custom scroll strategy', fakeAsync(() => {
const scrollStrategy: ScrollStrategy = {
attach: () => {},
enable: jasmine.createSpy('scroll strategy enable spy'),
disable: () => {}
};
dialog.open(PizzaMsg, {scrollStrategy});
expect(scrollStrategy.enable).toHaveBeenCalled();
}));
describe('passing in data', () => {
it('should be able to pass in data', () => {
let config = {
data: {
stringParam: 'hello',
dateParam: new Date()
}
};
let instance = dialog.open(DialogWithInjectedData, config).componentInstance;
expect(instance.data.stringParam).toBe(config.data.stringParam);
expect(instance.data.dateParam).toBe(config.data.dateParam);
});
it('should default to null if no data is passed', () => {
expect(() => {
let dialogRef = dialog.open(DialogWithInjectedData);
expect(dialogRef.componentInstance.data).toBeNull();
}).not.toThrow();
});
});
it('should not keep a reference to the component after the dialog is closed', fakeAsync(() => {
let dialogRef = dialog.open(PizzaMsg);
expect(dialogRef.componentInstance).toBeTruthy();
dialogRef.close();
viewContainerFixture.detectChanges();
flush();
expect(dialogRef.componentInstance).toBeFalsy('Expected reference to have been cleared.');
}));
it('should assign a unique id to each dialog', () => {
const one = dialog.open(PizzaMsg);
const two = dialog.open(PizzaMsg);
expect(one.id).toBeTruthy();
expect(two.id).toBeTruthy();
expect(one.id).not.toBe(two.id);
});
it('should allow for the id to be overwritten', () => {
const dialogRef = dialog.open(PizzaMsg, { id: 'pizza' });
expect(dialogRef.id).toBe('pizza');
});
it('should throw when trying to open a dialog with the same id as another dialog', () => {
dialog.open(PizzaMsg, { id: 'pizza' });
expect(() => dialog.open(PizzaMsg, { id: 'pizza' })).toThrowError(/must be unique/g);
});
it('should be able to find a dialog by id', () => {
const dialogRef = dialog.open(PizzaMsg, { id: 'pizza' });
expect(dialog.getDialogById('pizza')).toBe(dialogRef);
});
it('should toggle `aria-hidden` on the overlay container siblings', fakeAsync(() => {
const sibling = document.createElement('div');
overlayContainerElement.parentNode!.appendChild(sibling);
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
flush();
expect(sibling.getAttribute('aria-hidden')).toBe('true', 'Expected sibling to be hidden');
expect(overlayContainerElement.hasAttribute('aria-hidden'))
.toBe(false, 'Expected overlay container not to be hidden.');
dialogRef.close();
viewContainerFixture.detectChanges();
flush();
expect(sibling.hasAttribute('aria-hidden'))
.toBe(false, 'Expected sibling to no longer be hidden.');
sibling.parentNode!.removeChild(sibling);
}));
it('should restore `aria-hidden` to the overlay container siblings on close', fakeAsync(() => {
const sibling = document.createElement('div');
sibling.setAttribute('aria-hidden', 'true');
overlayContainerElement.parentNode!.appendChild(sibling);
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
flush();
expect(sibling.getAttribute('aria-hidden')).toBe('true', 'Expected sibling to be hidden.');
dialogRef.close();
viewContainerFixture.detectChanges();
flush();
expect(sibling.getAttribute('aria-hidden')).toBe('true', 'Expected sibling to remain hidden.');
sibling.parentNode!.removeChild(sibling);
}));
it('should not set `aria-hidden` on `aria-live` elements', fakeAsync(() => {
const sibling = document.createElement('div');
sibling.setAttribute('aria-live', 'polite');
overlayContainerElement.parentNode!.appendChild(sibling);
dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
flush();
expect(sibling.hasAttribute('aria-hidden'))
.toBe(false, 'Expected live element not to be hidden.');
sibling.parentNode!.removeChild(sibling);
}));
it('should add and remove classes while open', () => {
let dialogRef = dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.classList)
.not.toContain('custom-class-one', 'Expected class to be initially missing');
dialogRef.addPanelClass('custom-class-one');
expect(pane.classList).toContain('custom-class-one', 'Expected class to be added');
dialogRef.removePanelClass('custom-class-one');
expect(pane.classList).not.toContain('custom-class-one', 'Expected class to be removed');
});
describe('disableClose option', () => {
it('should prevent closing via clicks on the backdrop', fakeAsync(() => {
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeTruthy();
}));
it('should prevent closing via the escape key', fakeAsync(() => {
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeTruthy();
}));
it('should allow for the disableClose option to be updated while open', fakeAsync(() => {
let dialogRef = dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeTruthy();
dialogRef.disableClose = false;
backdrop.click();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeFalsy();
}));
});
describe('hasBackdrop option', () => {
it('should have a backdrop', () => {
dialog.open(PizzaMsg, {
hasBackdrop: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
});
it('should not have a backdrop', () => {
dialog.open(PizzaMsg, {
hasBackdrop: false,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
});
});
describe('panelClass option', () => {
it('should have custom panel class', () => {
dialog.open(PizzaMsg, {
panelClass: 'custom-panel-class',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.custom-panel-class')).toBeTruthy();
});
});
describe('backdropClass option', () => {
it('should have default backdrop class', () => {
dialog.open(PizzaMsg, {
backdropClass: '',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-dark-backdrop')).toBeTruthy();
});
it('should have custom backdrop class', () => {
dialog.open(PizzaMsg, {
backdropClass: 'custom-backdrop-class',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.custom-backdrop-class')).toBeTruthy();
});
});
describe('focus management', () => {
// When testing focus, all of the elements must be in the DOM.
beforeEach(() => document.body.appendChild(overlayContainerElement));
afterEach(() => document.body.removeChild(overlayContainerElement));
it('should focus the first tabbable element of the dialog on open', fakeAsync(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.tagName)
.toBe('INPUT', 'Expected first tabbable element (input) in the dialog to be focused.');
}));
it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: false
});
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.tagName).not.toBe('INPUT');
}));
it('should re-focus trigger element when dialog closes', fakeAsync(() => {
// Create a element that has focus before the dialog is opened.
let button = document.createElement('button');
button.id = 'dialog-trigger';
document.body.appendChild(button);
button.focus();
let dialogRef = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef });