I am currently testing a simple component that I have created that looks like this:
@Input()
public type: string;
private saveSubscription: Subscription;
public ngOnDestroy(): void {
this.saveSubscription.unsubscribe();
}
public ngOnInit(): void {
this.onSave();
}
private onSave(): void {
this.saveSubscription = this.editService.saveSelection.subscribe(() => {
this.editService.type.next(this.type);
});
}
In my test, I want to make sure that if I set the input manually, and then call ngOnInit()
, the value emitted by the editService
will equal my value.
it('should set up the subscription for saving', fakeAsync(() => {
// Arrange
component.type = 'full';
// Act
component.ngOnInit();
fixture.detectChanges();
mockedEditService.saveSelection.next();
// Assert
mockedEditService.type.subscribe(type => {
expect(type).toEqual(component.type);
});
}));
I'm struggling with testing async code and am not sure if I am doing this correctly. The test always passes even with incorrect test cases and I suspect that the code in question is not waiting for the subscription to resolve before making its assertion.
Reading the Angular test docs, I've been able to familiarize myself a bit more, but I was wondering if you all had any suggestions.
Thanks
Aucun commentaire:
Enregistrer un commentaire