-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test-users): test user reducer and action
- 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
Showing
3 changed files
with
3,536 additions
and
3,400 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
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); | ||
|
Oops, something went wrong.