効果がトリガーされたら、単体テストで両方のオブザーバブルをテストして、コードのこの部分の 100% のコード カバレッジを取得したいと考えています。awindow.location.href
がトリガーされるため、正しくテストできません。
export class RouteHelper {
static redirectToExternalUrl(url: string): any {
window.location.href = url;
}
}
効果
@Effect()
handleCreatePaymentSuccess$: Observable<
{} | routerActions.Go
> = this.actions$.pipe(
ofType(cartConfigActions.CREATE_PAYMENT_SUCCESS),
switchMap((action: any) => {
if (action.payload.redirect) {
/* istanbul ignore next */
return Observable.create(
RouteHelper.redirectToExternalUrl(action.payload.redirect),
);
} else {
return of(
new routerActions.Go({
path: [RouteHelper.paths['validate']],
}),
);
}
}),
);
else 条件の動作をテストする
it('should dispatch router action Go on success if no redirect url is provided', () => {
const payload = { redirect: null };
const action = new fromCartConfig.CreatePaymentSuccess(payload);
const completion = new routeractions.Go({
path: [RouteHelper.paths['validate']],
});
actions$.stream = cold('-a', { a: action });
const expected = cold('-c', { c: completion });
expect(effects.handleCreatePaymentSuccess$).toBeObservable(expected);
});
if 条件でテストが機能しない
it('should redirect to url that is returned from api', () => {
const payload = { redirect: 'http://www.stackoverflow.com' };
spyOn(RouteHelper, 'redirectToExternalUrl').withArgs(payload.redirect);
const action = new fromCartConfig.CreatePaymentSuccess(payload);
const completion = Observable.create(RouteHelper.redirectToExternalUrl);
actions$.stream = cold('-a', { a: action });
const expected = cold('-c', { c: completion });
expect(effects.handleCreatePaymentSuccess$).toBeObservable(expected);
});
誰かがIf条件をテストする方法を説明できますか?