Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue testing @ngrx/effects #117

Closed
xocomil opened this issue Jul 19, 2017 · 2 comments
Closed

Issue testing @ngrx/effects #117

xocomil opened this issue Jul 19, 2017 · 2 comments

Comments

@xocomil
Copy link

xocomil commented Jul 19, 2017

When testing using provideMockActions() I get TypeError: this.actions$.ofType is not a function. If I comment out the .ofType() filter in my effects class, then my tests run and pass/fail. Uncommenting the .ofType() filter in my effects works just fine when I serve the site. It is only in testing that I have the issue.

Imports:

import { provideMockActions } from '@ngrx/effects/testing';
import { hot, cold } from 'jasmine-marbles';
import { TestBed } from '@angular/core/testing';

TestBed:

    beforeEach(() => TestBed.configureTestingModule({
        providers: [
            InfoRepresentativeEffects,
            provideMockActions(() => actions),
            {
                provide: RepresentativeService,
                useValue: jasmine.createSpyObj('representativeService', ['getRepresentativeById'])
            }
        ]
    }));

Broken Test:

        it('should return a LOAD_SUCCESS action, on success', () => {
            service.getRepresentativeById.and.returnValue(Observable.of(fullRepresentative));

            actions = hot('--a-', { a: loadAction });

            const successAction = new fromRepresentative.LoadSuccessAction(fullRepresentative);

            const expectedResults = cold('--b', { b: successAction });

            expect(effects.infoRepresentative$).toBeObservable(expectedResults);
        });

Effect:

import { Injectable } from '@angular/core';
import { Actions, Effect, toPayload } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { RepresentativeService } from 'app/core/services-api/representative.service';
import * as representative from '../actions/representative-general-info.representative';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

@Injectable()
export class InfoRepresentativeEffects {
    constructor(
        private readonly actions$: Actions,
        private readonly representativeService: RepresentativeService) {}

    @Effect() infoRepresentative$: Observable<Action> = this.actions$
        .ofType(representative.LOAD)
        .map(toPayload)
        .switchMap((payload: string) => this.representativeService.getRepresentativeById(payload)
            .map(response => new representative.LoadSuccessAction(response))
            .catch(err => {
                console.log('error', err);

                return Observable.of(new representative.LoadFailAction(`There was an error getting representatives: ${err}`));
            })
        );
}

I've followed the migration document, but I can't seem to figure this one out.

@MikeRyanDev
Copy link
Member

Thanks for the report! Fix is in PR #121

@KevinOrfas
Copy link

I am not sure that issue is solve. I just run into that and just updated the ngrx again and checked the commit which apparently fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants