I have a method that has a pipe with a map inside of it. When I run the test I can't find a way to get into that map.
This is my component code
public getParents(): void {
Eif (this.filter.siteId !== null) {
this.parents$ = this.parentsM().pipe(
map(items => items.data),
catchError(error => {
this.messageService.errorHandler(error);
return Array<any>();
}));
}
}
This is my Spec.ts code
it("getParents with siteId", async(() => {
const mockItems:Response = {
codError: "",
msgError: "",
data: "test"}
let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
component.getParents();
component.parentsM().subscribe(items => {
expect(items).toEqual(mockItems);
});
spy.calls.reset();
}));
This is my code coverage
CodePudding user response:
Multiple issues here. You actually have to subscribe to this.parents$
to execute the map, which makes your test async. In your test, you're subscribing on the result of parentsM()
, which I guess doesn't return exactly this.parents$
, so you're not subscribing to the pipe you wrote. You may also use waitForAsync
instead of async
.
Please try this:
it("getParents with siteId", waitForAsync(() => { // <== waitForAsync
const mockItems:Response = {
codError: "",
msgError: "",
data: "test"}
let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
component.getParents();
component.parents$.subscribe(items => { // <== parents$
expect(items).toEqual(mockItems);
});
spy.calls.reset();
}));