|
| 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 | +}); |
0 commit comments