Skip to content

Commit a970234

Browse files
thegreatyamoriAngeluz-07
authored andcommitted
test: TT-155 Added unit tests for user ngrx flow
1 parent cf2c16a commit a970234

File tree

6 files changed

+156
-22
lines changed

6 files changed

+156
-22
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { LoadUserFail, LoadUserSuccess, UserActionTypes } from './user.actions';
2+
3+
import { User } from '../models/user';
4+
5+
describe('Actions for User', () => {
6+
it('LoadUserSuccess type is UserActionTypes.LOAD_USER_SUCCESS', () => {
7+
const user: User = {
8+
name: 'Jerson Morocho',
9+
10+
roles: [],
11+
groups: [],
12+
id: 'dd4a1571-b025-41c9-b35f-810841b43134',
13+
tenant_id: 'cc925a5d-9644-4a4f-8d99-0bee49aadd05',
14+
deleted: ''
15+
};
16+
17+
const loadUserSuccess = new LoadUserSuccess(user);
18+
19+
expect(loadUserSuccess.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS);
20+
});
21+
22+
it('LoadUserFail type is UserActionTypes.LOAD_USER_FAIL', () => {
23+
const loadUserFail = new LoadUserFail('error');
24+
25+
expect(loadUserFail.type).toEqual(UserActionTypes.LOAD_USER_FAIL);
26+
});
27+
28+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Observable, of, throwError } from 'rxjs';
2+
import { Action } from '@ngrx/store';
3+
import { User } from '../models/user';
4+
import { UserEffects } from './user.effects';
5+
import { TestBed } from '@angular/core/testing';
6+
import { UserService } from '../services/user.service';
7+
import { provideMockActions } from '@ngrx/effects/testing';
8+
import { HttpClientTestingModule } from '@angular/common/http/testing';
9+
import { LoadUser, UserActionTypes } from './user.actions';
10+
11+
describe('UserEffects', () => {
12+
let actions$: Observable<Action>;
13+
let effects: UserEffects;
14+
let service: UserService;
15+
const userInfo: User = {
16+
name: 'Jerson Morocho',
17+
18+
roles: [],
19+
groups: [],
20+
id: 'cc925a5d-9644-4a4f-8d99-0bee49aadd05',
21+
tenant_id: null,
22+
deleted: null
23+
};
24+
25+
beforeEach(() => {
26+
TestBed.configureTestingModule({
27+
providers: [UserEffects, provideMockActions(() => actions$)],
28+
imports: [HttpClientTestingModule],
29+
});
30+
31+
effects = TestBed.inject(UserEffects);
32+
service = TestBed.inject(UserService);
33+
});
34+
35+
it('should be created', async () => {
36+
expect(effects).toBeTruthy();
37+
});
38+
39+
it('action type is LOAD_USER_SUCCESS when service is executed successfully', async () => {
40+
const userId = 'dd4a1571-b025-41c9-b35f-810841b43134';
41+
const serviceSpy = spyOn(service, 'loadUser');
42+
43+
actions$ = of(new LoadUser(userId));
44+
serviceSpy.and.returnValue(of(userInfo));
45+
46+
effects.loadUserInfo$.subscribe((action) => {
47+
expect(action.type).toEqual(UserActionTypes.LOAD_USER_SUCCESS);
48+
});
49+
});
50+
51+
it('action type is LOAD_USER_FAIL when service fail in execution', async () => {
52+
const userId = 'dd4a1571-b025-41c9-b35f-810841b43134';
53+
const serviceSpy = spyOn(service, 'loadUser');
54+
55+
actions$ = of(new LoadUser(userId));
56+
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } }));
57+
58+
effects.loadUserInfo$.subscribe((action) => {
59+
expect(action.type).toEqual(UserActionTypes.LOAD_USER_FAIL);
60+
});
61+
});
62+
});

src/app/modules/user/store/user.effects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Actions, ofType, Effect } from '@ngrx/effects';
33
import { Action } from '@ngrx/store';
44
import { Observable, of } from 'rxjs';
55
import { catchError, map, mergeMap } from 'rxjs/operators';
6-
import { UserService } from './user.service';
6+
import { UserService } from '../services/user.service';
77
import * as actions from './user.actions';
88

99
@Injectable()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { userReducer } from './user.reducer';
2+
import { LoadUser, LoadUserFail, LoadUserSuccess } from './user.actions';
3+
import { User } from '../models/user';
4+
5+
describe('userReducer', () => {
6+
const initState = {
7+
name: '',
8+
email: '',
9+
roles: [],
10+
groups: [],
11+
}
12+
13+
it('on LoadUser, state equal to initState', () => {
14+
const userId = 'dd4a1571-b025-41c9-b35f-810841b43134';
15+
const action = new LoadUser(userId);
16+
const state = userReducer(initState, action);
17+
18+
expect(state).toEqual(initState);
19+
});
20+
21+
it('on LoadUserSuccess, userFound is saved in store', () => {
22+
const userFound: User = {
23+
name: 'Jerson Morocho',
24+
25+
roles: [],
26+
groups: [],
27+
id: 'dd4a1571-b025-41c9-b35f-810841b43134',
28+
tenant_id: null,
29+
deleted: null
30+
};
31+
32+
const action = new LoadUserSuccess(userFound);
33+
const state = userReducer(initState, action);
34+
35+
expect(state).toEqual(userFound);
36+
});
37+
38+
it('on LoadUserFail, state equal to initState', () => {
39+
const action = new LoadUserFail('error');
40+
const state = userReducer(initState, action);
41+
42+
expect(state).toEqual(initState);
43+
});
44+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getUserInfo } from './user.selectors';
2+
import { User } from '../models/user';
3+
4+
describe('UserSelectors', () => {
5+
const userInfo: User = {
6+
name: 'Jerson Morocho',
7+
8+
roles: [],
9+
groups: [],
10+
id: 'cc925a5d-9644-4a4f-8d99-0bee49aadd05',
11+
tenant_id: null,
12+
deleted: null
13+
};
14+
15+
it('should select user info', () => {
16+
const result = getUserInfo.projector(userInfo);
17+
18+
expect(userInfo.email).toEqual('[email protected]');
19+
});
20+
21+
});

src/app/modules/user/store/user.service.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)