|
| 1 | +import { ChangeDetectionStrategy, Component, signal } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; |
| 4 | +import { FormComponent } from './form.component'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'lu-form-host', |
| 8 | + imports: [FormComponent, ReactiveFormsModule], |
| 9 | + template: ` |
| 10 | + <form luForm [formGroup]="formGroup" [focusInvalidOnSubmit]="focusInvalidOnSubmit()" (submit)="onSubmit()"> |
| 11 | + <input id="name" formControlName="name" /> |
| 12 | + <input id="email" formControlName="email" /> |
| 13 | + <button id="save" type="submit">Save</button> |
| 14 | + </form> |
| 15 | + `, |
| 16 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 17 | +}) |
| 18 | +class FormHost { |
| 19 | + readonly focusInvalidOnSubmit = signal(true); |
| 20 | + |
| 21 | + readonly formGroup = new FormGroup({ |
| 22 | + name: new FormControl('', Validators.required), |
| 23 | + email: new FormControl('', Validators.required), |
| 24 | + }); |
| 25 | + |
| 26 | + onSubmit(): void { |
| 27 | + this.formGroup.markAllAsTouched(); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +describe(FormComponent.name, () => { |
| 32 | + function createHost() { |
| 33 | + TestBed.configureTestingModule({ imports: [FormHost] }); |
| 34 | + const fixture = TestBed.createComponent(FormHost); |
| 35 | + fixture.detectChanges(); |
| 36 | + const form = (fixture.nativeElement as HTMLElement).querySelector<HTMLFormElement>('form')!; |
| 37 | + document.body.appendChild(fixture.nativeElement); |
| 38 | + return { fixture, form }; |
| 39 | + } |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + document.body.innerHTML = ''; |
| 43 | + }); |
| 44 | + |
| 45 | + const flushFocusTimeout = () => new Promise((resolve) => setTimeout(resolve)); |
| 46 | + |
| 47 | + it('should focus the first invalid field when submitting an invalid form', async () => { |
| 48 | + const { form } = createHost(); |
| 49 | + |
| 50 | + form.dispatchEvent(new Event('submit')); |
| 51 | + await flushFocusTimeout(); |
| 52 | + |
| 53 | + expect(document.activeElement?.id).toBe('name'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not move focus when the form is valid', async () => { |
| 57 | + const { fixture, form } = createHost(); |
| 58 | + fixture.componentInstance.formGroup.setValue({ name: 'a', email: 'b' }); |
| 59 | + fixture.detectChanges(); |
| 60 | + const initialActiveElement = document.activeElement; |
| 61 | + |
| 62 | + form.dispatchEvent(new Event('submit')); |
| 63 | + await flushFocusTimeout(); |
| 64 | + |
| 65 | + expect(document.activeElement).toBe(initialActiveElement); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not move focus when focusInvalidOnSubmit is false', async () => { |
| 69 | + const { fixture, form } = createHost(); |
| 70 | + fixture.componentInstance.focusInvalidOnSubmit.set(false); |
| 71 | + fixture.detectChanges(); |
| 72 | + const initialActiveElement = document.activeElement; |
| 73 | + |
| 74 | + form.dispatchEvent(new Event('submit')); |
| 75 | + await flushFocusTimeout(); |
| 76 | + |
| 77 | + expect(document.activeElement).toBe(initialActiveElement); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should focus the first invalid field even when it becomes touched only in the submit handler', async () => { |
| 81 | + const { fixture, form } = createHost(); |
| 82 | + fixture.componentInstance.formGroup.controls.email.setValue('b'); |
| 83 | + fixture.detectChanges(); |
| 84 | + |
| 85 | + form.dispatchEvent(new Event('submit')); |
| 86 | + await flushFocusTimeout(); |
| 87 | + |
| 88 | + expect(document.activeElement?.id).toBe('name'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments