dimanche 7 juin 2020

angular testing library : How to test subscribe service in component

I want to test several action made after createOrder done, i try several use cases no one work, can someone give me the good way to success please :).

My component :

export class OrderStepComponent implements OnInit {
...
onCreateOrder(shoppingCart: IShoppingCartView): void {
// TODO when error toast notification
const order: Order = mapShoppingCartToOrder(shoppingCart);
this.orderService.createOrder(order).subscribe(() => {
console.log('subscribe');
this.messageService.success(TOAST\_MESSAGE.messages.orderCreated, this.configService.configuration.toastMessageDurationInMilliseconds);
this.userService.cleanUser();
this.router.navigate(['/']);
});
}

...

}

My test :

fit(`should clean user, reload user, and redirect to {'/'} when order created`, async (done) => {
const cleanUserSpy = spyOn(UserService.prototype, 'cleanUser');
const loadUserSpy = spyOn(UserService.prototype, 'loadUser');
const createOrderResult$: Observable<void> = of();


const createOrderSpy = spyOn(OrderService.prototype, 'createOrder').and.returnValue(createOrderResult$).and.callThrough();


....

await createOrderResult$.subscribe(() => {
// => this code is never reached
console.log('ok');
expect(cleanUserSpy).toHaveBeenCalled();
expect(loadUserSpy).toHaveBeenCalled();
expect(navigateSpy).toHaveBeenCalledWith(['']);
});

await click(getAllByText('shoppingCart.summary.button.validate')[0]);

await expect(createOrderSpy).toHaveBeenCalledWith({ modules: modulesAdded });

done();
...
}

Aucun commentaire:

Enregistrer un commentaire