Я тестирую компонент со Spectator, который содержит в своем 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$)
);
}
Объект querySuggestion $ observable связан через асинхронный канал c. Моя проблема заключается в том, что я пытаюсь выполнить интеграционный тест, который может запустить мой searchControl и проверить, правильно ли вызван метод getSuggestion.
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();
}));
Код tap(value => console.log(value))
никогда не вызывается, и мой тест не выполнен на haveBeenCalled, который возврат 0.
Angular версия: 8.2.10 Шутка: 24,9,0