I'm writing a test case in angular. I have written a condition if res.length === 1 redirect to the details page. (this.router.navigate(['details', id])). I'm getting the id from the first array of the object in body response as const id = res[0].id. These both line are not covered in my code coverage. Can anyone let me know where I made mistake?
I'm getting Expected spy navigate to have been called with [ [ '/product-details', 'SAAASD0001' ] ] but it was never called.
app.component.spec.ts
let router = {navigate: jasmine.createSpy('navigate')};
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
{ provide: Router, useValue: router }
]
})
it('should take data from store', () => {
const mockData = [
{
id: '123',
name: 'Stackoverlow',
}
]
expect(component.getList).toEqual(mockData);
const productId = mockData[0].id;
expect(router.navigate).toHaveBeenCalledWith(['/details', id]);
});
app.component.ts
getList() {
this.store
.select('content', 'catalogue')
.pipe(takeUntil(this.onDestroy$))
.subscribe((res) => {
Iif (res.length === 1) {
// this line doesn't cover
const id = res[0].id;
// this line doesn't cover
this.router.navigate(['details', id]);
} else {
this.list = category(res);
}
});
}
routerSpy?router