forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 9
/
bottom-sheet.spec.ts
1019 lines (775 loc) · 32.7 KB
/
bottom-sheet.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 {Directionality} from '@angular/cdk/bidi';
import {A, ESCAPE} from '@angular/cdk/keycodes';
import {OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
import {_supportsShadowDom} from '@angular/cdk/platform';
import {ViewportRuler} from '@angular/cdk/scrolling';
import {
dispatchKeyboardEvent,
createKeyboardEvent,
dispatchEvent,
} from '@angular/cdk/testing/private';
import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {
Component,
Directive,
Inject,
Injector,
NgModule,
TemplateRef,
ViewChild,
ViewContainerRef,
ViewEncapsulation,
} from '@angular/core';
import {
ComponentFixture,
fakeAsync,
flush,
flushMicrotasks,
inject,
TestBed,
tick,
} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MAT_BOTTOM_SHEET_DEFAULT_OPTIONS, MatBottomSheet} from './bottom-sheet';
import {MAT_BOTTOM_SHEET_DATA, MatBottomSheetConfig} from './bottom-sheet-config';
import {MatBottomSheetModule} from './bottom-sheet-module';
import {MatBottomSheetRef} from './bottom-sheet-ref';
describe('MatBottomSheet', () => {
let bottomSheet: MatBottomSheet;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let viewportRuler: ViewportRuler;
let testViewContainerRef: ViewContainerRef;
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
let mockLocation: SpyLocation;
beforeEach(fakeAsync(() => {
TestBed
.configureTestingModule({
imports: [MatBottomSheetModule, BottomSheetTestModule],
providers: [{provide: Location, useClass: SpyLocation}]
})
.compileComponents();
}));
beforeEach(inject([MatBottomSheet, OverlayContainer, ViewportRuler, Location],
(bs: MatBottomSheet, oc: OverlayContainer, vr: ViewportRuler, l: Location) => {
bottomSheet = bs;
overlayContainer = oc;
viewportRuler = vr;
overlayContainerElement = oc.getContainerElement();
mockLocation = l as SpyLocation;
}));
afterEach(() => {
overlayContainer.ngOnDestroy();
});
beforeEach(() => {
viewContainerFixture = TestBed.createComponent(ComponentWithChildViewContainer);
viewContainerFixture.detectChanges();
testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer;
});
it('should open a bottom sheet with a component', () => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('Pizza');
expect(bottomSheetRef.instance instanceof PizzaMsg).toBe(true);
expect(bottomSheetRef.instance.bottomSheetRef).toBe(bottomSheetRef);
});
it('should open a bottom sheet with a template', () => {
const templateRefFixture = TestBed.createComponent(ComponentWithTemplateRef);
templateRefFixture.componentInstance.localValue = 'Bees';
templateRefFixture.detectChanges();
const bottomSheetRef = bottomSheet.open(templateRefFixture.componentInstance.templateRef, {
data: {value: 'Knees'}
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('Cheese Bees Knees');
expect(templateRefFixture.componentInstance.bottomSheetRef).toBe(bottomSheetRef);
});
it('should position the bottom sheet at the bottom center of the screen', () => {
bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
const containerElement = overlayContainerElement.querySelector('mat-bottom-sheet-container')!;
const containerRect = containerElement.getBoundingClientRect();
const viewportSize = viewportRuler.getViewportSize();
expect(Math.floor(containerRect.bottom)).toBe(Math.floor(viewportSize.height));
expect(Math.floor(containerRect.left + containerRect.width / 2))
.toBe(Math.floor(viewportSize.width / 2));
});
it('should emit when the bottom sheet opening animation is complete', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('afterOpened spy');
bottomSheetRef.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 the correct injector', () => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
const injector = bottomSheetRef.instance.injector;
expect(bottomSheetRef.instance.bottomSheetRef).toBe(bottomSheetRef);
expect(injector.get<DirectiveWithViewContainer>(DirectiveWithViewContainer)).toBeTruthy();
});
it('should open a bottom sheet with a component and no ViewContainerRef', () => {
const bottomSheetRef = bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('Pizza');
expect(bottomSheetRef.instance instanceof PizzaMsg).toBe(true);
expect(bottomSheetRef.instance.bottomSheetRef).toBe(bottomSheetRef);
});
it('should apply the correct role to the container element', () => {
bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
const containerElement = overlayContainerElement.querySelector('mat-bottom-sheet-container')!;
expect(containerElement.getAttribute('role')).toBe('dialog');
});
it('should close a bottom sheet via the escape key', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const event = dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeNull();
expect(event.defaultPrevented).toBe(true);
}));
it('should not close a bottom sheet via the escape key with a modifier', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const event = createKeyboardEvent('keydown', ESCAPE, undefined, {alt: true});
dispatchEvent(document.body, event);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
expect(event.defaultPrevented).toBe(false);
}));
it('should close when clicking on the overlay backdrop', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
const backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));
it('should dispose of bottom sheet if view container is destroyed while animating',
fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
bottomSheetRef.dismiss();
viewContainerFixture.detectChanges();
viewContainerFixture.destroy();
flush();
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));
it('should emit the backdropClick stream when clicking on the overlay backdrop', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('backdropClick spy');
bottomSheetRef.backdropClick().subscribe(spy);
viewContainerFixture.detectChanges();
const backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
expect(spy).toHaveBeenCalledTimes(1);
viewContainerFixture.detectChanges();
flush();
// Additional clicks after the bottom sheet was 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 bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('keyboardEvent spy');
bottomSheetRef.keydownEvents().subscribe(spy);
viewContainerFixture.detectChanges();
const backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
const container =
overlayContainerElement.querySelector('mat-bottom-sheet-container') as HTMLElement;
dispatchKeyboardEvent(document.body, 'keydown', A);
dispatchKeyboardEvent(backdrop, 'keydown', A);
dispatchKeyboardEvent(container, 'keydown', A);
expect(spy).toHaveBeenCalledTimes(3);
}));
it('should allow setting the layout direction', () => {
bottomSheet.open(PizzaMsg, {direction: 'rtl'});
viewContainerFixture.detectChanges();
const overlayPane = overlayContainerElement.querySelector('.cdk-global-overlay-wrapper')!;
expect(overlayPane.getAttribute('dir')).toBe('rtl');
});
it('should inject the correct direction in the instantiated component', () => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {direction: 'rtl'});
viewContainerFixture.detectChanges();
expect(bottomSheetRef.instance.directionality.value).toBe('rtl');
});
it('should fall back to injecting the global direction if none is passed by the config', () => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {});
viewContainerFixture.detectChanges();
expect(bottomSheetRef.instance.directionality.value).toBe('ltr');
});
it('should be able to set a custom panel class', () => {
bottomSheet.open(PizzaMsg, {
panelClass: 'custom-panel-class',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.custom-panel-class')).toBeTruthy();
});
it('should be able to set a custom aria-label', () => {
bottomSheet.open(PizzaMsg, {
ariaLabel: 'Hello there',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
const container = overlayContainerElement.querySelector('mat-bottom-sheet-container')!;
expect(container.getAttribute('aria-label')).toBe('Hello there');
});
it('should be able to get dismissed through the service', fakeAsync(() => {
bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
bottomSheet.dismiss();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.childElementCount).toBe(0);
}));
it('should dismiss the bottom sheet when the service is destroyed', fakeAsync(() => {
bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);
bottomSheet.ngOnDestroy();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.childElementCount).toBe(0);
}));
it('should open a new bottom sheet after dismissing a previous sheet', fakeAsync(() => {
const config: MatBottomSheetConfig = {viewContainerRef: testViewContainerRef};
let bottomSheetRef: MatBottomSheetRef<any> = bottomSheet.open(PizzaMsg, config);
viewContainerFixture.detectChanges();
bottomSheetRef.dismiss();
viewContainerFixture.detectChanges();
// Wait for the dismiss animation to finish.
flush();
bottomSheetRef = bottomSheet.open(TacoMsg, config);
viewContainerFixture.detectChanges();
// Wait for the open animation to finish.
flush();
expect(bottomSheetRef.containerInstance._animationState)
.toBe('visible', `Expected the animation state would be 'visible'.`);
}));
it('should remove past bottom sheets when opening new ones', fakeAsync(() => {
bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
bottomSheet.open(TacoMsg);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.textContent).toContain('Taco');
}));
it('should not throw when opening multiple bottom sheet in quick succession', fakeAsync(() => {
expect(() => {
for (let i = 0; i < 3; i++) {
bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
}
flush();
}).not.toThrow();
}));
it('should remove bottom sheet if another is shown while its still animating open',
fakeAsync(() => {
bottomSheet.open(PizzaMsg);
viewContainerFixture.detectChanges();
bottomSheet.open(TacoMsg);
viewContainerFixture.detectChanges();
tick();
expect(overlayContainerElement.textContent).toContain('Taco');
tick(500);
}));
it('should emit after being dismissed', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg);
const spy = jasmine.createSpy('afterDismissed spy');
bottomSheetRef.afterDismissed().subscribe(spy);
viewContainerFixture.detectChanges();
bottomSheetRef.dismiss();
viewContainerFixture.detectChanges();
flush();
expect(spy).toHaveBeenCalledTimes(1);
}));
it('should be able to pass a result back to the dismissed stream', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open<PizzaMsg, any, number>(PizzaMsg);
const spy = jasmine.createSpy('afterDismissed spy');
bottomSheetRef.afterDismissed().subscribe(spy);
viewContainerFixture.detectChanges();
bottomSheetRef.dismiss(1337);
viewContainerFixture.detectChanges();
flush();
expect(spy).toHaveBeenCalledWith(1337);
}));
it('should be able to pass data when dismissing through the service', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open<PizzaMsg, any, number>(PizzaMsg);
const spy = jasmine.createSpy('afterDismissed spy');
bottomSheetRef.afterDismissed().subscribe(spy);
viewContainerFixture.detectChanges();
bottomSheet.dismiss(1337);
viewContainerFixture.detectChanges();
flush();
expect(spy).toHaveBeenCalledWith(1337);
}));
it('should close the bottom sheet when going forwards/backwards in history', fakeAsync(() => {
bottomSheet.open(PizzaMsg);
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
mockLocation.simulateUrlPop('');
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));
it('should close the bottom sheet when the location hash changes', fakeAsync(() => {
bottomSheet.open(PizzaMsg);
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
mockLocation.simulateHashChange('');
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));
it('should allow the consumer to disable closing a bottom sheet on navigation', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {closeOnNavigation: false});
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
mockLocation.simulateUrlPop('');
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
}));
it('should be able to attach a custom scroll strategy', fakeAsync(() => {
const scrollStrategy: ScrollStrategy = {
attach: () => {},
enable: jasmine.createSpy('scroll strategy enable spy'),
disable: () => {}
};
bottomSheet.open(PizzaMsg, {scrollStrategy});
expect(scrollStrategy.enable).toHaveBeenCalled();
}));
describe('passing in data', () => {
it('should be able to pass in data', () => {
const config = {
data: {
stringParam: 'hello',
dateParam: new Date()
}
};
const instance = bottomSheet.open(BottomSheetWithInjectedData, config).instance;
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(() => {
const bottomSheetRef = bottomSheet.open(BottomSheetWithInjectedData);
expect(bottomSheetRef.instance.data).toBeNull();
}).not.toThrow();
});
});
describe('disableClose option', () => {
it('should prevent closing via clicks on the backdrop', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
const backdrop =
overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
}));
it('should prevent closing via the escape key', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
}));
it('should allow for the disableClose option to be updated while open', fakeAsync(() => {
const bottomSheetRef = bottomSheet.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
const backdrop =
overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
bottomSheetRef.disableClose = false;
backdrop.click();
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));
});
describe('hasBackdrop option', () => {
it('should have a backdrop', () => {
bottomSheet.open(PizzaMsg, {
hasBackdrop: true,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
});
it('should not have a backdrop', () => {
bottomSheet.open(PizzaMsg, {
hasBackdrop: false,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
});
});
describe('backdropClass option', () => {
it('should have default backdrop class', () => {
bottomSheet.open(PizzaMsg, {
backdropClass: '',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-dark-backdrop')).toBeTruthy();
});
it('should have custom backdrop class', () => {
bottomSheet.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 bottom sheet container by default', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
});
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.tagName).toBe('MAT-BOTTOM-SHEET-CONTAINER',
'Expected bottom sheet container to be focused.');
}));
it('should create a focus trap if autoFocus is disabled', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: false
});
viewContainerFixture.detectChanges();
flushMicrotasks();
const focusTrapAnchors = overlayContainerElement.querySelectorAll('.cdk-focus-trap-anchor');
expect(focusTrapAnchors.length).toBeGreaterThan(0);
}));
it('should focus the first tabbable element of the bottom sheet on open when' +
'autoFocus is enabled', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: true
});
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.tagName).toBe('INPUT',
'Expected first tabbable element (input) in the sheet to be focused.');
}));
it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: false
});
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.tagName).not.toBe('INPUT');
}));
it('should re-focus trigger element when bottom sheet closes', fakeAsync(() => {
const button = document.createElement('button');
button.id = 'bottom-sheet-trigger';
document.body.appendChild(button);
button.focus();
const bottomSheetRef = bottomSheet.open(PizzaMsg, { viewContainerRef: testViewContainerRef });
flushMicrotasks();
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.id)
.not.toBe('bottom-sheet-trigger', 'Expected the focus to change when sheet was opened.');
bottomSheetRef.dismiss();
expect(document.activeElement!.id).not.toBe('bottom-sheet-trigger',
'Expcted the focus not to have changed before the animation finishes.');
flushMicrotasks();
viewContainerFixture.detectChanges();
tick(500);
expect(document.activeElement!.id).toBe('bottom-sheet-trigger',
'Expected that the trigger was refocused after the sheet is closed.');
document.body.removeChild(button);
}));
it('should be able to disable focus restoration', fakeAsync(() => {
const button = document.createElement('button');
button.id = 'bottom-sheet-trigger';
document.body.appendChild(button);
button.focus();
const bottomSheetRef = bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
restoreFocus: false
});
flushMicrotasks();
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.id)
.not.toBe('bottom-sheet-trigger', 'Expected the focus to change when sheet was opened.');
bottomSheetRef.dismiss();
expect(document.activeElement!.id).not.toBe('bottom-sheet-trigger',
'Expcted the focus not to have changed before the animation finishes.');
flushMicrotasks();
viewContainerFixture.detectChanges();
tick(500);
expect(document.activeElement!.id).not.toBe('bottom-sheet-trigger',
'Expected the trigger not to be refocused on close.');
document.body.removeChild(button);
}));
it('should not move focus if it was moved outside the sheet while animating', fakeAsync(() => {
// Create a element that has focus before the bottom sheet is opened.
const button = document.createElement('button');
const otherButton = document.createElement('button');
const body = document.body;
button.id = 'bottom-sheet-trigger';
otherButton.id = 'other-button';
body.appendChild(button);
body.appendChild(otherButton);
button.focus();
const bottomSheetRef = bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
flushMicrotasks();
viewContainerFixture.detectChanges();
flushMicrotasks();
expect(document.activeElement!.id).not.toBe('bottom-sheet-trigger',
'Expected the focus to change when the bottom sheet was opened.');
// Start the closing sequence and move focus out of bottom sheet.
bottomSheetRef.dismiss();
otherButton.focus();
expect(document.activeElement!.id)
.toBe('other-button', 'Expected focus to be on the alternate button.');
flushMicrotasks();
viewContainerFixture.detectChanges();
flush();
expect(document.activeElement!.id)
.toBe('other-button', 'Expected focus to stay on the alternate button.');
body.removeChild(button);
body.removeChild(otherButton);
}));
it('should re-focus trigger element inside the shadow DOM when the bottom sheet is dismissed',
fakeAsync(() => {
if (!_supportsShadowDom()) {
return;
}
viewContainerFixture.destroy();
const fixture = TestBed.createComponent(ShadowDomComponent);
fixture.detectChanges();
const button = fixture.debugElement.query(By.css('button'))!.nativeElement;
button.focus();
const ref = bottomSheet.open(PizzaMsg);
flushMicrotasks();
fixture.detectChanges();
flushMicrotasks();
const spy = spyOn(button, 'focus').and.callThrough();
ref.dismiss();
flushMicrotasks();
fixture.detectChanges();
tick(500);
expect(spy).toHaveBeenCalled();
}));
});
});
describe('MatBottomSheet with parent MatBottomSheet', () => {
let parentBottomSheet: MatBottomSheet;
let childBottomSheet: MatBottomSheet;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let fixture: ComponentFixture<ComponentThatProvidesMatBottomSheet>;
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [MatBottomSheetModule, BottomSheetTestModule, NoopAnimationsModule],
declarations: [ComponentThatProvidesMatBottomSheet],
}).compileComponents();
}));
beforeEach(inject([MatBottomSheet, OverlayContainer],
(bs: MatBottomSheet, oc: OverlayContainer) => {
parentBottomSheet = bs;
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
fixture = TestBed.createComponent(ComponentThatProvidesMatBottomSheet);
childBottomSheet = fixture.componentInstance.bottomSheet;
fixture.detectChanges();
}));
afterEach(() => {
overlayContainer.ngOnDestroy();
});
it('should close bottom sheets opened by parent when opening from child', fakeAsync(() => {
parentBottomSheet.open(PizzaMsg);
fixture.detectChanges();
tick(1000);
expect(overlayContainerElement.textContent)
.toContain('Pizza', 'Expected a bottom sheet to be opened');
childBottomSheet.open(TacoMsg);
fixture.detectChanges();
tick(1000);
expect(overlayContainerElement.textContent)
.toContain('Taco', 'Expected parent bottom sheet to be dismissed by opening from child');
}));
it('should close bottom sheets opened by child when opening from parent', fakeAsync(() => {
childBottomSheet.open(PizzaMsg);
fixture.detectChanges();
tick(1000);
expect(overlayContainerElement.textContent)
.toContain('Pizza', 'Expected a bottom sheet to be opened');
parentBottomSheet.open(TacoMsg);
fixture.detectChanges();
tick(1000);
expect(overlayContainerElement.textContent)
.toContain('Taco', 'Expected child bottom sheet to be dismissed by opening from parent');
}));
it('should not close parent bottom sheet when child is destroyed', fakeAsync(() => {
parentBottomSheet.open(PizzaMsg);
fixture.detectChanges();
tick(1000);
expect(overlayContainerElement.textContent)
.toContain('Pizza', 'Expected a bottom sheet to be opened');
childBottomSheet.ngOnDestroy();
fixture.detectChanges();
tick(1000);
expect(overlayContainerElement.textContent)
.toContain('Pizza', 'Expected a bottom sheet to stay open');
}));
});
describe('MatBottomSheet with default options', () => {
let bottomSheet: MatBottomSheet;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let testViewContainerRef: ViewContainerRef;
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
beforeEach(fakeAsync(() => {
const defaultConfig: MatBottomSheetConfig = {
hasBackdrop: false,
disableClose: true,
autoFocus: false
};
TestBed.configureTestingModule({
imports: [MatBottomSheetModule, BottomSheetTestModule],
providers: [
{provide: MAT_BOTTOM_SHEET_DEFAULT_OPTIONS, useValue: defaultConfig},
],
});
TestBed.compileComponents();
}));
beforeEach(inject([MatBottomSheet, OverlayContainer],
(b: MatBottomSheet, oc: OverlayContainer) => {
bottomSheet = b;
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
}));
afterEach(() => {
overlayContainer.ngOnDestroy();
});
beforeEach(() => {
viewContainerFixture = TestBed.createComponent(ComponentWithChildViewContainer);
viewContainerFixture.detectChanges();
testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer;
});
it('should use the provided defaults', () => {
bottomSheet.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
expect(document.activeElement!.tagName).not.toBe('INPUT');
});
it('should be overridable by open() options', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
hasBackdrop: true,
disableClose: false,
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
viewContainerFixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));
});
@Directive({selector: 'dir-with-view-container'})
class DirectiveWithViewContainer {
constructor(public viewContainerRef: ViewContainerRef) { }
}
@Component({template: `<dir-with-view-container></dir-with-view-container>`})
class ComponentWithChildViewContainer {
@ViewChild(DirectiveWithViewContainer) childWithViewContainer: DirectiveWithViewContainer;
get childViewContainer() {
return this.childWithViewContainer.viewContainerRef;
}
}
@Component({
selector: 'arbitrary-component-with-template-ref',
template: `<ng-template let-data let-bottomSheetRef="bottomSheetRef">
Cheese {{localValue}} {{data?.value}}{{setRef(bottomSheetRef)}}</ng-template>`,
})
class ComponentWithTemplateRef {
localValue: string;
bottomSheetRef: MatBottomSheetRef<any>;
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
setRef(bottomSheetRef: MatBottomSheetRef<any>): string {
this.bottomSheetRef = bottomSheetRef;
return '';
}
}
@Component({template: '<p>Pizza</p> <input> <button>Close</button>'})
class PizzaMsg {
constructor(public bottomSheetRef: MatBottomSheetRef<PizzaMsg>,
public injector: Injector,
public directionality: Directionality) {}
}
@Component({template: '<p>Taco</p>'})
class TacoMsg {}
@Component({
template: '',
providers: [MatBottomSheet]
})
class ComponentThatProvidesMatBottomSheet {
constructor(public bottomSheet: MatBottomSheet) {}
}
@Component({template: ''})
class BottomSheetWithInjectedData {
constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any) { }
}
@Component({
template: `<button>I'm a button</button>`,
encapsulation: ViewEncapsulation.ShadowDom
})
class ShadowDomComponent {}
// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
const TEST_DIRECTIVES = [
ComponentWithChildViewContainer,
ComponentWithTemplateRef,
PizzaMsg,