Skip to content

Commit 13de42e

Browse files
committed
test: #576 add test to effects files and selectors
1 parent 1b8bfd1 commit 13de42e

File tree

11 files changed

+778
-2
lines changed

11 files changed

+778
-2
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { Activity } from '../../shared/models/activity.model';
2+
import { TestBed } from '@angular/core/testing';
3+
import { provideMockActions } from '@ngrx/effects/testing';
4+
import { Action } from '@ngrx/store';
5+
import { Observable, of, throwError } from 'rxjs';
6+
import { ActivityService } from '../services/activity.service';
7+
import { ActivityManagementActionTypes } from './activity-management.actions';
8+
import { ActivityEffects } from './activity-management.effects';
9+
import { HttpClientTestingModule } from '@angular/common/http/testing';
10+
import { ToastrModule, ToastrService } from 'ngx-toastr';
11+
import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY } from '../../shared/messages';
12+
13+
describe('ActivityEffects', () => {
14+
let actions$: Observable<Action>;
15+
let effects: ActivityEffects;
16+
let service: ActivityService;
17+
let toastrService;
18+
const activity: Activity = { id: 'id', name: 'name', description: 'description', tenant_id: 'tenantId' };
19+
const activityList: Activity[] = [];
20+
21+
beforeEach(() => {
22+
TestBed.configureTestingModule({
23+
providers: [ActivityEffects, provideMockActions(() => actions$)],
24+
imports: [HttpClientTestingModule, ToastrModule.forRoot()],
25+
declarations: [],
26+
});
27+
effects = TestBed.inject(ActivityEffects);
28+
service = TestBed.inject(ActivityService);
29+
toastrService = TestBed.inject(ToastrService);
30+
});
31+
32+
it('should be created', async () => {
33+
expect(effects).toBeTruthy();
34+
});
35+
36+
it('action type is LOAD_ACTIVITIES_SUCCESS when service is executed sucessfully', async () => {
37+
actions$ = of({ type: ActivityManagementActionTypes.LOAD_ACTIVITIES });
38+
const serviceSpy = spyOn(service, 'getActivities');
39+
serviceSpy.and.returnValue(of(activityList));
40+
41+
effects.getActivities$.subscribe((action) => {
42+
expect(action.type).toEqual(ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS);
43+
});
44+
});
45+
46+
it('action type is LOAD_ACTIVITIES_FAIL when service fail in execution', async () => {
47+
actions$ = of({ type: ActivityManagementActionTypes.LOAD_ACTIVITIES });
48+
const serviceSpy = spyOn(service, 'getActivities');
49+
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } }));
50+
spyOn(toastrService, 'error');
51+
52+
effects.getActivities$.subscribe((action) => {
53+
expect(toastrService.error).toHaveBeenCalled();
54+
expect(action.type).toEqual(ActivityManagementActionTypes.LOAD_ACTIVITIES_FAIL);
55+
});
56+
});
57+
58+
it('action type is UPDATE_ACTIVITIES_SUCCESS when service is executed sucessfully', async () => {
59+
actions$ = of({ type: ActivityManagementActionTypes.UPDATE_ACTIVITY, activity });
60+
spyOn(service, 'updateActivity').and.returnValue(of(activity));
61+
spyOn(toastrService, 'success');
62+
63+
effects.updateActivity$.subscribe((action) => {
64+
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY);
65+
expect(action.type).toEqual(ActivityManagementActionTypes.UPDATE_ACTIVITY_SUCCESS);
66+
});
67+
});
68+
69+
it('action type is UPDATE_ACTIVITIES_FAIL when service fail in execution', async () => {
70+
actions$ = of({ type: ActivityManagementActionTypes.UPDATE_ACTIVITY, activity });
71+
spyOn(service, 'updateActivity').and.returnValue(throwError({ error: { message: 'fail!' } }));
72+
spyOn(toastrService, 'error');
73+
74+
effects.updateActivity$.subscribe((action) => {
75+
expect(toastrService.error).toHaveBeenCalled();
76+
expect(action.type).toEqual(ActivityManagementActionTypes.UPDATE_ACTIVITY_FAIL);
77+
});
78+
});
79+
80+
it('action type is CREATE_ACTIVITY_SUCCESS when service is executed sucessfully', async () => {
81+
actions$ = of({ type: ActivityManagementActionTypes.CREATE_ACTIVITY, activity });
82+
spyOn(service, 'createActivity').and.returnValue(of(activity));
83+
spyOn(toastrService, 'success');
84+
85+
effects.createActivity$.subscribe((action) => {
86+
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY);
87+
expect(action.type).toEqual(ActivityManagementActionTypes.CREATE_ACTIVITY_SUCCESS);
88+
});
89+
});
90+
91+
it('action type is CREATE_ACTIVITY_FAIL when service fail in execution', async () => {
92+
actions$ = of({ type: ActivityManagementActionTypes.CREATE_ACTIVITY, activity });
93+
spyOn(service, 'createActivity').and.returnValue(throwError({ error: { message: 'fail!' } }));
94+
spyOn(toastrService, 'error');
95+
96+
effects.createActivity$.subscribe((action) => {
97+
expect(toastrService.error).toHaveBeenCalled();
98+
expect(action.type).toEqual(ActivityManagementActionTypes.CREATE_ACTIVITY_FAIL);
99+
});
100+
});
101+
102+
it('action type is DELETE_ACTIVITY_SUCCESS when service is executed sucessfully', async () => {
103+
const activityId = 'activityId';
104+
actions$ = of({ type: ActivityManagementActionTypes.DELETE_ACTIVITY, activityId });
105+
spyOn(service, 'deleteActivity').and.returnValue(of({}));
106+
spyOn(toastrService, 'success');
107+
108+
effects.deleteActivity$.subscribe((action) => {
109+
expect(toastrService.success).toHaveBeenCalledWith(INFO_DELETE_SUCCESSFULLY);
110+
expect(action.type).toEqual(ActivityManagementActionTypes.DELETE_ACTIVITY_SUCCESS);
111+
});
112+
});
113+
114+
it('action type is DELETE_ACTIVITY_FAIL when service fail in execution', async () => {
115+
const activityId = 'activityId';
116+
actions$ = of({ type: ActivityManagementActionTypes.DELETE_ACTIVITY, activityId });
117+
spyOn(service, 'deleteActivity').and.returnValue(throwError({ error: { message: 'fail!' } }));
118+
spyOn(toastrService, 'error');
119+
120+
effects.deleteActivity$.subscribe((action) => {
121+
expect(toastrService.error).toHaveBeenCalled();
122+
expect(action.type).toEqual(ActivityManagementActionTypes.DELETE_ACTIVITY_FAIL);
123+
});
124+
});
125+
});

