1

I am trying to test an effect with Jasmine.

Unfortunaltely I don't manage to get the completion as I don't know how to mock pipeable operator...

@Effect()
initData$: Observable<Action> = this.actions$.pipe(
    ofType(INIT_DATA_ACTION),
    tap(() => {
        this.loadingService.showLoading();
    }),
    switchMap((data: any) => {
        return this.store.pipe(
            select(getAppDataResolved),
            take(1),
            switchMap((resolved: boolean) => {
                console.log('resolved');
                if (!resolved) {
                    return this.dataService.getInitData(this.loginService.user.id).pipe(
                        tap(() => {
                            this.loadingService.hideLoading();
                        }),
                        switchMap((response: any) => {

                            return Observable.from([
                                new AislesInitDataAction(response.userAisles),
                                new ItemsInitDataAction(response.userItems),
                                new ListsInitDataAction(response.userLists),
                                new ShopsInitDataAction(response.userShops),
                                new InitDataResolvedAction(),
                            ]);
                        }
                    ));
                } else {
                    this.loadingService.hideLoading();
                    return of(new InitDataResolvedAction());
                }
            }
            ));
    }),

My test:

fit('should return InitDataResolvedAction if resolved is true', () => {
    const spy = spyOn(store, 'pipe').and.returnValue(of(true));
    const spyOnLoadingService = spyOn(loadingService, 'showLoading');
    const action = new InitDataAction();
    const completion = new InitDataResolvedAction();

    actions$ = new ReplaySubject(1);
    actions$.next(action);

    effects.initData$.subscribe((a) => {
        console.log(a)
        // expect(a).toEqual(completion);
    });
});

All what I get is true as I mocked piped function.

Any idea how to mock operators used in pipe() ?

Thanks in advance

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Laurent B
  • 215
  • 1
  • 11

0 Answers0