Skip to content

Commit

Permalink
SRS frontend test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ppratikcr7 committed Oct 10, 2023
1 parent b5f9b1d commit 2736b4d
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StratificationFactor } from './stratification-factors.model';

export const actionFetchStratificationFactors = createAction(
'[Stratification Factors] Fetch Stratification Factors',
props<{ isLoading: boolean }>()
props<{ isLoadingStratificationFactors: boolean }>()
);

export const actionFetchStratificationFactorsSuccess = createAction(
Expand Down
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);
}));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class StratificationFactorsEffects {
filter(({ csvData }) => !!csvData),
switchMap(({ csvData }) =>
this.stratificationFactorsDataService.importStratificationFactors(csvData).pipe(
map((data) => {
map(() => {
return StratificationFactorsActions.actionImportStratificationFactorSuccess();
}),
catchError(() => [StratificationFactorsActions.actionImportStratificationFactorFailure()])
Expand Down Expand Up @@ -100,6 +100,8 @@ export class StratificationFactorsEffects {
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvData); // data is the text response of the http request.
hiddenElement.target = '_blank';
hiddenElement.download = value + '.csv';
document.body.appendChild(hiddenElement);
hiddenElement.click();
document.body.removeChild(hiddenElement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export interface StratificationFactorSimple {
}

export interface StratificationFactorsState extends EntityState<StratificationFactor> {
isLoading: boolean;
isLoadingStratificationFactors: boolean;
totalStratificationFactors: any;
}
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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const adapter: EntityAdapter<StratificationFactor> = createEntityAdapter<
export const { selectIds, selectEntities, selectAll, selectTotal } = adapter.getSelectors();

export const initialState: StratificationFactorsState = adapter.getInitialState({
isLoading: false,
isLoadingStratificationFactors: false,
totalStratificationFactors: null,
});

Expand Down
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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const selectAllStratificationFactors = createSelector(selectStratificatio

export const selectIsLoadingStratificationFactors = createSelector(
selectStratificationFactorsState,
(state) => state.isLoading
(state) => state.isLoadingStratificationFactors
);

export const selectTotalStratificationFactors = createSelector(
Expand Down
Loading

0 comments on commit 2736b4d

Please sign in to comment.