|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 10 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 11 | +import {Component, signal} from '@angular/core'; |
| 12 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 13 | +import {MatButtonModule} from '../../button'; |
| 14 | +import {MATERIAL_ANIMATIONS, provideNativeDateAdapter} from '../../core'; |
| 15 | +import {MatDatepickerModule} from '../../datepicker'; |
| 16 | +import {MatDatepickerActionsHarness} from './datepicker-actions-harness'; |
| 17 | +import {MatDatepickerInputHarness} from './datepicker-input-harness'; |
| 18 | + |
| 19 | +describe('MatDatepickerActionsHarness', () => { |
| 20 | + let fixture: ComponentFixture<DatepickerActionsHarnessTest>; |
| 21 | + let loader: HarnessLoader; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + TestBed.configureTestingModule({ |
| 25 | + providers: [ |
| 26 | + provideNativeDateAdapter(), |
| 27 | + {provide: MATERIAL_ANIMATIONS, useValue: {animationsDisabled: true}}, |
| 28 | + ], |
| 29 | + }); |
| 30 | + |
| 31 | + fixture = TestBed.createComponent(DatepickerActionsHarnessTest); |
| 32 | + loader = TestbedHarnessEnvironment.documentRootLoader(fixture); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should load datepicker actions harness', async () => { |
| 36 | + const input = await loader.getHarness(MatDatepickerInputHarness); |
| 37 | + await input.openCalendar(); |
| 38 | + const actions = await loader.getAllHarnesses(MatDatepickerActionsHarness); |
| 39 | + expect(actions.length).toBe(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be able to apply', async () => { |
| 43 | + const input = await loader.getHarness(MatDatepickerInputHarness); |
| 44 | + await input.openCalendar(); |
| 45 | + expect(await input.isCalendarOpen()).toBe(true); |
| 46 | + const actions = await loader.getHarness(MatDatepickerActionsHarness); |
| 47 | + await actions.apply(); |
| 48 | + expect(await input.isCalendarOpen()).toBe(false); |
| 49 | + expect(fixture.componentInstance.applied()).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should be able to cancel', async () => { |
| 53 | + const input = await loader.getHarness(MatDatepickerInputHarness); |
| 54 | + await input.openCalendar(); |
| 55 | + expect(await input.isCalendarOpen()).toBe(true); |
| 56 | + const actions = await loader.getHarness(MatDatepickerActionsHarness); |
| 57 | + await actions.cancel(); |
| 58 | + expect(await input.isCalendarOpen()).toBe(false); |
| 59 | + expect(fixture.componentInstance.applied()).toBe(false); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +@Component({ |
| 64 | + template: ` |
| 65 | + <input [matDatepicker]="picker"> |
| 66 | + <mat-datepicker #picker> |
| 67 | + <mat-datepicker-actions> |
| 68 | + <button mat-button matDatepickerCancel>Cancel</button> |
| 69 | + <button mat-button matDatepickerApply (click)="applied.set(true)">Apply</button> |
| 70 | + </mat-datepicker-actions> |
| 71 | + </mat-datepicker> |
| 72 | + `, |
| 73 | + imports: [MatDatepickerModule, MatButtonModule], |
| 74 | +}) |
| 75 | +class DatepickerActionsHarnessTest { |
| 76 | + applied = signal(false); |
| 77 | +} |
0 commit comments