Для проверки следующего родительского хост-компонента, вызывающего реальную функцию, когда дочерний элемент выдает , я думаю, что вы решаете проблему не с того конца.
В вашем случаевам нужно смоделировать компонент шаблона (дочерний компонент, компонент app-chat-feedback) и запустить событие вручную (не через весь процесс щелчка, как в случае другого модульного теста).Проверьте ниже, это должно заставить вас работать.
Это макет компонента шаблона
@Component({
template: '',
selector: 'app-chat-feedback'
})
export class ChatFeedbackComponentMock {
@Output() chatMessageFeedback$: EventEmitter<any> = new EventEmitter<any>();
// This will be the function that triggers the emit
emitFeedback(value: any) {
this.chatMessageFeedback$.emit(value);
}
}
Это компонент, функциональность которого вы тестируете, в данном случае, чтоchatFeedbackClicked
вызывается, когда ребенок генерирует событие.Это реальный компонент.
@Component({
template:`
// Your Logic here
<app-chat-feedback (chatMessageFeedback$)="chatFeedbackClicked($event)"></app-chat-feedback>
`
})
export class MyComponentUnderTest {
chatFeedbackClicked() {
// Your logic here
}
}
Это определение теста.
describe('MyComponentUnderTest', () => {
let fixture: ComponentFixture<MyComponentUnderTest>;
let component: MyComponentUnderTest;
beforeEach(() => {
// Register both the real component under test, and the mock component of the child
TestBed.configureTestingModule({
declarations: [ ChatFeedbackComponentMock, MyComponentUnderTest]
});
// Get a handle to the component fixture, and the component instancce
fixture = TestBed.createComponent(MyComponentUnderTest);
component = fixture.componentInstance;
});
// Important that you have the fakeAsync wrapper function, since the EventEmmiter behaves asynchronously
it('Should call chatFeedbackClicked when the chatMessageFeedback$ event is fired', fakeAsync(() => {
// Get a handle of the instance of the child mock component (the template component that you're mocking)
let child: ChatFeedbackComponentMock = fixture.debugElement.query(By.css('app-chat-feedback')).componentInstance;
// Spy on the function of the component under test, that will be asserted
spyOn(component, 'chatFeedbackClicked');
// Call emit feedback with some value, on the child component. This will trigger an emit call
child.emitFeedback('value');
// Since the calls are asynchronous, (EventEmmitter behaves like an observable), we need to simulate passing of time
tick();
// Assert that the function was called with the passed value
expect(component.chatFeedbackClicked).toHaveBeenCalledWith('value');
}));
});
Подробнее о тестировании асинхронных функций здесь и о fakeAsync
функции-обертке здесь