|
1 | 1 | import {ENTER, ESCAPE, RIGHT_ARROW} from '@angular/cdk/keycodes';
|
2 | 2 | import {OverlayContainer, Overlay, ScrollDispatcher} from '@angular/cdk/overlay';
|
3 |
| -import { |
4 |
| - createKeyboardEvent, |
5 |
| - dispatchEvent, |
6 |
| - dispatchFakeEvent, |
7 |
| - dispatchKeyboardEvent, |
8 |
| - dispatchMouseEvent, |
9 |
| -} from '@angular/cdk/testing'; |
| 3 | +import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing'; |
10 | 4 | import {Component, ViewChild} from '@angular/core';
|
11 | 5 | import {fakeAsync, flush, ComponentFixture, inject, TestBed} from '@angular/core/testing';
|
12 | 6 | import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
|
@@ -157,26 +151,18 @@ describe('MatDatepicker', () => {
|
157 | 151 | expect(parseInt(getComputedStyle(popup).height as string)).toBe(0);
|
158 | 152 | });
|
159 | 153 |
|
160 |
| - it('should close the popup when pressing ESCAPE', () => { |
| 154 | + it('should close the popup when pressing ESCAPE', fakeAsync(() => { |
161 | 155 | testComponent.datepicker.open();
|
162 | 156 | fixture.detectChanges();
|
163 | 157 |
|
164 |
| - let content = document.querySelector('.cdk-overlay-pane mat-datepicker-content')!; |
165 |
| - expect(content).toBeTruthy('Expected datepicker to be open.'); |
166 |
| - |
167 |
| - const keyboardEvent = createKeyboardEvent('keydown', ESCAPE); |
168 |
| - const stopPropagationSpy = spyOn(keyboardEvent, 'stopPropagation').and.callThrough(); |
| 158 | + expect(testComponent.datepicker.opened).toBe(true, 'Expected datepicker to be open.'); |
169 | 159 |
|
170 |
| - dispatchEvent(content, keyboardEvent); |
| 160 | + dispatchKeyboardEvent(document.body, 'keydown', ESCAPE); |
171 | 161 | fixture.detectChanges();
|
| 162 | + flush(); |
172 | 163 |
|
173 |
| - content = document.querySelector('.cdk-overlay-pane mat-datepicker-content')!; |
174 |
| - |
175 |
| - expect(content).toBeFalsy('Expected datepicker to be closed.'); |
176 |
| - expect(stopPropagationSpy).toHaveBeenCalled(); |
177 |
| - expect(keyboardEvent.defaultPrevented) |
178 |
| - .toBe(true, 'Expected default ESCAPE action to be prevented.'); |
179 |
| - }); |
| 164 | + expect(testComponent.datepicker.opened).toBe(false, 'Expected datepicker to be closed.'); |
| 165 | + })); |
180 | 166 |
|
181 | 167 | it('close should close dialog', fakeAsync(() => {
|
182 | 168 | testComponent.touch = true;
|
|
0 commit comments