Просмотр исходного кода

added unit tests

pull/113/head
meris.ahmatovic 3 лет назад
Родитель
Сommit
c14bfa1bb2

+ 50
- 1
src/__tests__/ReduxTests/inviteDialog.test.js Просмотреть файл

@@ -12,7 +12,11 @@ import { setUsersError } from "../../store/actions/users/usersActions";
import { getUsers, invite } from "../../store/saga/usersSaga";
import InviteDialog from "../../components/MUI/InviteDialog";
import * as userReqs from "../../store/actions/users/usersActions";
import { inviteUserSuccess } from "../../store/actions/users/usersActions";
import {
inviteUserSuccess,
inviteUserError,
} from "../../store/actions/users/usersActions";
import * as helper from "../../util/helpers/rejectErrorCodeHelper";

const props = {
title: "Any",
@@ -37,6 +41,7 @@ describe("invite dialog reducer tests onSuccess", () => {
let spyOnUseSelector;
let spyOnUseDispatch;
let mockDispatch;
let spyHelper;

beforeEach(() => {
// Mock useSelector hook
@@ -54,6 +59,9 @@ describe("invite dialog reducer tests onSuccess", () => {
// Mock dispatch function returned from useDispatch
mockDispatch = jest.fn();
spyOnUseDispatch.mockReturnValueOnce(mockDispatch);

// Mock rejectErrorCodeHelper
spyHelper = jest.spyOn(helper, "rejectErrorCodeHelper")
});

afterEach(() => {
@@ -157,4 +165,45 @@ describe("invite reducer tests default", () => {
expect(api.inviteUserRequest.mock.calls.length).toBe(1);
expect(dispatchedActions).toContainEqual(inviteUserSuccess());
});

it("should run inviteUser saga function with error actions in case of api error", async () => {
const dispatchedActions = [];

helper.rejectErrorCodeHelper = jest.fn(()=> 'Server error')

// we don't want to perform an actual api call in our tests
// so we will mock the getAllUsers api with jest
// this will mutate the dependency which we may reset if other tests
// are dependent on it
const mockError = {
response: {
data: { message: "Server error" },
},
};
api.inviteUserRequest = jest.fn(() => Promise.reject(mockError));

const fakeStore = {
getState: () => ({
invite: {
isSuccess: false,
errorMessage: "",
},
}),
dispatch: (action) => dispatchedActions.push(action),
};

// wait for saga to complete
await runSaga(fakeStore, invite, {
payload: {
firstName: "st",
lastName: "Test",
email: "test@dilig.net",
},
}).done;

expect(api.inviteUserRequest.mock.calls.length).toBe(1);
expect(dispatchedActions).toContainEqual(
inviteUserError("Server error")
);
});
});

+ 19
- 14
src/__tests__/ReduxTests/userManagementReducer.test.js Просмотреть файл

@@ -10,6 +10,7 @@ import { runSaga } from "redux-saga";
import { FETCH_USERS_REQ } from "../../store/actions/users/usersActionConstants";
import { setUsersError } from "../../store/actions/users/usersActions";
import { getUsers } from "../../store/saga/usersSaga";
import { useTranslation } from 'react-i18next';

describe("Stats reducer tests", () => {
const cont = (
@@ -48,22 +49,26 @@ describe("Stats reducer tests", () => {
});
});

// it('should handle users load errors in case of failure', async () => {
// const dispatchedActions = [];
it("should handle users load errors in case of failure", async () => {
const dispatchedActions = [];

// // we simulate an error by rejecting the promise
// // then we assert if our saga dispatched the action(s) correctly
// const error = { response: { data: { message: mockState.users.errorMessage } } };
// api.getAllUsers = jest.fn(() => Promise.reject(error));
// we simulate an error by rejecting the promise
// then we assert if our saga dispatched the action(s) correctly
const error = {
response: { data: { message: mockState.selections.fetchSelectionsErrorMessage } },
};
api.getAllUsers = jest.fn(() => Promise.reject(error));

// const fakeStore = {
// getState: () => (mockState.users.users),
// dispatch: action => dispatchedActions.push(action),
// };
const fakeStore = {
getState: () => mockState.users.users,
dispatch: (action) => dispatchedActions.push(action),
};

// await runSaga(fakeStore, getUsers).done;
await runSaga(fakeStore, getUsers).done;

// expect(api.getAllUsers.mock.calls.length).toBe(1);
// expect(dispatchedActions).toContainEqual(setUsersError(error.response.data.message));
// });
expect(api.getAllUsers.mock.calls.length).toBe(1);
expect(dispatchedActions).toContainEqual(
setUsersError(undefined)
);
});
});

Загрузка…
Отмена
Сохранить