|
| 1 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {Component, signal} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 6 | +import {DateAdapter, provideNativeDateAdapter} from '@angular/material/core'; |
| 7 | +import {MatTimepicker, MatTimepickerInput} from '@angular/material/timepicker'; |
| 8 | +import {MatTimepickerHarness} from './timepicker-harness'; |
| 9 | +import {MatTimepickerInputHarness} from './timepicker-input-harness'; |
| 10 | + |
| 11 | +describe('MatTimepickerInputHarness', () => { |
| 12 | + let fixture: ComponentFixture<TimepickerInputHarnessTest>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + let adapter: DateAdapter<Date>; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + TestBed.configureTestingModule({ |
| 18 | + providers: [provideNativeDateAdapter()], |
| 19 | + imports: [NoopAnimationsModule, TimepickerInputHarnessTest], |
| 20 | + }); |
| 21 | + |
| 22 | + adapter = TestBed.inject(DateAdapter); |
| 23 | + adapter.setLocale('en-US'); |
| 24 | + fixture = TestBed.createComponent(TimepickerInputHarnessTest); |
| 25 | + fixture.detectChanges(); |
| 26 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should load all timepicker input harnesses', async () => { |
| 30 | + const inputs = await loader.getAllHarnesses(MatTimepickerInputHarness); |
| 31 | + expect(inputs.length).toBe(2); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should filter inputs based on their value', async () => { |
| 35 | + fixture.componentInstance.value.set(createTime(15, 10)); |
| 36 | + fixture.changeDetectorRef.markForCheck(); |
| 37 | + const inputs = await loader.getAllHarnesses(MatTimepickerInputHarness.with({value: /3:10/})); |
| 38 | + expect(inputs.length).toBe(1); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should filter inputs based on their placeholder', async () => { |
| 42 | + const inputs = await loader.getAllHarnesses( |
| 43 | + MatTimepickerInputHarness.with({ |
| 44 | + placeholder: /^Pick/, |
| 45 | + }), |
| 46 | + ); |
| 47 | + |
| 48 | + expect(inputs.length).toBe(1); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should get whether the input is disabled', async () => { |
| 52 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 53 | + expect(await input.isDisabled()).toBe(false); |
| 54 | + |
| 55 | + fixture.componentInstance.disabled.set(true); |
| 56 | + expect(await input.isDisabled()).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should get whether the input is required', async () => { |
| 60 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 61 | + expect(await input.isRequired()).toBe(false); |
| 62 | + |
| 63 | + fixture.componentInstance.required.set(true); |
| 64 | + expect(await input.isRequired()).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should get the input value', async () => { |
| 68 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 69 | + fixture.componentInstance.value.set(createTime(15, 10)); |
| 70 | + fixture.changeDetectorRef.markForCheck(); |
| 71 | + |
| 72 | + expect(await input.getValue()).toBe('3:10 PM'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should set the input value', async () => { |
| 76 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 77 | + expect(await input.getValue()).toBeFalsy(); |
| 78 | + |
| 79 | + await input.setValue('3:10 PM'); |
| 80 | + expect(await input.getValue()).toBe('3:10 PM'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should set the input value based on date adapter validation and formatting', async () => { |
| 84 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 85 | + const validValues: any[] = [createTime(15, 10), '', 0, false]; |
| 86 | + const invalidValues: any[] = [null, undefined]; |
| 87 | + spyOn(adapter, 'format').and.returnValue('FORMATTED_VALUE'); |
| 88 | + spyOn(adapter, 'isValid').and.callFake(value => validValues.includes(value)); |
| 89 | + spyOn(adapter, 'deserialize').and.callFake(value => |
| 90 | + validValues.includes(value) ? value : null, |
| 91 | + ); |
| 92 | + spyOn(adapter, 'getValidDateOrNull').and.callFake((value: Date) => |
| 93 | + adapter.isValid(value) ? value : null, |
| 94 | + ); |
| 95 | + |
| 96 | + for (let value of validValues) { |
| 97 | + fixture.componentInstance.value.set(value); |
| 98 | + fixture.changeDetectorRef.markForCheck(); |
| 99 | + expect(await input.getValue()).toBe('FORMATTED_VALUE'); |
| 100 | + } |
| 101 | + |
| 102 | + for (let value of invalidValues) { |
| 103 | + fixture.componentInstance.value.set(value); |
| 104 | + fixture.changeDetectorRef.markForCheck(); |
| 105 | + expect(await input.getValue()).toBe(''); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('should get the input placeholder', async () => { |
| 110 | + const inputs = await loader.getAllHarnesses(MatTimepickerInputHarness); |
| 111 | + expect(await parallel(() => inputs.map(input => input.getPlaceholder()))).toEqual([ |
| 112 | + 'Pick a time', |
| 113 | + 'Select a time', |
| 114 | + ]); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should be able to change the input focused state', async () => { |
| 118 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 119 | + expect(await input.isFocused()).toBe(false); |
| 120 | + |
| 121 | + await input.focus(); |
| 122 | + expect(await input.isFocused()).toBe(true); |
| 123 | + |
| 124 | + await input.blur(); |
| 125 | + expect(await input.isFocused()).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should be able to open and close a timepicker', async () => { |
| 129 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 130 | + expect(await input.isTimepickerOpen()).toBe(false); |
| 131 | + |
| 132 | + await input.openTimepicker(); |
| 133 | + expect(await input.isTimepickerOpen()).toBe(true); |
| 134 | + |
| 135 | + await input.closeTimepicker(); |
| 136 | + expect(await input.isTimepickerOpen()).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should be able to get the harness for the associated timepicker', async () => { |
| 140 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 141 | + await input.openTimepicker(); |
| 142 | + expect(await input.getTimepicker()).toBeInstanceOf(MatTimepickerHarness); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should emit the `valueChange` event when the value is changed', async () => { |
| 146 | + const input = await loader.getHarness(MatTimepickerInputHarness.with({selector: '#bound'})); |
| 147 | + expect(fixture.componentInstance.changeCount).toBe(0); |
| 148 | + |
| 149 | + await input.setValue('3:15 PM'); |
| 150 | + expect(fixture.componentInstance.changeCount).toBeGreaterThan(0); |
| 151 | + }); |
| 152 | + |
| 153 | + function createTime(hours: number, minutes: number): Date { |
| 154 | + return adapter.setTime(adapter.today(), hours, minutes, 0); |
| 155 | + } |
| 156 | +}); |
| 157 | + |
| 158 | +@Component({ |
| 159 | + template: ` |
| 160 | + <input |
| 161 | + [matTimepicker]="boundPicker" |
| 162 | + [value]="value()" |
| 163 | + [disabled]="disabled()" |
| 164 | + [required]="required()" |
| 165 | + (valueChange)="changeCount = changeCount + 1" |
| 166 | + placeholder="Pick a time" |
| 167 | + id="bound"> |
| 168 | + <mat-timepicker #boundPicker/> |
| 169 | +
|
| 170 | + <input [matTimepicker]="basicPicker" id="basic" placeholder="Select a time"> |
| 171 | + <mat-timepicker #basicPicker/> |
| 172 | + `, |
| 173 | + standalone: true, |
| 174 | + imports: [MatTimepickerInput, MatTimepicker], |
| 175 | +}) |
| 176 | +class TimepickerInputHarnessTest { |
| 177 | + readonly value = signal<Date | null>(null); |
| 178 | + readonly disabled = signal(false); |
| 179 | + readonly required = signal(false); |
| 180 | + changeCount = 0; |
| 181 | +} |
0 commit comments