import * as actions from './user.actions';
import { Update } from '@ngrx/entity';
const INITIAL_STATE_WITH_ERROR = {
const BLANK_ERROR_MESSAGE = '';
describe('userReducer', () => {
describe('upon an undefined action', () => {
it('should return the default state upon an undefined action', () => {
const action = { type: 'NOT DEFINED' } as any;
expect(userReducer(initialUserState, action)).toEqual(initialUserState);
describe('upon CreateUser', () => {
it('should set loading to true and clear any error', () => {
const action = new actions.CreateUser({ user: generateUser() });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
error: BLANK_ERROR_MESSAGE
describe('upon CreateUserSuccess', () => {
it('should add the given User, set loading to false, and clear any error', () => {
const result = generateUser();
const action = new actions.CreateUserSuccess({ result });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
...generateUserMap([result]),
error: BLANK_ERROR_MESSAGE
describe('upon CreateUserFail', () => {
it('should set loading to true and echo the error', () => {
const error = 'test create error';
const action = new actions.CreateUserFail({ error });
expect(userReducer(initialUserState, action)).toEqual({
error: `User create failed: ${error}`
describe('upon SearchAllUserEntities', () => {
it('should remove User entities, set loading to true, and clear any error', () => {
const initialUserStateWithUserEntities = {
...INITIAL_STATE_WITH_ERROR,
const action = new actions.SearchAllUserEntities();
expect(userReducer(initialUserStateWithUserEntities, action)).toEqual({
error: BLANK_ERROR_MESSAGE
describe('upon SearchAllUserEntitiesSuccess', () => {
it('should add User entities, set loading to false, and clear any error', () => {
const result = generateUserArray();
const action = new actions.SearchAllUserEntitiesSuccess({ result });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
...generateUserMap(result),
error: BLANK_ERROR_MESSAGE
describe('upon SearchAllUserEntitiesFail', () => {
it('should set loading to false and echo the error', () => {
const error = 'test search error';
const action = new actions.SearchAllUserEntitiesFail({ error });
expect(userReducer(initialUserState, action)).toEqual({
error: `User search failed: ${error}`
describe('upon LoadUserById', () => {
it('should remove user entities, set selected id, and clear any error', () => {
const initialUserStateWithUserEntities = {
...INITIAL_STATE_WITH_ERROR,
const action = new actions.LoadUserById({ id });
expect(userReducer(initialUserStateWithUserEntities, action)).toEqual({
error: BLANK_ERROR_MESSAGE
describe('upon LoadUserByIdSuccess', () => {
it('should add the given User, set loading to false, and clear any error', () => {
const result = generateUser();
const action = new actions.LoadUserByIdSuccess({ result });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
...generateUserMap([result]),
error: BLANK_ERROR_MESSAGE
describe('upon LoadUserByIdFail', () => {
it('should set loading to false and echo the error', () => {
const error = 'test load by id error';
const action = new actions.LoadUserByIdFail({ error });
expect(userReducer(initialUserState, action)).toEqual({
error: `User load failed: ${error}`
describe('upon UpdateUser', () => {
it('should set loading to true and clear any errior', () => {
const user = generateUser();
const action = new actions.UpdateUser({ user });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
error: BLANK_ERROR_MESSAGE
describe('upon UpdateUserSuccess', () => {
it('should add the given User, set loading to false, and clear any error', () => {
const user = generateUser();
const initialUserStateWithUser = {
...INITIAL_STATE_WITH_ERROR,
...generateUserMap([user])
name: user.name + ' EDITED',
description: user.description + ' EDITED'
const action = new actions.UpdateUserSuccess({ update });
expect(userReducer(initialUserStateWithUser, action)).toEqual({
...initialUserStateWithUser,
...generateUserMap([updatedUser]),
error: BLANK_ERROR_MESSAGE
describe('upon UpdateUserFail', () => {
it('should set loading to false and echo the error', () => {
const error = 'test update error';
const action = new actions.UpdateUserFail({ error });
expect(userReducer(initialUserState, action)).toEqual({
error: `User update failed: ${error}`
describe('upon DeleteUserById', () => {
it('should set the id, set loading to true, and clear any error', () => {
const action = new actions.DeleteUserById({ id });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
error: BLANK_ERROR_MESSAGE
describe('upon DeleteUserByIdSuccess', () => {
it('should remove the id-given user, set loading to false, and clear any error', () => {
const userToBeRemoved = generateUser(id);
const expectedUserEntities = generateUserArray();
const userEntitiesWithUserToBeRemoved = [
const initialUserStateWithAllUserEntities = {
...INITIAL_STATE_WITH_ERROR,
...generateUserMap(userEntitiesWithUserToBeRemoved)
const action = new actions.DeleteUserByIdSuccess({ id });
userReducer(initialUserStateWithAllUserEntities, action)
...initialUserStateWithAllUserEntities,
...generateUserMap(expectedUserEntities),
error: BLANK_ERROR_MESSAGE
describe('upon DeleteUserByIdFail', () => {
it('should set loading to false and echo the error', () => {
const error = 'test delete error';
const action = new actions.DeleteUserByIdFail({ error });
expect(userReducer(initialUserState, action)).toEqual({
error: `User delete failed: ${error}`
describe('upon SetSearchQuery', () => {
it('should set the query', () => {
const action = new actions.SetSearchQuery(query);
expect(userReducer(initialUserState, action)).toEqual({
describe('upon SelectUserById', () => {
it('should set the id and clear any error', () => {
const action = new actions.SelectUserById({ id });
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
error: BLANK_ERROR_MESSAGE
describe('getters', () => {
describe('getSelectedId', () => {
it('should return the selected id', () => {
expect(getSelectedId(initialUserState)).toEqual(initialUserState.selectedId);
describe('getLoading', () => {
it('should return the selected id', () => {
expect(getLoading(initialUserState)).toEqual(initialUserState.loading);
describe('getError', () => {
it('should return the selected id', () => {
expect(getError(INITIAL_STATE_WITH_ERROR))
.toEqual(INITIAL_STATE_WITH_ERROR.error);
describe('getQuery', () => {
it('should return the selected id', () => {
expect(getQuery(initialUserState))
.toEqual(initialUserState.query);