Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: TT-155 Added unit tests for user ngrx flow
  • Loading branch information
thegreatyamori authored and Angeluz-07 committed Mar 19, 2021
commit a9702346743d3d7de06a572a40e0bc6630636e42
28 changes: 28 additions & 0 deletions src/app/modules/user/store/user.actions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LoadUserFail, LoadUserSuccess, UserActionTypes } from './user.actions';

import { User } from '../models/user';

describe('Actions for User', () => {
it('LoadUserSuccess type is UserActionTypes.LOAD_USER_SUCCESS', () => {
const user: User = {
name: 'Jerson Morocho',
email: '[email protected]',
roles: [],
groups: [],
id: 'dd4a1571-b025-41c9-b35f-810841b43134',
tenant_id: 'cc925a5d-9644-4a4f-8d99-0bee49aadd05',
deleted: ''
};

const loadUserSuccess = new LoadUserSuccess(user);

expect(loadUserSuccess.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS);
});

it('LoadUserFail type is UserActionTypes.LOAD_USER_FAIL', () => {
const loadUserFail = new LoadUserFail('error');

expect(loadUserFail.type).toEqual(UserActionTypes.LOAD_USER_FAIL);
});

});
62 changes: 62 additions & 0 deletions src/app/modules/user/store/user.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Observable, of, throwError } from 'rxjs';
import { Action } from '@ngrx/store';
import { User } from '../models/user';
import { UserEffects } from './user.effects';
import { TestBed } from '@angular/core/testing';
import { UserService } from '../services/user.service';
import { provideMockActions } from '@ngrx/effects/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { LoadUser, UserActionTypes } from './user.actions';

describe('UserEffects', () => {
let actions$: Observable<Action>;
let effects: UserEffects;
let service: UserService;
const userInfo: User = {
name: 'Jerson Morocho',
email: '[email protected]',
roles: [],
groups: [],
id: 'cc925a5d-9644-4a4f-8d99-0bee49aadd05',
tenant_id: null,
deleted: null
};

beforeEach(() => {
TestBed.configureTestingModule({
providers: [UserEffects, provideMockActions(() => actions$)],
imports: [HttpClientTestingModule],
});

effects = TestBed.inject(UserEffects);
service = TestBed.inject(UserService);
});

it('should be created', async () => {
expect(effects).toBeTruthy();
});

it('action type is LOAD_USER_SUCCESS when service is executed successfully', async () => {
const userId = 'dd4a1571-b025-41c9-b35f-810841b43134';
const serviceSpy = spyOn(service, 'loadUser');

actions$ = of(new LoadUser(userId));
serviceSpy.and.returnValue(of(userInfo));

effects.loadUserInfo$.subscribe((action) => {
expect(action.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS);
});
});

it('action type is LOAD_USER_FAIL when service fail in execution', async () => {
const userId = 'dd4a1571-b025-41c9-b35f-810841b43134';
const serviceSpy = spyOn(service, 'loadUser');

actions$ = of(new LoadUser(userId));
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } }));

effects.loadUserInfo$.subscribe((action) => {
expect(action.type).toEqual(UserActionTypes.LOAD_USER_FAIL);
});
});
});
2 changes: 1 addition & 1 deletion src/app/modules/user/store/user.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Actions, ofType, Effect } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { UserService } from './user.service';
import { UserService } from '../services/user.service';
import * as actions from './user.actions';

@Injectable()
Expand Down
44 changes: 44 additions & 0 deletions src/app/modules/user/store/user.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { userReducer } from './user.reducer';
import { LoadUser, LoadUserFail, LoadUserSuccess } from './user.actions';
import { User } from '../models/user';

describe('userReducer', () => {
const initState = {
name: '',
email: '',
roles: [],
groups: [],
}

it('on LoadUser, state equal to initState', () => {
const userId = 'dd4a1571-b025-41c9-b35f-810841b43134';
const action = new LoadUser(userId);
const state = userReducer(initState, action);

expect(state).toEqual(initState);
});

it('on LoadUserSuccess, userFound is saved in store', () => {
const userFound: User = {
name: 'Jerson Morocho',
email: '[email protected]',
roles: [],
groups: [],
id: 'dd4a1571-b025-41c9-b35f-810841b43134',
tenant_id: null,
deleted: null
};

const action = new LoadUserSuccess(userFound);
const state = userReducer(initState, action);

expect(state).toEqual(userFound);
});

it('on LoadUserFail, state equal to initState', () => {
const action = new LoadUserFail('error');
const state = userReducer(initState, action);

expect(state).toEqual(initState);
});
});
21 changes: 21 additions & 0 deletions src/app/modules/user/store/user.selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getUserInfo } from './user.selectors';
import { User } from '../models/user';

describe('UserSelectors', () => {
const userInfo: User = {
name: 'Jerson Morocho',
email: '[email protected]',
roles: [],
groups: [],
id: 'cc925a5d-9644-4a4f-8d99-0bee49aadd05',
tenant_id: null,
deleted: null
};

it('should select user info', () => {
const result = getUserInfo.projector(userInfo);

expect(userInfo.email).toEqual('[email protected]');
});

});
21 changes: 0 additions & 21 deletions src/app/modules/user/store/user.service.ts

This file was deleted.