-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5f9b1d
commit 2736b4d
Showing
11 changed files
with
439 additions
and
8 deletions.
There are no files selected for viewing
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
174 changes: 174 additions & 0 deletions
174
.../upgrade/src/app/core/stratification-factors/store/stratification-factors.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,174 @@ | ||
import { fakeAsync, tick } from '@angular/core/testing'; | ||
import { ActionsSubject } from '@ngrx/store'; | ||
import { BehaviorSubject, of, throwError } from 'rxjs'; | ||
import { StratificationFactorsEffects } from './stratification-factors.effects'; | ||
import * as StratificationFactorsActions from './stratification-factors.actions'; | ||
import { selectAllStratificationFactors } from './stratification-factors.selectors'; | ||
import { StratificationFactor } from './stratification-factors.model'; | ||
|
||
describe('StratificationFactorsEffects', () => { | ||
let actions$: ActionsSubject; | ||
let store$: any; | ||
let stratificationFactorsDataService: any; | ||
let router: any; | ||
let service: StratificationFactorsEffects; | ||
const mockData: StratificationFactor = { factor: 'favourite_food', values: { pizza: 10, burger: 5 } }; | ||
|
||
beforeEach(() => { | ||
actions$ = new ActionsSubject(); | ||
store$ = new BehaviorSubject({}); | ||
store$.dispatch = jest.fn(); | ||
stratificationFactorsDataService = {}; | ||
router = { | ||
navigate: jest.fn(), | ||
}; | ||
|
||
service = new StratificationFactorsEffects(actions$, router, store$, stratificationFactorsDataService); | ||
}); | ||
|
||
describe('fetchStratificationFactors$', () => { | ||
it('should dispatch actionFetchStratificationFactorsSuccess on API call success', fakeAsync(() => { | ||
stratificationFactorsDataService.fetchStratificationFactors = jest.fn().mockReturnValue(of(mockData[0])); | ||
selectAllStratificationFactors.setResult(mockData[0]); | ||
|
||
const expectedAction = StratificationFactorsActions.actionFetchStratificationFactorsSuccess({ | ||
stratificationFactors: mockData[0], | ||
}); | ||
|
||
service.fetchStratificationFactors$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
}); | ||
|
||
actions$.next( | ||
StratificationFactorsActions.actionFetchStratificationFactors({ isLoadingStratificationFactors: true }) | ||
); | ||
|
||
tick(0); | ||
})); | ||
|
||
it('should dispatch actionFetchStratificationFactorsFailure on API call failure', fakeAsync(() => { | ||
stratificationFactorsDataService.fetchStratificationFactors = jest | ||
.fn() | ||
.mockReturnValue(throwError(() => new Error('test'))); | ||
const expectedAction = StratificationFactorsActions.actionFetchStratificationFactorsFailure(); | ||
|
||
service.fetchStratificationFactors$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
}); | ||
|
||
actions$.next( | ||
StratificationFactorsActions.actionFetchStratificationFactors({ isLoadingStratificationFactors: true }) | ||
); | ||
|
||
tick(0); | ||
})); | ||
}); | ||
|
||
describe('deleteStratificationFactor$', () => { | ||
it('should dispatch actionDeleteStratificationFactorSuccess and navigate to participants page on success', fakeAsync(() => { | ||
stratificationFactorsDataService.deleteStratificationFactor = jest.fn().mockReturnValue(of([{ ...mockData }])); | ||
|
||
const expectedAction = StratificationFactorsActions.actionDeleteStratificationFactorSuccess({ | ||
stratificationFactor: { ...mockData }, | ||
}); | ||
console.log('expectedAction: ', expectedAction); | ||
service.deleteStratificationFactor$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
expect(router.navigate).toHaveBeenCalledWith(['/participants']); | ||
}); | ||
|
||
actions$.next( | ||
StratificationFactorsActions.actionDeleteStratificationFactor({ factor: { ...mockData[0] }.factor }) | ||
); | ||
|
||
tick(0); | ||
})); | ||
|
||
it('should dispatch actionDeleteStratificationFactorFailure on API call failure', fakeAsync(() => { | ||
stratificationFactorsDataService.deleteStratificationFactor = jest | ||
.fn() | ||
.mockReturnValue(throwError(() => new Error('test'))); | ||
|
||
const expectedAction = StratificationFactorsActions.actionDeleteStratificationFactorFailure(); | ||
|
||
service.deleteStratificationFactor$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
}); | ||
|
||
actions$.next(StratificationFactorsActions.actionDeleteStratificationFactor({ factor: mockData.factor })); | ||
|
||
tick(0); | ||
})); | ||
}); | ||
|
||
describe('importStratificationFactor$', () => { | ||
it('should dispatch actionImportStratificationFactorSuccess on success', fakeAsync(() => { | ||
const mockCSVData = 'data,test'; | ||
stratificationFactorsDataService.importStratificationFactors = jest.fn().mockReturnValue(of(null)); | ||
|
||
const expectedAction = StratificationFactorsActions.actionImportStratificationFactorSuccess(); | ||
|
||
service.importStratificationFactor$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
}); | ||
|
||
actions$.next(StratificationFactorsActions.actionImportStratificationFactor({ csvData: mockCSVData })); | ||
|
||
tick(0); | ||
})); | ||
|
||
it('should dispatch actionImportStratificationFactorFailure on API call failure', fakeAsync(() => { | ||
const mockCSVData = 'data,test'; | ||
stratificationFactorsDataService.importStratificationFactors = jest | ||
.fn() | ||
.mockReturnValue(throwError(() => new Error('test'))); | ||
|
||
const expectedAction = StratificationFactorsActions.actionImportStratificationFactorFailure(); | ||
|
||
service.importStratificationFactor$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
}); | ||
|
||
actions$.next(StratificationFactorsActions.actionImportStratificationFactor({ csvData: mockCSVData })); | ||
|
||
tick(0); | ||
})); | ||
}); | ||
|
||
describe('exportStratificationFactor$', () => { | ||
it('should dispatch actionExportStratificationFactorSuccess on success', fakeAsync(() => { | ||
const mockCSVFactor = 'uuid,factor1\nstudent1,1\nstudent2,0'; | ||
stratificationFactorsDataService.exportStratificationFactor = jest.fn().mockReturnValue(of(mockCSVFactor)); | ||
document.body.removeChild = jest.fn(); | ||
|
||
const expectedAction = StratificationFactorsActions.actionExportStratificationFactorSuccess(); | ||
service.exportStratificationFactor$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
|
||
// Check if the download method was called | ||
expect(document.body.removeChild).toHaveBeenCalled(); | ||
}); | ||
|
||
actions$.next(StratificationFactorsActions.actionExportStratificationFactor({ factor: mockCSVFactor })); | ||
|
||
tick(0); | ||
})); | ||
|
||
it('should dispatch actionExportStratificationFactorFailure on API call failure', fakeAsync(() => { | ||
const mockFactor = 'favourite_food'; | ||
stratificationFactorsDataService.exportStratificationFactor = jest | ||
.fn() | ||
.mockReturnValue(throwError(() => new Error('test'))); | ||
|
||
const expectedAction = StratificationFactorsActions.actionExportStratificationFactorFailure(); | ||
|
||
service.exportStratificationFactor$.subscribe((result) => { | ||
expect(result).toEqual(expectedAction); | ||
}); | ||
|
||
actions$.next(StratificationFactorsActions.actionExportStratificationFactor({ factor: mockFactor })); | ||
|
||
tick(0); | ||
})); | ||
}); | ||
}); |
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
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
90 changes: 90 additions & 0 deletions
90
...upgrade/src/app/core/stratification-factors/store/stratification-factors.reducers.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,90 @@ | ||
import { stratificationFactorsReducer, initialState } from './stratification-factors.reducers'; | ||
import * as StratificationFactorsActions from '../store/stratification-factors.actions'; | ||
import { StratificationFactor } from './stratification-factors.model'; | ||
|
||
const mockStratificationFactor: StratificationFactor = { | ||
factor: 'favourite_food', | ||
values: { pizza: 10, burger: 5 }, | ||
}; | ||
describe('StratificationFactorsReducer', () => { | ||
describe('actionFetchStratificationFactors', () => { | ||
it('should set isLoadingStratificationFactors to true', () => { | ||
const previousState = { ...initialState }; | ||
previousState.isLoadingStratificationFactors = true; | ||
|
||
const testAction = StratificationFactorsActions.actionFetchStratificationFactors({ | ||
isLoadingStratificationFactors: true, | ||
}); | ||
|
||
const newState = stratificationFactorsReducer(previousState, testAction); | ||
|
||
expect(newState.isLoadingStratificationFactors).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('actionFetchStratificationFactorsSuccess', () => { | ||
it('should set stratificationFactors and isLoadingStratificationFactors to false', () => { | ||
const previousState = { ...initialState }; | ||
previousState.isLoadingStratificationFactors = false; | ||
|
||
const testAction = StratificationFactorsActions.actionFetchStratificationFactorsSuccess({ | ||
stratificationFactors: [mockStratificationFactor], | ||
}); | ||
|
||
const newState = stratificationFactorsReducer(previousState, testAction); | ||
|
||
expect(newState.entities[mockStratificationFactor.factor]).toEqual(mockStratificationFactor); | ||
expect(newState.isLoadingStratificationFactors).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('actions to request failures and set isLoadingStratificationFactors to false', () => { | ||
const testActions = { | ||
actionFetchStratificationFactorsFailure: StratificationFactorsActions.actionFetchStratificationFactorsFailure, | ||
actionDeleteStratificationFactorFailure: StratificationFactorsActions.actionDeleteStratificationFactorFailure, | ||
}; | ||
|
||
for (const actionKey in testActions) { | ||
const previousState = { ...initialState }; | ||
previousState.isLoadingStratificationFactors = false; | ||
|
||
const newState = stratificationFactorsReducer(previousState, testActions[actionKey]()); | ||
|
||
it(`on ${actionKey} reducer should return a state with isLoadingStratificationFactors: false`, () => { | ||
expect(newState.isLoadingStratificationFactors).toEqual(false); | ||
}); | ||
} | ||
}); | ||
|
||
describe('actionDeleteStratificationFactorSuccess', () => { | ||
it('should remove stratification factor from entities', () => { | ||
const previousState = { ...initialState }; | ||
previousState.entities = { | ||
[mockStratificationFactor.factor]: mockStratificationFactor, | ||
}; | ||
|
||
const testAction = StratificationFactorsActions.actionDeleteStratificationFactorSuccess({ | ||
stratificationFactor: mockStratificationFactor, | ||
}); | ||
|
||
const newState = stratificationFactorsReducer(previousState, testAction); | ||
|
||
expect(newState.entities[mockStratificationFactor.factor]).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('actionSetIsLoadingStratificationFactors', () => { | ||
it('should set boolean for isLoadingStratificationFactors', () => { | ||
const previousState = { ...initialState }; | ||
previousState.isLoadingStratificationFactors = false; | ||
|
||
const testAction = StratificationFactorsActions.actionSetIsLoadingStratificationFactors({ | ||
isLoadingStratificationFactors: true, | ||
}); | ||
|
||
const newState = stratificationFactorsReducer(previousState, testAction); | ||
|
||
expect(newState.isLoadingStratificationFactors).toEqual(true); | ||
}); | ||
}); | ||
}); |
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
39 changes: 39 additions & 0 deletions
39
...pgrade/src/app/core/stratification-factors/store/stratification-factors.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,39 @@ | ||
import { initialState } from './stratification-factors.reducers'; | ||
import * as StratificationFactorsSelectors from './stratification-factors.selectors'; | ||
|
||
describe('StratificationFactorsSelectors', () => { | ||
const mockState = { ...initialState }; | ||
|
||
describe('#selectIsLoadingStratificationFactors', () => { | ||
it('should return boolean from isLoadingStratificationFactors', () => { | ||
const previousState = { ...mockState }; | ||
previousState.isLoadingStratificationFactors = false; | ||
|
||
const result = StratificationFactorsSelectors.selectIsLoadingStratificationFactors.projector(previousState); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('#selectAllStratificationFactors', () => { | ||
it('should return all stratification factors', () => { | ||
const previousState = { ...mockState }; | ||
previousState.totalStratificationFactors = []; | ||
|
||
const result = StratificationFactorsSelectors.selectAllStratificationFactors.projector(previousState); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('#selectTotalStratificationFactors', () => { | ||
it('should return the total count of stratification factors', () => { | ||
const previousState = { ...mockState }; | ||
previousState.totalStratificationFactors = 1; | ||
|
||
const result = StratificationFactorsSelectors.selectTotalStratificationFactors.projector(previousState); | ||
|
||
expect(result).toEqual(1); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.