I have an Angular component that uses ngrx
ActionsSubject
to listen to dispatched actions. How can I write a test that can check this subscription of ActionsSubject
?
The component looks like this:
export class TestComponent {
constructor(public actionsSubject: ActionsSubject) { }
public isUpdated = false;
public ngOnInit() {
this.actionSubscription = this.actionsSubject.subscribe(action => {
if (action.type === "Update") {
this.isUpdated = true; <----- I want to test this condition
}
});
}
}
Currently, I am dispatching update action manually:
it('isUpdated should be true, when update action dispatched', () => {
component.store.dispatch(new UpdateAction());
expect(component.isUpdated).toEqual(true);
});
It works, but I want to mock this subscription instead of calling action manually.
For example:
it('isUpdated should be true, when update action dispatched', () => {
let actionSubject = TestBed.get(ActionsSubject);
const action = {
type: 'Update'
};
actionSubject = of({ action });
component.ngOnInit();
fixture.detectChanges()
expect(component.isUpdated).toEqual(true);
});
But it never triggers the subscription in the component. How can I test it efficiently?
Aucun commentaire:
Enregistrer un commentaire