9
9
ComponentRef
10
10
} from '@angular/core' ;
11
11
import { TestBed , fakeAsync , tick , async , inject } from '@angular/core/testing' ;
12
- import { BrowserModule } from '@angular/platform-browser' ;
12
+ import { BrowserModule , By } from '@angular/platform-browser' ;
13
13
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
14
14
import { IgxOverlayService } from './overlay' ;
15
15
import { IgxToggleDirective , IgxToggleModule , IgxOverlayOutletDirective } from './../../directives/toggle/toggle.directive' ;
@@ -2718,61 +2718,43 @@ describe('igxOverlay', () => {
2718
2718
const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
2719
2719
const overlay = fixture . componentInstance . overlay ;
2720
2720
const overlaySettings : OverlaySettings = {
2721
- modal : true ,
2722
2721
closeOnEsc : true ,
2723
- positionStrategy : new GlobalPositionStrategy ( )
2724
2722
} ;
2725
2723
2726
- const targetButton = 'Escape' ;
2727
- const escEvent = new KeyboardEvent ( 'keydown' , {
2728
- key : targetButton
2729
- } ) ;
2730
-
2731
2724
overlay . show ( overlay . attach ( SimpleDynamicComponent ) , overlaySettings ) ;
2732
2725
tick ( ) ;
2733
2726
2734
2727
let overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2735
- overlayWrapper . addEventListener ( 'keydown' , ( event : KeyboardEvent ) => {
2736
- if ( event . key === targetButton ) {
2737
- overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2738
- expect ( overlayWrapper ) . toBeFalsy ( ) ;
2739
- }
2740
- } ) ;
2741
- tick ( ) ;
2742
2728
expect ( overlayWrapper ) . toBeTruthy ( ) ;
2743
- document . dispatchEvent ( escEvent ) ;
2729
+
2730
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2744
2731
tick ( ) ;
2732
+
2733
+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2734
+ expect ( overlayWrapper ) . toBeFalsy ( ) ;
2745
2735
} ) ) ;
2746
2736
2747
2737
it ( 'Should not close the component when esc key is pressed and closeOnEsc is false' , fakeAsync ( ( ) => {
2748
2738
const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
2749
2739
const overlay = fixture . componentInstance . overlay ;
2750
2740
const overlaySettings : OverlaySettings = {
2751
- modal : true ,
2752
- positionStrategy : new GlobalPositionStrategy ( )
2741
+ closeOnEsc : false
2753
2742
} ;
2754
- const targetButton = 'Escape' ;
2755
- const escEvent = new KeyboardEvent ( 'keydown' , {
2756
- key : targetButton
2757
- } ) ;
2758
2743
2759
2744
overlay . show ( overlay . attach ( SimpleDynamicComponent ) , overlaySettings ) ;
2760
2745
tick ( ) ;
2761
2746
2762
2747
let overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2763
- overlayWrapper . addEventListener ( 'keydown' , ( event : KeyboardEvent ) => {
2764
- if ( event . key === targetButton ) {
2765
- overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2766
- }
2767
- } ) ;
2768
- document . dispatchEvent ( escEvent ) ;
2748
+ expect ( overlayWrapper ) . toBeTruthy ( ) ;
2749
+
2750
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2769
2751
tick ( ) ;
2770
- fixture . detectChanges ( ) ;
2771
2752
2753
+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2772
2754
expect ( overlayWrapper ) . toBeTruthy ( ) ;
2773
2755
} ) ) ;
2774
2756
2775
- it ( 'Should close the opened overlays consecutively on escape keypress' , fakeAsync ( ( ) => {
2757
+ it ( 'Should close the opened overlays consecutively on esc keypress' , fakeAsync ( ( ) => {
2776
2758
const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
2777
2759
const overlay = fixture . componentInstance . overlay ;
2778
2760
overlay . show ( overlay . attach ( SimpleDynamicComponent ) , { closeOnEsc : true } ) ;
@@ -2783,59 +2765,70 @@ describe('igxOverlay', () => {
2783
2765
const overlayDiv = document . getElementsByClassName ( CLASS_OVERLAY_MAIN ) [ 0 ] ;
2784
2766
expect ( overlayDiv . children . length ) . toBe ( 2 ) ;
2785
2767
2786
- const escEvent = new KeyboardEvent ( 'keydown' , {
2787
- key : 'Escape'
2788
- } ) ;
2789
-
2790
- document . dispatchEvent ( escEvent ) ;
2768
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2791
2769
tick ( ) ;
2792
2770
expect ( overlayDiv . children . length ) . toBe ( 1 ) ;
2793
2771
2794
- document . dispatchEvent ( escEvent ) ;
2772
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2795
2773
tick ( ) ;
2796
2774
expect ( overlayDiv . children . length ) . toBe ( 0 ) ;
2797
2775
} ) ) ;
2798
2776
2777
+ it ( 'Should not close the opened overlays consecutively on esc keypress' , fakeAsync ( ( ) => {
2778
+ const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
2779
+ const overlay = fixture . componentInstance . overlay ;
2780
+ overlay . show ( overlay . attach ( SimpleDynamicComponent ) , { closeOnEsc : true } ) ;
2781
+ tick ( ) ;
2782
+ overlay . show ( overlay . attach ( SimpleDynamicComponent ) , { closeOnEsc : false } ) ;
2783
+ tick ( ) ;
2784
+ overlay . show ( overlay . attach ( SimpleDynamicComponent ) , { closeOnEsc : true } ) ;
2785
+ tick ( ) ;
2786
+
2787
+ const overlayDiv = document . getElementsByClassName ( CLASS_OVERLAY_MAIN ) [ 0 ] ;
2788
+ expect ( overlayDiv . children . length ) . toBe ( 3 ) ;
2789
+
2790
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2791
+ tick ( ) ;
2792
+ expect ( overlayDiv . children . length ) . toBe ( 2 ) ;
2793
+
2794
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2795
+ tick ( ) ;
2796
+ expect ( overlayDiv . children . length ) . toBe ( 2 ) ;
2797
+ } ) ) ;
2798
+
2799
2799
// Test #1883 #1820
2800
2800
it ( 'It should close the component when esc key is pressed and there were other keys pressed prior to esc.' , fakeAsync ( ( ) => {
2801
2801
const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
2802
2802
const overlay = fixture . componentInstance . overlay ;
2803
2803
const overlaySettings : OverlaySettings = {
2804
- modal : true ,
2805
2804
closeOnEsc : true ,
2806
- positionStrategy : new GlobalPositionStrategy ( )
2807
2805
} ;
2808
2806
2809
- const escEvent = new KeyboardEvent ( 'keydown' , {
2810
- key : 'Escape'
2811
- } ) ;
2812
- const enterEvent = new KeyboardEvent ( 'keydown' , {
2813
- key : 'Enter'
2814
- } ) ;
2815
- const arrowUpEvent = new KeyboardEvent ( 'keydown' , {
2816
- key : 'ArrowUp'
2817
- } ) ;
2818
- const aEvent = new KeyboardEvent ( 'keydown' , {
2819
- key : 'a'
2820
- } ) ;
2821
-
2822
2807
overlay . show ( overlay . attach ( SimpleDynamicComponent ) , overlaySettings ) ;
2823
2808
tick ( ) ;
2824
2809
2825
2810
let overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2826
- overlayWrapper . addEventListener ( 'keydown' , ( event : KeyboardEvent ) => {
2827
- if ( event . key === 'Escape' ) {
2828
- overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2829
- expect ( overlayWrapper ) . toBeFalsy ( ) ;
2830
- }
2831
- } ) ;
2811
+ expect ( overlayWrapper ) . toBeTruthy ( ) ;
2812
+
2813
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Enter' , document ) ;
2832
2814
tick ( ) ;
2815
+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2833
2816
expect ( overlayWrapper ) . toBeTruthy ( ) ;
2834
2817
2835
- document . dispatchEvent ( enterEvent ) ;
2836
- document . dispatchEvent ( aEvent ) ;
2837
- document . dispatchEvent ( arrowUpEvent ) ;
2838
- document . dispatchEvent ( escEvent ) ;
2818
+ UIInteractions . triggerKeyDownEvtUponElem ( 'a' , document ) ;
2819
+ tick ( ) ;
2820
+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2821
+ expect ( overlayWrapper ) . toBeTruthy ( ) ;
2822
+
2823
+ UIInteractions . triggerKeyDownEvtUponElem ( 'ArrowUp' , document ) ;
2824
+ tick ( ) ;
2825
+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2826
+ expect ( overlayWrapper ) . toBeTruthy ( ) ;
2827
+
2828
+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
2829
+ tick ( ) ;
2830
+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2831
+ expect ( overlayWrapper ) . toBeFalsy ( ) ;
2839
2832
} ) ) ;
2840
2833
2841
2834
// 3.2 Non - Modal
0 commit comments