Here is my component code that i want to test:
allpages: Array<Page> = [
{ name: 'Home', url: '/home' },
];
constructor(private router: Router) {
this.$routerEvent = this.router.events.subscribe((event: Event) => {
if (event instanceof NavigationEnd) {
let route = event.url !== '/' ? event.url : event.urlAfterRedirects;
this.currentPage = this.allpages.find(
(page) => page.url === route
);
}
});
}
Here is my test:
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [NavigationBarComponent],
imports: [MatIconModule, RouterTestingModule],
}).compileComponents();
fixture = TestBed.createComponent(NavigationBarComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should set home page as current page', inject([Router], (router: Router) => {
router.initialNavigation();
expect(component.currentPage).toEqual({ name: 'Home', url: '/home' });
}));
Test fails because component.currentPage = undefined.
I have read that navigation is an async operation, how do i properly should implement the test, or maybe i went absolutely incorrect way?