I've been working on an angular 11 unit test case for code within an if block. Interestingly, when I comment out the if block, all the tests pass without any issues. However, when the if block is present, it seems that the code isn't entering the block as expected.
splash-screen.service.ts
init() {
// Retrieving the splash screen element
this.splashScreenEl = this.document.body.querySelector('#splash-screen');
this.router.events.pipe(filter((event => event instanceof NavigationEnd)), take(1)).subscribe(() => {
setTimeout(() => {
// Checking if the splash screen element exists...
// Hide it upon the first NavigationEnd event
if (this.splashScreenEl) {
this.hide();
}
});
});
}
splash-screen.service.ts
it('should trigger hide method with end navigation event', fakeAsync(() => {
const methodSpy: jasmine.Spy = spyOn(service, 'hide');
eventSubject.next(new NavigationEnd(1, 'http://localhost:4200/', 'http://localhost:4200/'));
tick();
const splashScreenEl = jasmine.createSpy('HTML Element').and.returnValue(true);
service.splashScreenEl = splashScreenEl;
expect(service.splashScreenEl).toBeTruthy();
expect(methodSpy).toHaveBeenCalled();
}));
Any advice on how to get this test case to successfully pass would be greatly appreciated!
PS: I'm fairly new to writing jasmine test cases.