99 ComponentRef
1010} from '@angular/core' ;
1111import { TestBed , fakeAsync , tick , async , inject } from '@angular/core/testing' ;
12- import { BrowserModule } from '@angular/platform-browser' ;
12+ import { BrowserModule , By } from '@angular/platform-browser' ;
1313import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
1414import { IgxOverlayService } from './overlay' ;
1515import { IgxToggleDirective , IgxToggleModule , IgxOverlayOutletDirective } from './../../directives/toggle/toggle.directive' ;
@@ -2718,61 +2718,43 @@ describe('igxOverlay', () => {
27182718 const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
27192719 const overlay = fixture . componentInstance . overlay ;
27202720 const overlaySettings : OverlaySettings = {
2721- modal : true ,
27222721 closeOnEsc : true ,
2723- positionStrategy : new GlobalPositionStrategy ( )
27242722 } ;
27252723
2726- const targetButton = 'Escape' ;
2727- const escEvent = new KeyboardEvent ( 'keydown' , {
2728- key : targetButton
2729- } ) ;
2730-
27312724 overlay . show ( overlay . attach ( SimpleDynamicComponent ) , overlaySettings ) ;
27322725 tick ( ) ;
27332726
27342727 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 ( ) ;
27422728 expect ( overlayWrapper ) . toBeTruthy ( ) ;
2743- document . dispatchEvent ( escEvent ) ;
2729+
2730+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
27442731 tick ( ) ;
2732+
2733+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
2734+ expect ( overlayWrapper ) . toBeFalsy ( ) ;
27452735 } ) ) ;
27462736
27472737 it ( 'Should not close the component when esc key is pressed and closeOnEsc is false' , fakeAsync ( ( ) => {
27482738 const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
27492739 const overlay = fixture . componentInstance . overlay ;
27502740 const overlaySettings : OverlaySettings = {
2751- modal : true ,
2752- positionStrategy : new GlobalPositionStrategy ( )
2741+ closeOnEsc : false
27532742 } ;
2754- const targetButton = 'Escape' ;
2755- const escEvent = new KeyboardEvent ( 'keydown' , {
2756- key : targetButton
2757- } ) ;
27582743
27592744 overlay . show ( overlay . attach ( SimpleDynamicComponent ) , overlaySettings ) ;
27602745 tick ( ) ;
27612746
27622747 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 ) ;
27692751 tick ( ) ;
2770- fixture . detectChanges ( ) ;
27712752
2753+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
27722754 expect ( overlayWrapper ) . toBeTruthy ( ) ;
27732755 } ) ) ;
27742756
2775- it ( 'Should close the opened overlays consecutively on escape keypress' , fakeAsync ( ( ) => {
2757+ it ( 'Should close the opened overlays consecutively on esc keypress' , fakeAsync ( ( ) => {
27762758 const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
27772759 const overlay = fixture . componentInstance . overlay ;
27782760 overlay . show ( overlay . attach ( SimpleDynamicComponent ) , { closeOnEsc : true } ) ;
@@ -2783,59 +2765,70 @@ describe('igxOverlay', () => {
27832765 const overlayDiv = document . getElementsByClassName ( CLASS_OVERLAY_MAIN ) [ 0 ] ;
27842766 expect ( overlayDiv . children . length ) . toBe ( 2 ) ;
27852767
2786- const escEvent = new KeyboardEvent ( 'keydown' , {
2787- key : 'Escape'
2788- } ) ;
2789-
2790- document . dispatchEvent ( escEvent ) ;
2768+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
27912769 tick ( ) ;
27922770 expect ( overlayDiv . children . length ) . toBe ( 1 ) ;
27932771
2794- document . dispatchEvent ( escEvent ) ;
2772+ UIInteractions . triggerKeyDownEvtUponElem ( 'Escape' , document ) ;
27952773 tick ( ) ;
27962774 expect ( overlayDiv . children . length ) . toBe ( 0 ) ;
27972775 } ) ) ;
27982776
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+
27992799 // Test #1883 #1820
28002800 it ( 'It should close the component when esc key is pressed and there were other keys pressed prior to esc.' , fakeAsync ( ( ) => {
28012801 const fixture = TestBed . createComponent ( EmptyPageComponent ) ;
28022802 const overlay = fixture . componentInstance . overlay ;
28032803 const overlaySettings : OverlaySettings = {
2804- modal : true ,
28052804 closeOnEsc : true ,
2806- positionStrategy : new GlobalPositionStrategy ( )
28072805 } ;
28082806
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-
28222807 overlay . show ( overlay . attach ( SimpleDynamicComponent ) , overlaySettings ) ;
28232808 tick ( ) ;
28242809
28252810 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 ) ;
28322814 tick ( ) ;
2815+ overlayWrapper = document . getElementsByClassName ( CLASS_OVERLAY_WRAPPER_MODAL ) [ 0 ] ;
28332816 expect ( overlayWrapper ) . toBeTruthy ( ) ;
28342817
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 ( ) ;
28392832 } ) ) ;
28402833
28412834 // 3.2 Non - Modal
0 commit comments