In my Angular project I have this code in one of my components:
delete(post: PostInterface): void {
const delete$ = this.appDataService.proxy
.delete(post, this.paginate)
.pipe(switchMap(() => this.loadDatas()));
this.subscriptions.add(
this.appDataService.start(delete$, 'delete').subscribe()
);
}
And in my spec file:
describe('PostListComponent', () => {
let component: PostListComponent;
let fixture: ComponentFixture<PostListComponent>;
let debugElement: DebugElement;
beforeEach(async () => {
await TestBed.configureTestingModule({
// ...
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(PostListComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
fixture.detectChanges();
});
});
describe('functions', () => {
it('should be delete with proxy.delete() and reload datas', fakeAsync(() => {
spyOn(component, 'loadDatas');
component.delete(1);
flush();
expect(component.loadDatas).toHaveBeenCalledTimes(1);
}));
});
});
In real environment the switchMap
works fine, but in test it fails with this message:
PostListComponent > functions > should be delete with proxy.delete() and reload datas
Expected spy loadDatas to have been called once. It was called 0 times.
Why isn't run the switchMap
in my test? Any idea?