Skip to content

Issue testing @ngrx/effects #117

@xocomil

Description

@xocomil

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions