diff --git a/packages/angular-test-app/src/select/select.spec.ts b/packages/angular-test-app/src/select/select.spec.ts index a926dfa033..f79c77f876 100644 --- a/packages/angular-test-app/src/select/select.spec.ts +++ b/packages/angular-test-app/src/select/select.spec.ts @@ -16,24 +16,66 @@ import { By } from '@angular/platform-browser'; @Component({ selector: 'ix-example-form-select', - template: `
- - Test - - - - - + template: ` + + + + + + + + +

TypeError-Example

+ + - `, + `, }) class SelectComponent { public form = new FormGroup({ select: new FormControl('1') }); + value = '3'; + selection = ['3', '4', '5']; + + public updateSelection() { + this.value = '6'; + this.selection = ['6', '7', '8']; + } +} + +function waitForHydration(element: HTMLElement, timeout = 5000): Promise { + return new Promise((resolve, reject) => { + const interval = 50; + let elapsedTime = 0; + + const checkClass = () => { + if (element.classList.contains('hydrated')) { + resolve(); + } else if (elapsedTime >= timeout) { + reject(new Error(`Timeout waiting for hydration`)); + } else { + elapsedTime += interval; + setTimeout(checkClass, interval); + } + }; + + checkClass(); + }); +} + +function checkForError(consoleSpy: jasmine.Spy, errorName: string): boolean { + return consoleSpy.calls + .allArgs() + .some((arg) => arg[0].toString().includes(errorName)); } describe('SelectFormComponent', () => { let component: SelectComponent; let fixture: ComponentFixture; + let consoleSpy: jasmine.Spy; beforeEach(async () => { await TestBed.configureTestingModule({ @@ -44,6 +86,10 @@ describe('SelectFormComponent', () => { fixture = TestBed.createComponent(SelectComponent); component = fixture.componentInstance; + + consoleSpy = spyOn(console, 'error').and.callThrough(); + + fixture.detectChanges(); }); beforeEach(async () => { @@ -51,12 +97,28 @@ describe('SelectFormComponent', () => { await TestBed.inject(ApplicationInitStatus).donePromise; }); - it('should change the input value', async () => { - const select = fixture.debugElement.query(By.css('ix-select')); + it('should change the form control value', async () => { + const select = fixture.debugElement.query(By.css('ix-select[formControlName="select"]')); + + await waitForHydration(select.nativeElement); + component.form.get('select')!.setValue('2'); fixture.detectChanges(); expect(select.nativeElement.value).toBe('2'); expect(component).toBeDefined(); }); + + it('should change the input value and check for errors', async () => { + const select = fixture.debugElement.query(By.css('ix-select:not([formControlName="select"])')); + + await waitForHydration(select.nativeElement); + + component.updateSelection(); + fixture.detectChanges(); + + expect(checkForError(consoleSpy, 'TypeError')).toBe(false); + expect(select.nativeElement.value).toBe('6'); + expect(component).toBeDefined(); + }); });