Performance: Testing forms and user interactions
This affects how quickly and smoothly user inputs are processed and how responsive the form feels during interaction.
Jump into concepts and practice - no test required
it('good test', async () => { const input = fixture.nativeElement.querySelector('input'); input.value = 'test'; input.dispatchEvent(new Event('input')); fixture.detectChanges(); await fixture.whenStable(); expect(component.form.value).toEqual({name: 'test'}); });
it('bad test', () => { const input = fixture.nativeElement.querySelector('input'); input.value = 'test'; input.dispatchEvent(new Event('input')); fixture.detectChanges(); // No async wait for debounce or async validators expect(component.form.value).toEqual({name: 'test'}); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous detectChanges without async wait | Multiple DOM updates | Multiple reflows per input | High paint cost due to forced sync updates | [X] Bad |
| Async test with fixture.whenStable() | Minimal DOM updates batched | Single reflow after stable state | Lower paint cost with smooth updates | [OK] Good |
component.form.value.name after simulating user input?component.form.controls['name'].setValue('Alice');
fixture.detectChanges();it('should update form on input', () => {
const input = fixture.nativeElement.querySelector('input[name="email"]');
input.value = 'test@example.com';
// Missing event dispatch here
fixture.detectChanges();
expect(component.form.value.email).toBe('test@example.com');
});