src/app/modules/activities-management/store/activity-management.selectors.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,19 @@ describe('ActivityManagement Selectors', () => {
1616
expect(activityFound).toEqual(activities[0]);
1717
});
1818

19+
it('should return all the data in the state when the selector allActivities is called', () => {
20+
const activities = [{id: 'id', name: 'abc', description: 'xxx'},
21+
{id: '2', name: 'xyz', description: 'yyy'}];
22+
const activityState = {data: activities};
23+
24+
expect(selectors.allActivities.projector(activityState)).toBe(activities);
25+
});
26+
27+
it('should select isLoading when the selector getIsLoading is called', () => {
28+
const isLoadingValue = true;
29+
const activityState = { isLoading: isLoadingValue };
30+
31+
expect(selectors.getIsLoading.projector(activityState)).toBe(isLoadingValue);
32+
});
33+
1934
});
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { ProjectType } from '../../../../shared/models/project-type.model';
2+
import { TestBed } from '@angular/core/testing';
3+
import { provideMockActions } from '@ngrx/effects/testing';
4+
import { Action } from '@ngrx/store';
5+
import { Observable, of, throwError } from 'rxjs';
6+
import { ProjectTypeService } from '../services/project-type.service';
7+
import { ProjectTypeActionTypes } from './project-type.actions';
8+
import { ProjectTypeEffects } from './project-type.effects';
9+
import { HttpClientTestingModule } from '@angular/common/http/testing';
10+
import { ToastrModule, ToastrService } from 'ngx-toastr';
11+
import { INFO_DELETE_SUCCESSFULLY, INFO_SAVED_SUCCESSFULLY } from '../../../../shared/messages';
12+
13+
describe('ProjectTypeEffects', () => {
14+
let actions$: Observable<Action>;
15+
let effects: ProjectTypeEffects;
16+
let service: ProjectTypeService;
17+
let toastrService;
18+
const projectType: ProjectType = { id: 'id', name: 'name', description: 'description' };
19+
const projectTypes: ProjectType[] = [];
20+
beforeEach(() => {
21+
TestBed.configureTestingModule({
22+
providers: [ProjectTypeEffects, provideMockActions(() => actions$)],
23+
imports: [HttpClientTestingModule, ToastrModule.forRoot()],
24+
declarations: [],
25+
});
26+
effects = TestBed.inject(ProjectTypeEffects);
27+
service = TestBed.inject(ProjectTypeService);
28+
toastrService = TestBed.inject(ToastrService);
29+
});
30+
31+
it('SHOULD be created', async () => {
32+
expect(effects).toBeTruthy();
33+
});
34+
35+
it('action type is LOAD_PROJECT_TYPES_SUCCESS when service is executed sucessfully', async () => {
36+
actions$ = of({ type: ProjectTypeActionTypes.LOAD_PROJECT_TYPES });
37+
const serviceSpy = spyOn(service, 'getProjectTypes');
38+
serviceSpy.and.returnValue(of(projectTypes));
39+
40+
effects.getProjectTypes$.subscribe((action) => {
41+
expect(action.type).toEqual(ProjectTypeActionTypes.LOAD_PROJECT_TYPES_SUCCESS);
42+
});
43+
});
44+
45+
it('action type is LOAD_PROJECT_TYPES_FAIL when service fail in execution', async () => {
46+
actions$ = of({ type: ProjectTypeActionTypes.LOAD_PROJECT_TYPES });
47+
const serviceSpy = spyOn(service, 'getProjectTypes');
48+
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } }));
49+
spyOn(toastrService, 'error');
50+
51+
effects.getProjectTypes$.subscribe((action) => {
52+
expect(toastrService.error).toHaveBeenCalled();
53+
expect(action.type).toEqual(ProjectTypeActionTypes.LOAD_PROJECT_TYPES_FAIL);
54+
});
55+
});
56+
57+
it('action type is UPDATE_PROJECT_TYPE_SUCCESS when service is executed sucessfully', async () => {
58+
actions$ = of({ type: ProjectTypeActionTypes.UPDATE_PROJECT_TYPE, projectType });
59+
spyOn(toastrService, 'success');
60+
spyOn(service, 'updateProjectType').and.returnValue(of(projectType));
61+
62+
effects.updateProjectType$.subscribe((action) => {
63+
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY);
64+
expect(action.type).toEqual(ProjectTypeActionTypes.UPDATE_PROJECT_TYPE_SUCCESS);
65+
});
66+
});
67+
68+
it('action type is UPDATE_PROJECT_TYPE_FAIL when service fail in execution', async () => {
69+
actions$ = of({ type: ProjectTypeActionTypes.UPDATE_PROJECT_TYPE, projectType });
70+
spyOn(toastrService, 'error');
71+
spyOn(service, 'updateProjectType').and.returnValue(throwError({ error: { message: 'fail!' } }));
72+
73+
effects.updateProjectType$.subscribe((action) => {
74+
expect(toastrService.error).toHaveBeenCalled();
75+
expect(action.type).toEqual(ProjectTypeActionTypes.UPDATE_PROJECT_TYPE_FAIL);
76+
});
77+
});
78+
79+
it('action type is CREATE_PROJECT_TYPE_SUCCESS when service is executed sucessfully', async () => {
80+
actions$ = of({ type: ProjectTypeActionTypes.CREATE_PROJECT_TYPE, payload: projectType });
81+
spyOn(toastrService, 'success');
82+
spyOn(service, 'createProjectType').and.returnValue(of(projectType));
83+
84+
effects.createProjectType$.subscribe((action) => {
85+
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY);
86+
expect(action.type).toEqual(ProjectTypeActionTypes.CREATE_PROJECT_TYPE_SUCCESS);
87+
});
88+
});
89+
90+
it('action type is CREATE_PROJECT_TYPE_FAIL when service fail in execution', async () => {
91+
actions$ = of({ type: ProjectTypeActionTypes.CREATE_PROJECT_TYPE, payload: projectType });
92+
spyOn(toastrService, 'error');
93+
spyOn(service, 'createProjectType').and.returnValue(throwError({ error: { message: 'fail!' } }));
94+
95+
effects.createProjectType$.subscribe((action) => {
96+
expect(toastrService.error).toHaveBeenCalled();
97+
expect(action.type).toEqual(ProjectTypeActionTypes.CREATE_PROJECT_TYPE_FAIL);
98+
});
99+
});
100+
101+
it('action type is DELETE_PROJECT_TYPE_SUCCESS when service is executed sucessfully', async () => {
102+
const projectTypeId = 'projectTypeId';
103+
actions$ = of({ type: ProjectTypeActionTypes.DELETE_PROJECT_TYPE, projectTypeId });
104+
spyOn(toastrService, 'success');
105+
spyOn(service, 'deleteProjectType').and.returnValue(of({}));
106+
107+
effects.deleteProjectType$.subscribe((action) => {
108+
expect(toastrService.success).toHaveBeenCalledWith(INFO_DELETE_SUCCESSFULLY);
109+
expect(action.type).toEqual(ProjectTypeActionTypes.DELETE_PROJECT_TYPE_SUCCESS);
110+
});
111+
});
112+
113+
it('action type is DELETE_PROJECT_TYPE_FAIL when service fail in execution', async () => {
114+
const projectTypeId = 'projectTypeId';
115+
actions$ = of({ type: ProjectTypeActionTypes.DELETE_PROJECT_TYPE, projectTypeId });
116+
spyOn(toastrService, 'error');
117+
spyOn(service, 'deleteProjectType').and.returnValue(throwError({ error: { message: 'fail!' } }));
118+
119+
effects.deleteProjectType$.subscribe((action) => {
120+
expect(toastrService.error).toHaveBeenCalled();
121+
expect(action.type).toEqual(ProjectTypeActionTypes.DELETE_PROJECT_TYPE_FAIL);
122+
});
123+
});
124+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as selectors from './project-type.selectors';
2+
import { ProjectType } from '../../../../shared/models/project-type.model';
3+
describe('ProjectTypeSelectors', () => {
4+
it('should select allProjectTypes', () => {
5+
const projectType = [{ id: 'id', name: 'abc', description: 'xxx' }];
6+
const projectTypeState = { data: projectType };
7+
8+
expect(selectors.allProjectTypes.projector(projectTypeState)).toBe(projectType);
9+
});
10+
11+
it('should select projectTypeIdToEdit', () => {
12+
const projectType = 'projectTypeId';
13+
const projectTypeState = { projectTypeIdToEdit: projectType };
14+
15+
expect(selectors.projectTypeIdToEdit.projector(projectTypeState)).toBe(projectType);
16+
});
17+
18+
it('should select getProjectTypeById', () => {
19+
const projectTypes = [
20+
{ id: 'id', name: 'abc', description: 'xxx' },
21+
{ id: 'id2', name: 'abc2', description: 'xxx' },
22+
];
23+
const projectTypeId = 'id';
24+
const projectTypeExpect = { id: 'id', name: 'abc', description: 'xxx' };
25+
26+
expect(selectors.getProjectTypeById.projector(projectTypes, projectTypeId)).toEqual(projectTypeExpect);
27+
});
28+
});

0 commit comments

Comments
 (0)