|
1 | | -import { Component } from '@angular/core'; |
2 | | -import { |
3 | | - ControlValueAccessor, |
4 | | - FormControl, |
5 | | - FormsModule, |
6 | | - ReactiveFormsModule, |
7 | | -} from '@angular/forms'; |
| 1 | +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; |
8 | 2 | import { runValueAccessorTests } from 'ngx-cva-test-suite'; |
9 | | -import { NgxMaskDirective, provideEnvironmentNgxMask } from 'ngx-mask'; |
| 3 | +import { Component, ElementRef, ViewChild } from '@angular/core'; |
| 4 | +import { NgxMaskDirective, provideNgxMask } from 'ngx-mask'; |
10 | 5 |
|
11 | 6 | @Component({ |
12 | | - imports: [NgxMaskDirective, FormsModule, ReactiveFormsModule], |
13 | | - template: ` |
14 | | - <input |
15 | | - class="text-align-right" |
16 | | - mask="Hh:m0" |
17 | | - [dropSpecialCharacters]="false" |
18 | | - [showMaskTyped]="true" |
19 | | - [leadZeroDateTime]="true" |
20 | | - [formControl]="innerFormControl" |
21 | | - (blur)="onTouched()" /> |
22 | | - `, |
| 7 | + template: `<input |
| 8 | + mask="Hh:m0" |
| 9 | + [dropSpecialCharacters]="false" |
| 10 | + [showMaskTyped]="true" |
| 11 | + [leadZeroDateTime]="true" |
| 12 | + #ctrl />`, |
| 13 | + imports: [NgxMaskDirective], |
23 | 14 | }) |
24 | | -class TestComponent implements ControlValueAccessor { |
25 | | - innerFormControl = new FormControl(''); |
26 | | - private _onChange: null | ((value: string) => void) = null; |
27 | | - private _onTouched: null | (() => void) = null; |
| 15 | +export class CVAWrapperComponent { |
| 16 | + @ViewChild(NgxMaskDirective) defaultCtrl!: NgxMaskDirective; |
| 17 | + @ViewChild('ctrl') readonly ctrl!: ElementRef<HTMLInputElement>; |
28 | 18 |
|
29 | | - registerOnChange(onChange: (value: string) => void): void { |
30 | | - this._onChange = onChange; |
31 | | - } |
32 | | - |
33 | | - registerOnTouched(onTouched: () => void): void { |
34 | | - this._onTouched = onTouched; |
35 | | - } |
36 | | - |
37 | | - onChange(value: string): void { |
38 | | - if (this._onChange) { |
39 | | - this._onChange(value); |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - onTouched(): void { |
44 | | - if (this._onTouched) { |
45 | | - this._onTouched(); |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - setDisabledState(isDisabled: boolean): void { |
50 | | - if (isDisabled) this.innerFormControl.disable({ emitEvent: false }); |
51 | | - else this.innerFormControl.enable({ emitEvent: false }); |
52 | | - } |
53 | | - |
54 | | - get disabled() { |
55 | | - return this.innerFormControl.disabled; |
56 | | - } |
57 | | - |
58 | | - writeValue(value: string): void { |
59 | | - this.innerFormControl.setValue(value, { emitEvent: false }); |
60 | | - } |
| 19 | + get value(): string { |
| 20 | + return this.ctrl?.nativeElement.value; |
| 21 | + } |
61 | 22 | } |
62 | 23 |
|
63 | | -describe('CVA', () => { |
64 | | - runValueAccessorTests({ |
65 | | - /** Component, that is being tested */ |
66 | | - component: TestComponent, |
67 | | - |
68 | | - /** |
69 | | - * All the metadata required for this test to run. |
70 | | - * Under the hood calls TestBed.configureTestingModule with provided config. |
71 | | - */ |
72 | | - testModuleMetadata: { |
73 | | - imports: [ReactiveFormsModule, FormsModule], |
74 | | - providers: [provideEnvironmentNgxMask()], |
75 | | - }, |
76 | | - /** Whether component is able to track "onBlur" events separately */ |
77 | | - supportsOnBlur: true, |
78 | | - /** |
79 | | - * CSS selector for the element, that should dispatch `blur` event. |
80 | | - * Required and used only if `supportsOnBlur` is set to true. |
81 | | - */ |
82 | | - nativeControlSelector: 'input', |
83 | | - /** |
84 | | - * Tests the correctness of an approach that is used to set value in the component, |
85 | | - * when the change is internal. It's optional and can be omitted by passing "null" |
86 | | - */ |
87 | | - internalValueChangeSetter: null, |
88 | | - /** Function to get the value of a component in a runtime. */ |
89 | | - getComponentValue: (fixture) => fixture.componentInstance.innerFormControl.value, |
90 | | - resetCustomValue: { value: null }, |
91 | | - getValues: () => ['20:00', '10:00', null], |
92 | | - }); |
| 24 | +describe('TimeFieldFormComponent', () => { |
| 25 | + runValueAccessorTests<NgxMaskDirective, CVAWrapperComponent>({ |
| 26 | + component: NgxMaskDirective, |
| 27 | + testModuleMetadata: { |
| 28 | + providers: [provideNgxMask()], |
| 29 | + imports: [FormsModule, ReactiveFormsModule], |
| 30 | + }, |
| 31 | + hostTemplate: { |
| 32 | + hostComponent: CVAWrapperComponent, |
| 33 | + getTestingComponent: fixture => fixture.componentInstance.defaultCtrl, |
| 34 | + }, |
| 35 | + supportsOnBlur: true, |
| 36 | + nativeControlSelector: 'input[mask]', |
| 37 | + internalValueChangeSetter: null, |
| 38 | + getComponentValue: null, |
| 39 | + resetCustomValue: { value: '__:__' }, |
| 40 | + getValues: () => ['10:00', '20:00', null], |
| 41 | + }); |
93 | 42 | }); |
0 commit comments