Skip to content

Commit

Permalink
[#14] Refactor some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manh-t committed Jul 28, 2023
1 parent a758024 commit edc3325
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/screens/Dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import DashboardEmpty from 'components/Dashboard/Empty';
import DashboardHeader from 'components/Dashboard/Header';
import { getDaysAgoFromISODate, getdddMMMDDDateFromISODate } from 'helpers/datetime';
import { useAppDispatch, useAppSelector } from 'hooks';
import { getSurveysAsyncThunk, surveysAction } from 'store/reducers/Surveys';
import { getUserAsyncThunk } from 'store/reducers/User';
import { getSurveys, surveysAction } from 'store/reducers/Surveys';
import { getUser } from 'store/reducers/User';

const DashBoardScreen = (): JSX.Element => {
const { surveys, currentPosition, isInitialLoading } = useAppSelector((state) => state.surveys);
Expand All @@ -29,8 +29,8 @@ const DashBoardScreen = (): JSX.Element => {
};

useEffect(() => {
dispatch(getSurveysAsyncThunk());
dispatch(getUserAsyncThunk());
dispatch(getSurveys());
dispatch(getUser());
}, [dispatch]);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/store/reducers/Surveys/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const surveysReducers = {
},
};

export const getSurveysThunkCreator: AsyncThunkPayloadCreator<Survey[], void, JSONObject> = async () => {
export const getSurveysThunkCreator: AsyncThunkPayloadCreator<Survey[], void, JSONObject> = () => {
return getSurveys().then((response: DeserializableListResponse) => {
const surveys = deserializeList<Survey>(response.data);

Expand Down
46 changes: 19 additions & 27 deletions src/store/reducers/Surveys/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { AxiosResponse } from 'axios';

import { getSurveys } from 'adapters/Survey';
import { getSurveys as getSurveysAdapter } from 'adapters/Survey';

import { SurveysState, getSurveysAsyncThunk, surveysSlice } from '.';
import { getSurveys, surveysSlice } from '.';
import { surveysReducers } from './actions';

jest.mock('adapters/Survey');
Expand Down Expand Up @@ -37,36 +37,34 @@ describe('surveys slice', () => {
isInitialLoading: true,
};
describe('nextSurvey', () => {
it('changes the current position to next position', () => {
const mockState: SurveysState = { ...mockInitialState };
beforeEach(() => {
mockInitialState.currentPosition = 0;
});

surveysReducers.nextSurvey(mockState);
it('changes the current position to next position', () => {
surveysReducers.nextSurvey(mockInitialState);

expect(mockState.currentPosition).toBe(1);
expect(mockInitialState.currentPosition).toBe(1);
});

it("resets the current position when the next position is larger than the total surveys's indexes", () => {
const mockState: SurveysState = { ...mockInitialState };
surveysReducers.nextSurvey(mockInitialState);
surveysReducers.nextSurvey(mockInitialState);
surveysReducers.nextSurvey(mockInitialState);

surveysReducers.nextSurvey(mockState);
surveysReducers.nextSurvey(mockState);
surveysReducers.nextSurvey(mockState);

expect(mockState.currentPosition).toBe(0);
expect(mockInitialState.currentPosition).toBe(0);
});
});

describe('selectSurvey', () => {
it('changes the current position to the selected position', () => {
const mockState: SurveysState = { ...mockInitialState };

surveysReducers.selectSurvey(mockState, { type: 'selectSurvey', payload: 2 });
surveysReducers.selectSurvey(mockInitialState, { type: 'selectSurvey', payload: 2 });

expect(mockState.currentPosition).toBe(2);
expect(mockInitialState.currentPosition).toBe(2);
});
});

describe('getSurveysAsyncThunk', () => {
describe('getSurveys', () => {
const successResponse = {
data: [
{
Expand Down Expand Up @@ -100,10 +98,10 @@ describe('surveys slice', () => {
};

it('calls getSurveys API successfully', async () => {
(getSurveys as jest.Mock).mockResolvedValue(successResponse as AxiosResponse);
(getSurveysAdapter as jest.Mock).mockResolvedValue(successResponse as AxiosResponse);
const dispatch = jest.fn();

const getSurveysFunction = getSurveysAsyncThunk();
const getSurveysFunction = getSurveys();

const getSurveysPayload = await getSurveysFunction(dispatch, () => {}, undefined);

Expand Down Expand Up @@ -132,12 +130,6 @@ describe('surveys slice', () => {
];

expect(getSurveysPayload.payload).toEqual(expectedResult);

expect(dispatch).toHaveBeenNthCalledWith(1, getSurveysAsyncThunk.pending(getSurveysPayload.meta.requestId));
expect(dispatch).toHaveBeenNthCalledWith(
2,
getSurveysAsyncThunk.fulfilled(expectedResult, getSurveysPayload.meta.requestId)
);
});
});

Expand All @@ -147,7 +139,7 @@ describe('surveys slice', () => {
currentPosition: 0,
isInitialLoading: true,
};
describe('getSurveysAsyncThunk.fulfilled', () => {
describe('getSurveys.fulfilled', () => {
it('returns the surveys', async () => {
const expectedResult = [
{
Expand Down Expand Up @@ -182,7 +174,7 @@ describe('surveys slice', () => {
});
});

describe('getSurveysAsyncThunk.rejected', () => {
describe('getSurveys.rejected', () => {
it('returns no surveys', async () => {
const dispatchedState = surveysSlice.reducer(mockEmptyState, {
type: 'surveys/getSurveys/rejected',
Expand Down
6 changes: 3 additions & 3 deletions src/store/reducers/Surveys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ export const initialState: SurveysState = {
isInitialLoading: true,
};

export const getSurveysAsyncThunk = createAsyncThunk('surveys/getSurveys', getSurveysThunkCreator);
export const getSurveys = createAsyncThunk('surveys/getSurveys', getSurveysThunkCreator);

export const surveysSlice = createSlice({
name: 'surveys',
initialState,
reducers: surveysReducers,
extraReducers: (builder) => {
builder.addCase(getSurveysAsyncThunk.fulfilled, (state, action) => {
builder.addCase(getSurveys.fulfilled, (state, action) => {
state.isInitialLoading = false;
state.surveys = action.payload;
});
builder.addCase(getSurveysAsyncThunk.rejected, (state) => {
builder.addCase(getSurveys.rejected, (state) => {
state.isInitialLoading = false;
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/store/reducers/User/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeserializableResponse, deserialize } from 'helpers/deserializer';
import { JSONObject } from 'helpers/json';
import { User } from 'types/user';

export const getUserThunkCreator: AsyncThunkPayloadCreator<User, void, JSONObject> = async () => {
export const getUserThunkCreator: AsyncThunkPayloadCreator<User, void, JSONObject> = () => {
return getUserInfo().then((response: DeserializableResponse) => {
const user = deserialize<User>(response.data);

Expand Down
12 changes: 6 additions & 6 deletions src/store/reducers/User/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { AxiosResponse } from 'axios';

import { getUserInfo } from 'adapters/User';

import { getUserAsyncThunk, userSlice } from '.';
import { getUser, userSlice } from '.';

jest.mock('adapters/User');

describe('user slice', () => {
describe('getUserAsyncThunk', () => {
describe('getUser', () => {
const successResponse = {
data: {
id: '1',
Expand All @@ -25,7 +25,7 @@ describe('user slice', () => {
(getUserInfo as jest.Mock).mockResolvedValue(successResponse as AxiosResponse);
const dispatch = jest.fn();

const getUserFunction = getUserAsyncThunk();
const getUserFunction = getUser();

const getUserPayload = await getUserFunction(dispatch, () => {}, undefined);

Expand All @@ -39,14 +39,14 @@ describe('user slice', () => {

expect(getUserPayload.payload).toEqual(expectedResult);

expect(dispatch).toHaveBeenNthCalledWith(1, getUserAsyncThunk.pending(getUserPayload.meta.requestId));
expect(dispatch).toHaveBeenNthCalledWith(2, getUserAsyncThunk.fulfilled(expectedResult, getUserPayload.meta.requestId));
expect(dispatch).toHaveBeenNthCalledWith(1, getUser.pending(getUserPayload.meta.requestId));
expect(dispatch).toHaveBeenNthCalledWith(2, getUser.fulfilled(expectedResult, getUserPayload.meta.requestId));
});
});

describe('extraReducers', () => {
const mockEmptyState = {};
describe('getUserAsyncThunk.fulfilled', () => {
describe('getUser.fulfilled', () => {
it('returns the user info', async () => {
const expectedResult = {
id: '1',
Expand Down
4 changes: 2 additions & 2 deletions src/store/reducers/User/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export interface UserState {

export const initialState: UserState = {};

export const getUserAsyncThunk = createAsyncThunk('user/getUser', getUserThunkCreator);
export const getUser = createAsyncThunk('user/getUser', getUserThunkCreator);

export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(getUserAsyncThunk.fulfilled, (state, action) => {
builder.addCase(getUser.fulfilled, (state, action) => {
state.user = action.payload;
});
},
Expand Down

0 comments on commit edc3325

Please sign in to comment.