Testing Effects
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { cold, hot } from 'jasmine-marbles';
import { Observable } from 'rxjs';
import {
InsertUser,
InsertUserSuccess,
InsertUserFail
} from './user.actions';
import { generateUser, generateUserArray } from '../../core/user';
// TODO: Change this path when you move your service file:
import { UserService } from '../../core/user.service';
import { UserEffects } from './user.effects';
describe('UserEffects', () => {
let actions: Observable<any>;
let effects: UserEffects;
let service;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
UserEffects,
provideMockActions(() => actions),
{
provide: UserService,
useValue: { create: jest.fn() }
},
]
});
effects = TestBed.get(UserEffects);
service = TestBed.get(UserService);
});
it('should be constructed', () => {
expect(effects).toBeTruthy();
});
// Effect Test
describe('insert', () => {
it('should return InsertUserSuccess action with entity on success', () => {
const entity = generateUser(),
insertAction = new InsertUser({ user: entity }),
successAction = new InsertUserSuccess({ result: entity }),
response = cold('-b|', { b: entity }),
expected = cold('-s', { s: successAction });
actions = hot('a-', { a: insertAction });
service.create = jest.fn(() => response);
expect(effects.insert).toBeObservable(expected);
});
});
// Effect Test - Failure Path
it('should return InsertUserFail with error object on failure', () => {
const entity = generateUser(),
insertAction = new InsertUser({ user: entity }),
failAction = new InsertUserFail({ error: 'fail' }),
response = cold('-#|', {}, { message: 'fail' }),
expected = cold('-f', { f: failAction });
actions = hot('i-', { i: insertAction });
service.create = jest.fn(() => response);
expect(effects.insert).toBeObservable(expected);
});
});
Write tests for Tour of Heroes:
https://stackblitz.com/github/jessesanders/tour-of-heroes/tree/ngrx-entity
Last updated