|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { |
| 3 | + ComponentFixture, |
| 4 | + TestBed, |
| 5 | + fakeAsync, |
| 6 | + tick, |
| 7 | +} from '@angular/core/testing'; |
| 8 | +import { |
| 9 | + ReactiveFormsModule, |
| 10 | + FormControl, |
| 11 | + Validators, |
| 12 | + FormGroup, |
| 13 | + ValidatorFn, |
| 14 | + AbstractControl, |
| 15 | + AsyncValidatorFn, |
| 16 | +} from '@angular/forms'; |
| 17 | +import { By } from '@angular/platform-browser'; |
| 18 | +import { TagsInputComponent } from './tags-input.component'; |
| 19 | +import { of } from 'rxjs'; |
| 20 | + |
| 21 | +describe('TagsInputComponent Required Validation Behavior', () => { |
| 22 | + let fixture: ComponentFixture<TestFormComponent>; |
| 23 | + let testHost: TestFormComponent; |
| 24 | + let inputEl: HTMLInputElement; |
| 25 | + let tagComp: TagsInputComponent; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + fixture = TestBed.createComponent(TestFormComponent); |
| 29 | + fixture.detectChanges(); |
| 30 | + tagComp = fixture.debugElement.query( |
| 31 | + By.directive(TagsInputComponent), |
| 32 | + ).componentInstance; |
| 33 | + testHost = fixture.componentInstance; |
| 34 | + const el = fixture.debugElement.query( |
| 35 | + By.css('.aui-tags-input'), |
| 36 | + ).nativeElement; |
| 37 | + inputEl = el.querySelector('input'); |
| 38 | + }); |
| 39 | + |
| 40 | + // it('should mark form valid when input has value but tag is not confirmed yet', fakeAsync(() => { |
| 41 | + // expect(testHost.form.valid).toBeFalsy(); |
| 42 | + |
| 43 | + // inputEl.value = 'hello'; |
| 44 | + // inputEl.dispatchEvent(new Event('input')); |
| 45 | + // fixture.detectChanges(); |
| 46 | + // tick(); |
| 47 | + // fixture.detectChanges(); |
| 48 | + |
| 49 | + // expect(testHost.form.valid).toBeTruthy(); |
| 50 | + |
| 51 | + // inputEl.dispatchEvent(new Event('blur')); |
| 52 | + // fixture.detectChanges(); |
| 53 | + // tick(); |
| 54 | + // fixture.detectChanges(); |
| 55 | + |
| 56 | + // expect(testHost.form.valid).toBeTruthy(); |
| 57 | + // expect(testHost.form.get('tags')!.value).toEqual(['hello']); |
| 58 | + // })); |
| 59 | + |
| 60 | + describe('allowRepeat Behavior', () => { |
| 61 | + it('should NOT allow duplicate tags when allowRepeat = false', fakeAsync(() => { |
| 62 | + testHost.allowRepeat = false; |
| 63 | + fixture.detectChanges(); |
| 64 | + |
| 65 | + inputEl.value = 'a'; |
| 66 | + inputEl.dispatchEvent(new Event('input')); |
| 67 | + inputEl.dispatchEvent(new Event('blur')); |
| 68 | + tick(); |
| 69 | + fixture.detectChanges(); |
| 70 | + |
| 71 | + inputEl.value = 'a'; |
| 72 | + inputEl.dispatchEvent(new Event('input')); |
| 73 | + inputEl.dispatchEvent(new Event('blur')); |
| 74 | + tick(); |
| 75 | + fixture.detectChanges(); |
| 76 | + |
| 77 | + expect(testHost.form.get('tags')!.value).toEqual(['a']); |
| 78 | + })); |
| 79 | + |
| 80 | + it('should allow duplicate tags when allowRepeat = true', fakeAsync(() => { |
| 81 | + testHost.allowRepeat = true; |
| 82 | + fixture.detectChanges(); |
| 83 | + |
| 84 | + inputEl.value = 'a'; |
| 85 | + inputEl.dispatchEvent(new Event('input')); |
| 86 | + inputEl.dispatchEvent(new Event('blur')); |
| 87 | + tick(); |
| 88 | + fixture.detectChanges(); |
| 89 | + |
| 90 | + inputEl.value = 'a'; |
| 91 | + inputEl.dispatchEvent(new Event('input')); |
| 92 | + inputEl.dispatchEvent(new Event('blur')); |
| 93 | + tick(); |
| 94 | + fixture.detectChanges(); |
| 95 | + |
| 96 | + expect(testHost.form.get('tags')!.value).toEqual(['a', 'a']); |
| 97 | + })); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('allowEmpty Behavior', () => { |
| 101 | + it('should NOT add empty tag when allowEmpty = false', fakeAsync(() => { |
| 102 | + testHost.allowEmpty = false; |
| 103 | + fixture.detectChanges(); |
| 104 | + |
| 105 | + inputEl.value = ''; |
| 106 | + inputEl.dispatchEvent(new Event('input')); |
| 107 | + inputEl.dispatchEvent(new Event('blur')); |
| 108 | + tick(); |
| 109 | + fixture.detectChanges(); |
| 110 | + |
| 111 | + expect(testHost.form.get('tags')!.value).toEqual([]); |
| 112 | + })); |
| 113 | + |
| 114 | + it('should add empty tag when allowEmpty = true', fakeAsync(() => { |
| 115 | + testHost.allowEmpty = true; |
| 116 | + fixture.detectChanges(); |
| 117 | + |
| 118 | + inputEl.value = ''; |
| 119 | + inputEl.dispatchEvent(new Event('input')); |
| 120 | + inputEl.dispatchEvent(new Event('blur')); |
| 121 | + tick(); |
| 122 | + fixture.detectChanges(); |
| 123 | + |
| 124 | + expect(testHost.form.get('tags')!.value).toEqual(['']); |
| 125 | + })); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('inputValidator behavior', () => { |
| 129 | + it('should NOT add tag when input does NOT pass inputValidator', fakeAsync(() => { |
| 130 | + testHost.checkFn = control => { |
| 131 | + const value = control.value as string[]; |
| 132 | + if (value.includes('a')) { |
| 133 | + return { patternB: true }; |
| 134 | + } |
| 135 | + return null; |
| 136 | + }; |
| 137 | + fixture.detectChanges(); |
| 138 | + |
| 139 | + inputEl.value = 'apple'; |
| 140 | + inputEl.dispatchEvent(new Event('input')); |
| 141 | + inputEl.dispatchEvent(new Event('blur')); |
| 142 | + tick(0); |
| 143 | + fixture.detectChanges(); |
| 144 | + |
| 145 | + expect(tagComp.model).toEqual([]); |
| 146 | + expect(testHost.form.get('tags')!.value).toEqual([]); |
| 147 | + expect(testHost.form.valid).toBeFalsy(); |
| 148 | + })); |
| 149 | + |
| 150 | + it('should add tag when input passes inputValidator', fakeAsync(() => { |
| 151 | + testHost.checkFn = control => { |
| 152 | + const value = control.value as string[]; |
| 153 | + if (value.includes('a')) { |
| 154 | + return { patternB: true }; |
| 155 | + } |
| 156 | + return null; |
| 157 | + }; |
| 158 | + inputEl.value = 'ccc'; |
| 159 | + inputEl.dispatchEvent(new Event('input')); |
| 160 | + inputEl.dispatchEvent(new Event('blur')); |
| 161 | + tick(0); |
| 162 | + fixture.detectChanges(); |
| 163 | + |
| 164 | + expect(tagComp.model).toEqual(['ccc']); |
| 165 | + expect(testHost.form.get('tags')!.value).toEqual(['ccc']); |
| 166 | + expect(testHost.form.valid).toBeTruthy(); |
| 167 | + })); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('inputAsyncValidator behavior', () => { |
| 171 | + it('should block adding tag when async validator resolves to false', fakeAsync(() => { |
| 172 | + testHost.inputAsyncValidator = (_control: AbstractControl) => |
| 173 | + of({ tagAsyncInvalid: true }); |
| 174 | + fixture.detectChanges(); |
| 175 | + |
| 176 | + inputEl.value = 'bad'; |
| 177 | + inputEl.dispatchEvent(new Event('input')); |
| 178 | + inputEl.dispatchEvent(new Event('blur')); |
| 179 | + |
| 180 | + tick(); |
| 181 | + fixture.detectChanges(); |
| 182 | + |
| 183 | + expect(testHost.form.get('tags')!.value).toEqual([]); |
| 184 | + })); |
| 185 | + |
| 186 | + it('should allow adding tag when async validator resolves to true', fakeAsync(() => { |
| 187 | + testHost.inputAsyncValidator = (_control: AbstractControl) => |
| 188 | + Promise.resolve(null); |
| 189 | + fixture.detectChanges(); |
| 190 | + |
| 191 | + inputEl.value = 'hello'; |
| 192 | + inputEl.dispatchEvent(new Event('input')); |
| 193 | + inputEl.dispatchEvent(new Event('blur')); |
| 194 | + |
| 195 | + tick(); |
| 196 | + fixture.detectChanges(); |
| 197 | + |
| 198 | + expect(testHost.form.get('tags')!.value).toEqual(['hello']); |
| 199 | + })); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
| 203 | +@Component({ |
| 204 | + template: ` |
| 205 | + <form [formGroup]="form"> |
| 206 | + <aui-tags-input |
| 207 | + required |
| 208 | + formControlName="tags" |
| 209 | + [allowRepeat]="allowRepeat" |
| 210 | + [allowEmpty]="allowEmpty" |
| 211 | + [inputValidator]="checkFn" |
| 212 | + [inputAsyncValidator]="inputAsyncValidator" |
| 213 | + ></aui-tags-input> |
| 214 | + </form> |
| 215 | + `, |
| 216 | + imports: [ReactiveFormsModule, TagsInputComponent], |
| 217 | +}) |
| 218 | +class TestFormComponent { |
| 219 | + allowRepeat = false; |
| 220 | + allowEmpty = false; |
| 221 | + |
| 222 | + inputAsyncValidator: AsyncValidatorFn = (_control: AbstractControl) => |
| 223 | + Promise.resolve(null); |
| 224 | + |
| 225 | + checkFn: ValidatorFn = () => { |
| 226 | + return null; |
| 227 | + }; |
| 228 | + |
| 229 | + form = new FormGroup({ |
| 230 | + tags: new FormControl<string[]>([], Validators.required), |
| 231 | + }); |
| 232 | +} |
0 commit comments