/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {Injector, signal} from '@angular/core'; import {TestBed} from '@angular/core/testing'; import { applyWhenValue, debounce, disabled, form, hidden, required, submit, } from '@angular/forms/signals'; import {timeout} from '@angular/private/testing'; describe('debounce', () => { describe('by duration', () => { it('should synchronize value immediately if non-positive', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 0); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize value after duration', async () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 1); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); await timeout(0); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize value immediately on touch', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 1); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); street.markAsTouched(); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize the value before running the submit action', async () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 500); }, options(), ); let submittedStreet: string | undefined; addressForm.street().controlValue.set('1600 Amphitheatre Pkwy'); await submit(addressForm, async (form) => { submittedStreet = form.street().value(); }); expect(submittedStreet).toBe('1600 Amphitheatre Pkwy'); }); }); describe('by function', () => { it('should synchronize value immediately by default', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, () => {}); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize value immediately on touch', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, forever); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); street.markAsTouched(); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize value after promise resolves', async () => { const {promise, resolve} = Promise.withResolvers(); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, () => promise); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); resolve(); await promise; expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize value after most recently returned promise resolves', async () => { const first = Promise.withResolvers(); const second = Promise.withResolvers(); const debounceFn = jasmine .createSpy('debounceFn') .and.returnValues(first.promise, second.promise); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, debounceFn); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); street.controlValue.set('2000 N Shoreline Blvd'); expect(street.value()).toBe(''); first.resolve(); await first.promise; expect(street.value()).toBe(''); second.resolve(); await second.promise; expect(street.value()).toBe('2000 N Shoreline Blvd'); }); it('should be ignored if value is directly set before it resolves', async () => { const debounceResult = Promise.withResolvers(); const debounceFn = jasmine.createSpy('debounceFn').and.returnValues(debounceResult.promise); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, debounceFn); }, options(), ); const street = addressForm.street(); // Set `controlValue` which will trigger a debounce update. street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); // Directly set value during debounce duration. street.value.set('2000 N Shoreline Blvd'); expect(street.value()).toBe('2000 N Shoreline Blvd'); expect(street.controlValue()).toBe('2000 N Shoreline Blvd'); // Wait for the debounced update, which should be ignored. await debounceResult.resolve; expect(street.value()).toBe('2000 N Shoreline Blvd'); }); describe('abort signal', () => { it('should be aborted if control value is set again', async () => { const {promise, resolve} = Promise.withResolvers(); const abortSpy = jasmine.createSpy('abort'); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, (_context, signal) => { signal.addEventListener('abort', abortSpy); return promise; }); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(abortSpy).not.toHaveBeenCalled(); street.controlValue.set('2000 N Shoreline Blvd'); expect(abortSpy).toHaveBeenCalledTimes(1); resolve(); await promise; expect(street.value()).toBe('2000 N Shoreline Blvd'); }); it('should be aborted on touch', async () => { const abortSpy = jasmine.createSpy('abort'); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, (_context, signal) => { signal.addEventListener('abort', abortSpy); return forever(); }); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(abortSpy).not.toHaveBeenCalled(); street.markAsTouched(); expect(abortSpy).toHaveBeenCalledTimes(1); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should remove abort listener when debounce completes', async () => { const addListenerSpy = spyOn(AbortSignal.prototype, 'addEventListener').and.callThrough(); const removeListenerSpy = spyOn( AbortSignal.prototype, 'removeEventListener', ).and.callThrough(); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 1); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(addListenerSpy).toHaveBeenCalledOnceWith('abort', jasmine.any(Function), { once: true, }); expect(removeListenerSpy).not.toHaveBeenCalled(); await timeout(10); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); expect(removeListenerSpy).toHaveBeenCalledOnceWith('abort', jasmine.any(Function)); }); }); }); describe('until blurred', () => { it('should synchronize value immediately on touch', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); street.markAsTouched(); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should be ignored if value is directly set before blur', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); street.value.set('2000 N Shoreline Blvd'); expect(street.value()).toBe('2000 N Shoreline Blvd'); expect(street.controlValue()).toBe('2000 N Shoreline Blvd'); street.markAsTouched(); expect(street.value()).toBe('2000 N Shoreline Blvd'); }); it('should synchronize descendant values when an ancestor is touched', () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); addressForm().markAsTouched(); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize the value of a disabled field when it is touched', () => { const locked = signal(false); const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); disabled(address.street, {when: () => locked()}); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); locked.set(true); street.markAsTouched(); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize values below a hidden field when an ancestor is touched', () => { const showAddress = signal(true); const user = signal({address: {street: ''}}); const userForm = form( user, (user) => { debounce(user.address.street, 'blur'); hidden(user.address, {when: () => !showAddress()}); }, options(), ); const street = userForm.address.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); showAddress.set(false); userForm().markAsTouched(); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should synchronize descendant values before running the submit action', async () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); }, options(), ); let submittedStreet: string | undefined; addressForm.street().controlValue.set('1600 Amphitheatre Pkwy'); await submit(addressForm, async (form) => { submittedStreet = form.street().value(); }); expect(submittedStreet).toBe('1600 Amphitheatre Pkwy'); }); it('should not create the fields of a hidden subtree when an ancestor is touched', () => { const user = signal({address: {street: ''}}); const userForm = form( user, (user) => { hidden(user.address, {when: () => true}); }, options(), ); userForm().markAsTouched(); expect((userForm.address() as any).structure._areChildrenMaterialized()).toBeFalse(); }); it('should not run the submit action when a synchronized descendant value is invalid', async () => { const address = signal({street: '1600 Amphitheatre Pkwy'}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); required(address.street); }, options(), ); const action = jasmine.createSpy('action'); addressForm.street().controlValue.set(''); expect(await submit(addressForm, action)).toBeFalse(); expect(action).not.toHaveBeenCalled(); expect(addressForm.street().errors()).toEqual([jasmine.objectContaining({kind: 'required'})]); }); it('should synchronize the values of sibling fields before running the submit action', async () => { const address = signal({street: '', city: ''}); const addressForm = form( address, (address) => { debounce(address.street, 'blur'); debounce(address.city, 'blur'); }, options(), ); let submitted: {street: string; city: string} | undefined; addressForm.street().controlValue.set('1600 Amphitheatre Pkwy'); addressForm.city().controlValue.set('Mountain View'); await submit(addressForm, async (form) => { submitted = form().value(); }); expect(submitted).toEqual({street: '1600 Amphitheatre Pkwy', city: 'Mountain View'}); }); }); describe('inheritance', () => { it('should inherit debounce from parent', async () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address, 1); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); await timeout(0); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('can override inherited debounce', async () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address, 1); debounce(address.street, 0); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it(`should not affect parent's debounce`, async () => { const address = signal({street: ''}); const addressForm = form( address, (address) => { debounce(address, 1); debounce(address.street, 0); }, options(), ); addressForm().controlValue.set({street: '1600 Amphitheatre Pkwy'}); expect(addressForm().controlValue()).toEqual({street: '1600 Amphitheatre Pkwy'}); expect(addressForm().value()).toEqual({street: ''}); await timeout(0); expect(addressForm().value()).toEqual({street: '1600 Amphitheatre Pkwy'}); }); it(`should not affect a sibling's debounce`, async () => { const address = signal({street: '', city: ''}); const addressForm = form( address, (address) => { debounce(address.street, 1); }, options(), ); addressForm.street().controlValue.set('1600 Amphitheatre Pkwy'); expect(addressForm().value()).toEqual({street: '', city: ''}); addressForm.city().controlValue.set('Mountain View'); expect(addressForm().value()).toEqual({street: '', city: 'Mountain View'}); await timeout(0); expect(addressForm().value()).toEqual({ street: '1600 Amphitheatre Pkwy', city: 'Mountain View', }); }); }); describe('aggregation', () => { it('should apply the last debounce rule', () => { const address = signal({street: '', city: ''}); const addressForm = form( address, (address) => { debounce(address.street, 1); debounce(address.street, 0); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should apply the last debounce rule from schemas', async () => { const address = signal({street: '', city: ''}); const schema1 = (address: any) => { debounce(address.street, 0); }; const schema2 = (address: any) => { debounce(address.street, 1); }; const addressForm = form( address, (address) => { schema1(address); schema2(address); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); await timeout(0); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); }); it('should apply the last debounce rule from conditional schemas', async () => { const address = signal({street: '', city: ''}); const debounced = signal(false); const addressForm = form( address, (address) => { applyWhenValue( address, () => debounced(), (address) => { debounce(address.street, 0); }, ); applyWhenValue( address, () => debounced(), (address) => { debounce(address.street, 1); }, ); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); debounced.set(true); street.controlValue.set('2000 N Shoreline Blvd'); expect(street.controlValue()).toBe('2000 N Shoreline Blvd'); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); await timeout(0); expect(street.value()).toBe('2000 N Shoreline Blvd'); }); it('should apply debounce rule conditionally', async () => { const address = signal({street: '', city: ''}); const debounced = signal(true); const addressForm = form( address, (address) => { applyWhenValue( address.street, () => debounced(), (street) => { debounce(street, 1); }, ); }, options(), ); const street = addressForm.street(); street.controlValue.set('1600 Amphitheatre Pkwy'); expect(street.controlValue()).toBe('1600 Amphitheatre Pkwy'); expect(street.value()).toBe(''); await timeout(0); expect(street.value()).toBe('1600 Amphitheatre Pkwy'); debounced.set(false); street.controlValue.set('2000 N Shoreline Blvd'); expect(street.value()).toBe('2000 N Shoreline Blvd'); }); }); }); /** Options for testing. */ function options() { return {injector: TestBed.inject(Injector)}; } /** Returns a promise that will never resolve. */ function forever(): Promise { return new Promise(() => {}); }