Skip to content

Commit

Permalink
test: #576 add test to effects files and selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRC-ioet committed Nov 23, 2020
1 parent 1b8bfd1 commit 13de42e
Show file tree
Hide file tree
Showing 11 changed files with 778 additions and 2 deletions.
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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@ describe('ActivityManagement Selectors', () => {
expect(activityFound).toEqual(activities[0]);
});

it('should return all the data in the state when the selector allActivities is called', () => {
const activities = [{id: 'id', name: 'abc', description: 'xxx'},
{id: '2', name: 'xyz', description: 'yyy'}];
const activityState = {data: activities};

expect(selectors.allActivities.projector(activityState)).toBe(activities);
});

it('should select isLoading when the selector getIsLoading is called', () => {
const isLoadingValue = true;
const activityState = { isLoading: isLoadingValue };

expect(selectors.getIsLoading.projector(activityState)).toBe(isLoadingValue);
});

});
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);
});
});
});
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);
});
});

0 comments on commit 13de42e

Please sign in to comment.