Skip to content

Commit

Permalink
chore(test-users): test user reducer and action
Browse files Browse the repository at this point in the history
- adds tests for the userReducer
- adds tests for the user action
- ensures that all tests added are passing
- ensures that tests added have 100% coverage

[Finishes #161178877]
  • Loading branch information
Chidiebere Oguejiofor committed Oct 15, 2018
1 parent 44e6d87 commit f924407
Show file tree
Hide file tree
Showing 3 changed files with 3,536 additions and 3,400 deletions.
99 changes: 99 additions & 0 deletions client/src/tests/redux/actions/users/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint max-nested-callbacks: off */
import moxios from "moxios";
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import instance from '../../../../config/axios';
import { searchUser, clearUser } from "../../../../../src/redux/actions/users";
import { IS_LOADING, IS_LOGGED_IN, SEARCH_USERS } from "../../../../../src/redux/actions/types";

const middleware = [thunk];
const mockStore = configureMockStore(middleware);


describe('Testing usersAction', () => {
beforeEach(() => {
moxios.install(instance);
});

afterEach(() => {
moxios.uninstall(instance);
});

describe('clearUser', () => {
it('should dispatch success action wih users object being empty', (done) => {
const store = mockStore({});
store.dispatch(clearUser());
const actions = store.getActions();
expect(actions[0].type)
.toBe(SEARCH_USERS);
expect(actions[0].payload)
.toEqual({
data: { users: [] }
});
done();
});
});
describe('searchUser', () => {
it('the searchUser should dispatch actions in the correct order when it succeeds', (done) => {
const store = mockStore({});
const mockResponse = {
data: 'mockData'
};
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: mockResponse
});
});
const mockQuery = 'mockQuery';

return store.dispatch(searchUser(mockQuery))
.then(() => {
const actions = store.getActions();
expect(actions[0].type)
.toBe(IS_LOGGED_IN);
expect(actions[1].type)
.toBe(IS_LOADING);
expect(actions[2].type)
.toBe(SEARCH_USERS);
expect(actions[1].type)
.toBe(IS_LOADING);
done();
});
});


it('the searchUser should dispatch actions in the correct order when it fails', (done) => {
const store = mockStore({});
const mockResponse = {
error: 'error',
status: 'failed'
};
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.reject({
status: 400,
response: mockResponse
});
});

return store.dispatch(searchUser())
.then(() => {
const actions = store.getActions();
expect(actions[0].type)
.toBe(IS_LOGGED_IN);
expect(actions[1].type)
.toBe(IS_LOADING);
expect(actions[2].type)
.toBe(SEARCH_USERS);
expect(actions[2].payload)
.toBe(mockResponse);
expect(actions[1].type)
.toBe(IS_LOADING);
done();
done();
});
});
});
});
37 changes: 37 additions & 0 deletions client/src/tests/redux/reducers/userReducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import userReducer from '../../../../src/redux/reducers/userReducer';
import { SEARCH_USERS } from "../../../../src/redux/actions/types";

const initialState = {
users: { data: { users: [] } }
};
describe('Testing userReducer', () => {
it('should return a default state when the action type does not exist', () => {
const mockState = {
mock: 'mock'
};

const newState = userReducer(mockState, { type: 'unknown-type' });
expect(newState)
.toBe(mockState);
});
it('should return the initialState when the state is undefined', () => {
const mockAction = { type: 'mock-action-type' };

const newState = userReducer(undefined, mockAction);
expect(newState)
.toEqual(initialState);
});

it(`should set the users with action.payload when the action.type === ${SEARCH_USERS}`, () => {
const expected = 'expected-users';
const mockAction = {
type: SEARCH_USERS,
payload: expected
};

const newState = userReducer({}, mockAction);
expect(newState.users)
.toBe(expected);
});
});

Loading

0 comments on commit f924407

Please sign in to comment.