-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: #576 add test to effects files and selectors
- Loading branch information
1 parent
1b8bfd1
commit 13de42e
Showing
11 changed files
with
778 additions
and
2 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
src/app/modules/activities-management/store/activity-management.effects.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Activity } from '../../shared/models/activity.model'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { provideMockActions } from '@ngrx/effects/testing'; | ||
import { Action } from '@ngrx/store'; | ||
import { Observable, of, throwError } from 'rxjs'; | ||
import { ActivityService } from '../services/activity.service'; | ||
import { ActivityManagementActionTypes } from './activity-management.actions'; | ||
import { ActivityEffects } from './activity-management.effects'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { ToastrModule, ToastrService } from 'ngx-toastr'; | ||
import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY } from '../../shared/messages'; | ||
|
||
describe('ActivityEffects', () => { | ||
let actions$: Observable<Action>; | ||
let effects: ActivityEffects; | ||
let service: ActivityService; | ||
let toastrService; | ||
const activity: Activity = { id: 'id', name: 'name', description: 'description', tenant_id: 'tenantId' }; | ||
const activityList: Activity[] = []; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ActivityEffects, provideMockActions(() => actions$)], | ||
imports: [HttpClientTestingModule, ToastrModule.forRoot()], | ||
declarations: [], | ||
}); | ||
effects = TestBed.inject(ActivityEffects); | ||
service = TestBed.inject(ActivityService); | ||
toastrService = TestBed.inject(ToastrService); | ||
}); | ||
|
||
it('should be created', async () => { | ||
expect(effects).toBeTruthy(); | ||
}); | ||
|
||
it('action type is LOAD_ACTIVITIES_SUCCESS when service is executed sucessfully', async () => { | ||
actions$ = of({ type: ActivityManagementActionTypes.LOAD_ACTIVITIES }); | ||
const serviceSpy = spyOn(service, 'getActivities'); | ||
serviceSpy.and.returnValue(of(activityList)); | ||
|
||
effects.getActivities$.subscribe((action) => { | ||
expect(action.type).toEqual(ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is LOAD_ACTIVITIES_FAIL when service fail in execution', async () => { | ||
actions$ = of({ type: ActivityManagementActionTypes.LOAD_ACTIVITIES }); | ||
const serviceSpy = spyOn(service, 'getActivities'); | ||
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
spyOn(toastrService, 'error'); | ||
|
||
effects.getActivities$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.LOAD_ACTIVITIES_FAIL); | ||
}); | ||
}); | ||
|
||
it('action type is UPDATE_ACTIVITIES_SUCCESS when service is executed sucessfully', async () => { | ||
actions$ = of({ type: ActivityManagementActionTypes.UPDATE_ACTIVITY, activity }); | ||
spyOn(service, 'updateActivity').and.returnValue(of(activity)); | ||
spyOn(toastrService, 'success'); | ||
|
||
effects.updateActivity$.subscribe((action) => { | ||
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.UPDATE_ACTIVITY_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is UPDATE_ACTIVITIES_FAIL when service fail in execution', async () => { | ||
actions$ = of({ type: ActivityManagementActionTypes.UPDATE_ACTIVITY, activity }); | ||
spyOn(service, 'updateActivity').and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
spyOn(toastrService, 'error'); | ||
|
||
effects.updateActivity$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.UPDATE_ACTIVITY_FAIL); | ||
}); | ||
}); | ||
|
||
it('action type is CREATE_ACTIVITY_SUCCESS when service is executed sucessfully', async () => { | ||
actions$ = of({ type: ActivityManagementActionTypes.CREATE_ACTIVITY, activity }); | ||
spyOn(service, 'createActivity').and.returnValue(of(activity)); | ||
spyOn(toastrService, 'success'); | ||
|
||
effects.createActivity$.subscribe((action) => { | ||
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.CREATE_ACTIVITY_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is CREATE_ACTIVITY_FAIL when service fail in execution', async () => { | ||
actions$ = of({ type: ActivityManagementActionTypes.CREATE_ACTIVITY, activity }); | ||
spyOn(service, 'createActivity').and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
spyOn(toastrService, 'error'); | ||
|
||
effects.createActivity$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.CREATE_ACTIVITY_FAIL); | ||
}); | ||
}); | ||
|
||
it('action type is DELETE_ACTIVITY_SUCCESS when service is executed sucessfully', async () => { | ||
const activityId = 'activityId'; | ||
actions$ = of({ type: ActivityManagementActionTypes.DELETE_ACTIVITY, activityId }); | ||
spyOn(service, 'deleteActivity').and.returnValue(of({})); | ||
spyOn(toastrService, 'success'); | ||
|
||
effects.deleteActivity$.subscribe((action) => { | ||
expect(toastrService.success).toHaveBeenCalledWith(INFO_DELETE_SUCCESSFULLY); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.DELETE_ACTIVITY_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is DELETE_ACTIVITY_FAIL when service fail in execution', async () => { | ||
const activityId = 'activityId'; | ||
actions$ = of({ type: ActivityManagementActionTypes.DELETE_ACTIVITY, activityId }); | ||
spyOn(service, 'deleteActivity').and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
spyOn(toastrService, 'error'); | ||
|
||
effects.deleteActivity$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ActivityManagementActionTypes.DELETE_ACTIVITY_FAIL); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...p/modules/customer-management/components/projects-type/store/project-type.effects.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { ProjectType } from '../../../../shared/models/project-type.model'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { provideMockActions } from '@ngrx/effects/testing'; | ||
import { Action } from '@ngrx/store'; | ||
import { Observable, of, throwError } from 'rxjs'; | ||
import { ProjectTypeService } from '../services/project-type.service'; | ||
import { ProjectTypeActionTypes } from './project-type.actions'; | ||
import { ProjectTypeEffects } from './project-type.effects'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { ToastrModule, ToastrService } from 'ngx-toastr'; | ||
import { INFO_DELETE_SUCCESSFULLY, INFO_SAVED_SUCCESSFULLY } from '../../../../shared/messages'; | ||
|
||
describe('ProjectTypeEffects', () => { | ||
let actions$: Observable<Action>; | ||
let effects: ProjectTypeEffects; | ||
let service: ProjectTypeService; | ||
let toastrService; | ||
const projectType: ProjectType = { id: 'id', name: 'name', description: 'description' }; | ||
const projectTypes: ProjectType[] = []; | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ProjectTypeEffects, provideMockActions(() => actions$)], | ||
imports: [HttpClientTestingModule, ToastrModule.forRoot()], | ||
declarations: [], | ||
}); | ||
effects = TestBed.inject(ProjectTypeEffects); | ||
service = TestBed.inject(ProjectTypeService); | ||
toastrService = TestBed.inject(ToastrService); | ||
}); | ||
|
||
it('SHOULD be created', async () => { | ||
expect(effects).toBeTruthy(); | ||
}); | ||
|
||
it('action type is LOAD_PROJECT_TYPES_SUCCESS when service is executed sucessfully', async () => { | ||
actions$ = of({ type: ProjectTypeActionTypes.LOAD_PROJECT_TYPES }); | ||
const serviceSpy = spyOn(service, 'getProjectTypes'); | ||
serviceSpy.and.returnValue(of(projectTypes)); | ||
|
||
effects.getProjectTypes$.subscribe((action) => { | ||
expect(action.type).toEqual(ProjectTypeActionTypes.LOAD_PROJECT_TYPES_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is LOAD_PROJECT_TYPES_FAIL when service fail in execution', async () => { | ||
actions$ = of({ type: ProjectTypeActionTypes.LOAD_PROJECT_TYPES }); | ||
const serviceSpy = spyOn(service, 'getProjectTypes'); | ||
serviceSpy.and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
spyOn(toastrService, 'error'); | ||
|
||
effects.getProjectTypes$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.LOAD_PROJECT_TYPES_FAIL); | ||
}); | ||
}); | ||
|
||
it('action type is UPDATE_PROJECT_TYPE_SUCCESS when service is executed sucessfully', async () => { | ||
actions$ = of({ type: ProjectTypeActionTypes.UPDATE_PROJECT_TYPE, projectType }); | ||
spyOn(toastrService, 'success'); | ||
spyOn(service, 'updateProjectType').and.returnValue(of(projectType)); | ||
|
||
effects.updateProjectType$.subscribe((action) => { | ||
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.UPDATE_PROJECT_TYPE_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is UPDATE_PROJECT_TYPE_FAIL when service fail in execution', async () => { | ||
actions$ = of({ type: ProjectTypeActionTypes.UPDATE_PROJECT_TYPE, projectType }); | ||
spyOn(toastrService, 'error'); | ||
spyOn(service, 'updateProjectType').and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
|
||
effects.updateProjectType$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.UPDATE_PROJECT_TYPE_FAIL); | ||
}); | ||
}); | ||
|
||
it('action type is CREATE_PROJECT_TYPE_SUCCESS when service is executed sucessfully', async () => { | ||
actions$ = of({ type: ProjectTypeActionTypes.CREATE_PROJECT_TYPE, payload: projectType }); | ||
spyOn(toastrService, 'success'); | ||
spyOn(service, 'createProjectType').and.returnValue(of(projectType)); | ||
|
||
effects.createProjectType$.subscribe((action) => { | ||
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.CREATE_PROJECT_TYPE_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is CREATE_PROJECT_TYPE_FAIL when service fail in execution', async () => { | ||
actions$ = of({ type: ProjectTypeActionTypes.CREATE_PROJECT_TYPE, payload: projectType }); | ||
spyOn(toastrService, 'error'); | ||
spyOn(service, 'createProjectType').and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
|
||
effects.createProjectType$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.CREATE_PROJECT_TYPE_FAIL); | ||
}); | ||
}); | ||
|
||
it('action type is DELETE_PROJECT_TYPE_SUCCESS when service is executed sucessfully', async () => { | ||
const projectTypeId = 'projectTypeId'; | ||
actions$ = of({ type: ProjectTypeActionTypes.DELETE_PROJECT_TYPE, projectTypeId }); | ||
spyOn(toastrService, 'success'); | ||
spyOn(service, 'deleteProjectType').and.returnValue(of({})); | ||
|
||
effects.deleteProjectType$.subscribe((action) => { | ||
expect(toastrService.success).toHaveBeenCalledWith(INFO_DELETE_SUCCESSFULLY); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.DELETE_PROJECT_TYPE_SUCCESS); | ||
}); | ||
}); | ||
|
||
it('action type is DELETE_PROJECT_TYPE_FAIL when service fail in execution', async () => { | ||
const projectTypeId = 'projectTypeId'; | ||
actions$ = of({ type: ProjectTypeActionTypes.DELETE_PROJECT_TYPE, projectTypeId }); | ||
spyOn(toastrService, 'error'); | ||
spyOn(service, 'deleteProjectType').and.returnValue(throwError({ error: { message: 'fail!' } })); | ||
|
||
effects.deleteProjectType$.subscribe((action) => { | ||
expect(toastrService.error).toHaveBeenCalled(); | ||
expect(action.type).toEqual(ProjectTypeActionTypes.DELETE_PROJECT_TYPE_FAIL); | ||
}); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
...modules/customer-management/components/projects-type/store/project-type.selectors.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as selectors from './project-type.selectors'; | ||
import { ProjectType } from '../../../../shared/models/project-type.model'; | ||
describe('ProjectTypeSelectors', () => { | ||
it('should select allProjectTypes', () => { | ||
const projectType = [{ id: 'id', name: 'abc', description: 'xxx' }]; | ||
const projectTypeState = { data: projectType }; | ||
|
||
expect(selectors.allProjectTypes.projector(projectTypeState)).toBe(projectType); | ||
}); | ||
|
||
it('should select projectTypeIdToEdit', () => { | ||
const projectType = 'projectTypeId'; | ||
const projectTypeState = { projectTypeIdToEdit: projectType }; | ||
|
||
expect(selectors.projectTypeIdToEdit.projector(projectTypeState)).toBe(projectType); | ||
}); | ||
|
||
it('should select getProjectTypeById', () => { | ||
const projectTypes = [ | ||
{ id: 'id', name: 'abc', description: 'xxx' }, | ||
{ id: 'id2', name: 'abc2', description: 'xxx' }, | ||
]; | ||
const projectTypeId = 'id'; | ||
const projectTypeExpect = { id: 'id', name: 'abc', description: 'xxx' }; | ||
|
||
expect(selectors.getProjectTypeById.projector(projectTypes, projectTypeId)).toEqual(projectTypeExpect); | ||
}); | ||
}); |
Oops, something went wrong.