Интеграционный тест ReactiveForm, метод valueChanges не запущен
Я тестирую компонент с помощью 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$ связывается через асинхронный канал. Моя проблема в том, что я пытаюсь выполнить интеграционный тест, который может вызвать мой searchControl et look, если метод 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.
Угловая версия: 8.2.10
Jest: 24.9.0