|
1 | 1 | import {ENTER, ESCAPE, RIGHT_ARROW} from '@angular/cdk/keycodes';
|
2 |
| -import {OverlayContainer} from '@angular/cdk/overlay'; |
| 2 | +import {OverlayContainer, Overlay, ScrollDispatcher} from '@angular/cdk/overlay'; |
3 | 3 | import {
|
4 | 4 | createKeyboardEvent,
|
5 | 5 | dispatchEvent,
|
@@ -27,7 +27,8 @@ import {MatInputModule} from '../input/index';
|
27 | 27 | import {MatDatepicker} from './datepicker';
|
28 | 28 | import {MatDatepickerInput} from './datepicker-input';
|
29 | 29 | import {MatDatepickerToggle} from './datepicker-toggle';
|
30 |
| -import {MatDatepickerIntl, MatDatepickerModule} from './index'; |
| 30 | +import {MatDatepickerIntl, MatDatepickerModule, MAT_DATEPICKER_SCROLL_STRATEGY} from './index'; |
| 31 | +import {Subject} from 'rxjs/Subject'; |
31 | 32 |
|
32 | 33 | describe('MatDatepicker', () => {
|
33 | 34 | const SUPPORTS_INTL = typeof Intl != 'undefined';
|
@@ -342,17 +343,55 @@ describe('MatDatepicker', () => {
|
342 | 343 | testComponent.datepicker.open();
|
343 | 344 | fixture.detectChanges();
|
344 | 345 |
|
345 |
| - spyOn(testComponent.datepicker, 'close').and.callThrough(); |
346 |
| - |
| 346 | + const spy = jasmine.createSpy('close event spy'); |
| 347 | + const subscription = testComponent.datepicker.closedStream.subscribe(spy); |
347 | 348 | const backdrop = document.querySelector('.cdk-overlay-backdrop')! as HTMLElement;
|
348 | 349 |
|
349 | 350 | backdrop.click();
|
350 | 351 | fixture.detectChanges();
|
351 | 352 | flush();
|
352 | 353 |
|
353 |
| - expect(testComponent.datepicker.close).toHaveBeenCalledTimes(1); |
| 354 | + expect(spy).toHaveBeenCalledTimes(1); |
354 | 355 | expect(testComponent.datepicker.opened).toBe(false);
|
| 356 | + subscription.unsubscribe(); |
355 | 357 | }));
|
| 358 | + |
| 359 | + it('should reset the datepicker when it is closed externally', |
| 360 | + fakeAsync(inject([OverlayContainer], (oldOverlayContainer: OverlayContainer) => { |
| 361 | + |
| 362 | + // Destroy the old container manually since resetting the testing module won't do it. |
| 363 | + oldOverlayContainer.ngOnDestroy(); |
| 364 | + TestBed.resetTestingModule(); |
| 365 | + |
| 366 | + const scrolledSubject = new Subject(); |
| 367 | + |
| 368 | + // Stub out a `CloseScrollStrategy` so we can trigger a detachment via the `OverlayRef`. |
| 369 | + fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [ |
| 370 | + { |
| 371 | + provide: ScrollDispatcher, |
| 372 | + useValue: {scrolled: () => scrolledSubject} |
| 373 | + }, |
| 374 | + { |
| 375 | + provide: MAT_DATEPICKER_SCROLL_STRATEGY, |
| 376 | + deps: [Overlay], |
| 377 | + useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close() |
| 378 | + } |
| 379 | + ]); |
| 380 | + |
| 381 | + fixture.detectChanges(); |
| 382 | + testComponent = fixture.componentInstance; |
| 383 | + |
| 384 | + testComponent.datepicker.open(); |
| 385 | + fixture.detectChanges(); |
| 386 | + |
| 387 | + expect(testComponent.datepicker.opened).toBe(true); |
| 388 | + |
| 389 | + scrolledSubject.next(); |
| 390 | + flush(); |
| 391 | + fixture.detectChanges(); |
| 392 | + |
| 393 | + expect(testComponent.datepicker.opened).toBe(false); |
| 394 | + }))); |
356 | 395 | });
|
357 | 396 |
|
358 | 397 | describe('datepicker with too many inputs', () => {
|
|
0 commit comments