Angular – Writing unit tests for setTimeout in functions

// component functions

onFocusoutControl(control, input) {
setTimeout(() => {
control.markAsTouched();
}, 200);
}

// import functions and classes from testing
import {async, ComponentFixture, TestBed, inject, fakeAsync, tick} from ‘@angular/core/testing’;

// unit testing
it(‘should close the input when focused out’, fakeAsync(() => {
const control = component.form.controls.emailAddress;
component.onFocusoutControl(control, input);
tick(200);

fixture.whenStable().then( () => {
expect(control.touched).toBeTruthy();
});
}));

Add a Comment

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image