11import { Component , signal , ViewChild , ChangeDetectionStrategy } from '@angular/core' ;
2- import { ComponentFixture , fakeAsync , flush , TestBed } from '@angular/core/testing' ;
2+ import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3+ import { MATERIAL_ANIMATIONS } from '../core' ;
34import { MatContextMenuTrigger } from './context-menu-trigger' ;
45import { MatMenu } from './menu' ;
56import { MatMenuItem } from './menu-item' ;
@@ -8,6 +9,10 @@ import {dispatchFakeEvent, dispatchMouseEvent} from '@angular/cdk/testing/privat
89describe ( 'context menu trigger' , ( ) => {
910 let fixture : ComponentFixture < ContextMenuTest > ;
1011
12+ function wait ( milliseconds : number ) {
13+ return new Promise ( resolve => setTimeout ( resolve , milliseconds ) ) ;
14+ }
15+
1116 function getTrigger ( ) : HTMLElement {
1217 return fixture . nativeElement . querySelector ( '.area' ) ;
1318 }
@@ -21,6 +26,9 @@ describe('context menu trigger', () => {
2126 }
2227
2328 beforeEach ( ( ) => {
29+ TestBed . configureTestingModule ( {
30+ providers : [ { provide : MATERIAL_ANIMATIONS , useValue : { animationsDisabled : true } } ] ,
31+ } ) ;
2432 fixture = TestBed . createComponent ( ContextMenuTest ) ;
2533 fixture . detectChanges ( ) ;
2634 } ) ;
@@ -32,16 +40,16 @@ describe('context menu trigger', () => {
3240 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
3341 } ) ;
3442
35- it ( 'should close the menu when clicking outside the trigger' , fakeAsync ( ( ) => {
43+ it ( 'should close the menu when clicking outside the trigger' , async ( ) => {
3644 dispatchMouseEvent ( getTrigger ( ) , 'contextmenu' , 10 , 10 ) ;
3745 fixture . detectChanges ( ) ;
3846 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
3947
4048 document . body . click ( ) ;
4149 fixture . detectChanges ( ) ;
42- flush ( ) ;
50+ await wait ( 50 ) ;
4351 expect ( getMenu ( ) ) . toBe ( null ) ;
44- } ) ) ;
52+ } ) ;
4553
4654 it ( 'should reposition the menu when right-clicking within the area' , ( ) => {
4755 dispatchMouseEvent ( getTrigger ( ) , 'contextmenu' , 10 , 10 ) ;
@@ -57,43 +65,43 @@ describe('context menu trigger', () => {
5765 expect ( menuRect . left ) . toBe ( 50 ) ;
5866 } ) ;
5967
60- it ( 'should ignore the first auxclick after opening' , fakeAsync ( ( ) => {
68+ it ( 'should ignore the first auxclick after opening' , async ( ) => {
6169 dispatchMouseEvent ( getTrigger ( ) , 'contextmenu' , 10 , 10 ) ;
6270 fixture . detectChanges ( ) ;
6371 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
6472
6573 dispatchMouseEvent ( document . body , 'auxclick' ) ;
6674 fixture . detectChanges ( ) ;
67- flush ( ) ;
75+ await wait ( 50 ) ;
6876 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
6977
7078 dispatchMouseEvent ( document . body , 'auxclick' ) ;
7179 fixture . detectChanges ( ) ;
72- flush ( ) ;
80+ await wait ( 50 ) ;
7381 expect ( getMenu ( ) ) . toBe ( null ) ;
74- } ) ) ;
82+ } ) ;
7583
76- it ( 'should close on `contextmenu` events outside the trigger' , fakeAsync ( ( ) => {
84+ it ( 'should close on `contextmenu` events outside the trigger' , async ( ) => {
7785 dispatchMouseEvent ( getTrigger ( ) , 'contextmenu' , 10 , 10 ) ;
7886 fixture . detectChanges ( ) ;
7987 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
8088
8189 dispatchMouseEvent ( document . body , 'contextmenu' ) ;
8290 fixture . detectChanges ( ) ;
83- flush ( ) ;
91+ await wait ( 50 ) ;
8492 expect ( getMenu ( ) ) . toBe ( null ) ;
85- } ) ) ;
93+ } ) ;
8694
87- it ( 'should not close on `contextmenu` events from inside the menu' , fakeAsync ( ( ) => {
95+ it ( 'should not close on `contextmenu` events from inside the menu' , async ( ) => {
8896 dispatchMouseEvent ( getTrigger ( ) , 'contextmenu' , 10 , 10 ) ;
8997 fixture . detectChanges ( ) ;
9098 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
9199
92100 dispatchMouseEvent ( getMenu ( ) ! , 'contextmenu' ) ;
93101 fixture . detectChanges ( ) ;
94- flush ( ) ;
102+ await wait ( 50 ) ;
95103 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
96- } ) ) ;
104+ } ) ;
97105
98106 it ( 'should set aria-controls on the trigger while the menu is open' , ( ) => {
99107 expect ( getTrigger ( ) . getAttribute ( 'aria-controls' ) ) . toBe ( null ) ;
@@ -124,7 +132,7 @@ describe('context menu trigger', () => {
124132 scroller . remove ( ) ;
125133 } ) ;
126134
127- it ( 'should emit events when the menu is opened and closed' , fakeAsync ( ( ) => {
135+ it ( 'should emit events when the menu is opened and closed' , async ( ) => {
128136 const { opened, closed} = fixture . componentInstance ;
129137 expect ( opened ) . toHaveBeenCalledTimes ( 0 ) ;
130138 expect ( closed ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -136,21 +144,21 @@ describe('context menu trigger', () => {
136144
137145 document . body . click ( ) ;
138146 fixture . detectChanges ( ) ;
139- flush ( ) ;
147+ await wait ( 50 ) ;
140148 expect ( opened ) . toHaveBeenCalledTimes ( 1 ) ;
141149 expect ( closed ) . toHaveBeenCalledTimes ( 1 ) ;
142- } ) ) ;
150+ } ) ;
143151
144- it ( 'should close the menu if the trigger is destroyed' , fakeAsync ( ( ) => {
152+ it ( 'should close the menu if the trigger is destroyed' , async ( ) => {
145153 dispatchMouseEvent ( getTrigger ( ) , 'contextmenu' , 10 , 10 ) ;
146154 fixture . detectChanges ( ) ;
147155 expect ( getMenu ( ) ) . toBeTruthy ( ) ;
148156
149157 fixture . componentInstance . showTrigger . set ( false ) ;
150158 fixture . detectChanges ( ) ;
151- flush ( ) ;
159+ await wait ( 50 ) ;
152160 expect ( getMenu ( ) ) . toBe ( null ) ;
153- } ) ) ;
161+ } ) ;
154162
155163 it ( 'should not open when clicking on a disabled context menu trigger' , ( ) => {
156164 fixture . componentInstance . disabled . set ( true ) ;
0 commit comments