This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
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.
feat(#104): added test cases for user redux tree
- Loading branch information
Andreas Gasser
committed
Apr 8, 2019
1 parent
1108e90
commit c132141
Showing
4 changed files
with
190 additions
and
0 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,142 @@ | ||
/* global it testUtils */ | ||
import GraphApi from '../../../util/GraphApi'; | ||
|
||
import reducer, * as reduxUser from '../index'; | ||
|
||
import * as selectorsAuth from '../../auth/selectors'; | ||
|
||
jest.mock('../../../util/GraphApi'); | ||
|
||
const { __testables__ } = reduxUser; | ||
|
||
const mockedData = { | ||
user: { | ||
email: 'test.user@test.com', | ||
firstname: 'Test', | ||
lastname: 'User', | ||
}, | ||
userId: 'ca57a08b-2b14-44df-a39b-2a175c5a6294', | ||
}; | ||
|
||
describe('user: simple action test suite', () => { | ||
it('should create userSetUser action', () => { | ||
const { user } = mockedData; | ||
expect(__testables__.userSetUser(user)) | ||
.toEqual({ | ||
type: __testables__.USER_SET_USER, | ||
payload: user, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('user: complex action test suite', () => { | ||
const API = new GraphApi(); | ||
const mockstore = testUtils.createMockStoreWithApi(API); | ||
|
||
let dateNowMock; | ||
let authUserIdSelectorMock; | ||
|
||
beforeAll(() => { | ||
dateNowMock = jest.spyOn(Date, 'now').mockImplementation(() => 1553237036202); | ||
authUserIdSelectorMock = jest.spyOn(selectorsAuth, 'authUserIdSelector') | ||
.mockImplementation(() => mockedData.userId); | ||
}); | ||
|
||
afterEach(() => { | ||
API.resetMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
dateNowMock.mockClear(); | ||
authUserIdSelectorMock.mockClear(); | ||
}); | ||
|
||
it('should handle updateUser', async (done) => { | ||
const { user } = mockedData; | ||
|
||
// mock api response | ||
API.mockMutationResponseOnce({ updateUser: { user } }); | ||
|
||
// prepare expected actions | ||
const HOC_ACTIONS = testUtils.createHocActions({ | ||
baseType: 'USER_UPDATE_USER_REQUEST', | ||
payload: { updateUser: { user } }, | ||
}); | ||
|
||
const store = mockstore(); | ||
const { dispatch } = store; | ||
|
||
const expectedActions = [ | ||
HOC_ACTIONS.START, | ||
__testables__.userSetUser(user), | ||
HOC_ACTIONS.SUCCESS, | ||
]; | ||
|
||
await reduxUser.updateUser(user)(dispatch); | ||
|
||
expect(store.getActions()).toEqual(expectedActions); | ||
|
||
done(); | ||
}); | ||
|
||
it('should handle getUserInfo', async (done) => { | ||
const { user } = mockedData; | ||
|
||
// mock api response | ||
API.mockQueryResponseOnce({ getUserInfo: user }); | ||
|
||
// prepare expected actions | ||
const HOC_ACTIONS = testUtils.createHocActions({ | ||
baseType: 'USER_GET_USER_INFO_REQUEST', | ||
payload: { getUserInfo: user }, | ||
}); | ||
|
||
const store = mockstore(); | ||
const { dispatch } = store; | ||
|
||
const expectedActions = [ | ||
HOC_ACTIONS.START, | ||
__testables__.userSetUser(user), | ||
HOC_ACTIONS.SUCCESS, | ||
]; | ||
|
||
await reduxUser.getUserInfo()(dispatch, {}); | ||
|
||
expect(API.query.mock.calls[0][1]) | ||
.toEqual({ userId: mockedData.userId }); | ||
|
||
expect(store.getActions()).toEqual(expectedActions); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
describe('user: reducer test suite', () => { | ||
const initialUser = { | ||
firstname: '', | ||
lastname: '', | ||
email: '', | ||
}; | ||
|
||
const initialState = { | ||
user: initialUser, | ||
userInfoRequest: testUtils.createHocReducerState(), | ||
updateUserRequest: testUtils.createHocReducerState(), | ||
}; | ||
|
||
const dummyTestAction = testUtils.dummyTestAction(); | ||
|
||
it('should create initial store state', () => { | ||
expect(reducer(undefined, dummyTestAction)) | ||
.toEqual(initialState); | ||
}); | ||
|
||
it('should handle USER_SET_USER', () => { | ||
const { user } = mockedData; | ||
expect(reducer(undefined, __testables__.userSetUser(user))) | ||
.toEqual({ | ||
...initialState, | ||
user, | ||
}); | ||
}); | ||
}); |
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 @@ | ||
/* global testUtils */ | ||
import * as selectors from '../selectors'; | ||
|
||
describe('user selector test suite', () => { | ||
const initialState = { | ||
user: { | ||
user: { | ||
email: 'test.user@test.com', | ||
firstname: 'Test', | ||
lastname: 'User', | ||
}, | ||
userInfoRequest: testUtils.createHocReducerState(), | ||
updateUserRequest: testUtils.createHocReducerState(), | ||
} | ||
}; | ||
|
||
it('should handle userStateSelector', () => { | ||
expect(selectors.__testables__.userStateSelector(initialState)) | ||
.toEqual(initialState.user); | ||
}) | ||
|
||
it('should handle userStateSelector', () => { | ||
expect(selectors.userSelector(initialState)) | ||
.toEqual(initialState.user.user); | ||
}); | ||
|
||
it('should handle getUserInfoRequestSelector', () => { | ||
expect(selectors.getUserInfoRequestSelector(initialState)) | ||
.toEqual(initialState.user.userInfoRequest); | ||
}); | ||
|
||
it('should handle updateUserRequestSelector', () => { | ||
expect(selectors.updateUserRequestSelector(initialState)) | ||
.toEqual(initialState.user.updateUserRequest); | ||
}); | ||
|
||
}); |
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
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