I test a component with Spectator which contain in it's ngInit
listenToSuggestions() {
this.querySuggestions$ =
this.searchControl.valueChanges
.pipe(
tap(value => console.log(value)),
filter(value => value && value.length > 1),
debounceTime(300),
switchMap(searchInput => {
return this.getSuggestion(searchInput);
}),
takeUntil(this.destroy$)
);
}
The querySuggestion$ observable is binded through an async pipe. My problem is I try to do an integration test which can trigger my searchControl et look if the getSuggestion method is correctly called.
it('should trigger suggestions', fakeAsync(async () => {
spectator.component.ngOnInit();
spectator.detectChanges();
await spectator.fixture.whenStable();
spectator.detectChanges();
const spyGetSuggestion = spyOn(spectator.component, 'getSuggestion');
const input = spectator.query('input') as HTMLInputElement;
expect(input.placeholder).toEqual('Search');
// three different approach to trigger my formControl
input.focus();
input.value = 'test';
input.dispatchEvent(new Event('input'));
spectator.tick(300);
spectator.typeInElement('test', input);
spectator.tick(300);
spectator.component.searchControl.setValue('test');
spectator.tick(300);
expect(spyGetSuggestion).toHaveBeenCalled();
}));
The code tap(value => console.log(value))
is never called and my test failed on the haveBeenCalled which return 0.
Angular version: 8.2.10
Jest: 24.9.0