-
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(request): Member should be able to create permission request
- create action that consume the request endpoint - create reducer to manage the state - test the action and the reducer [Finishes #161059614]
- Loading branch information
Jacob Nouwatin
authored and
Jacob Nouwatin
committed
Oct 11, 2018
1 parent
339c708
commit a72ccd5
Showing
13 changed files
with
231 additions
and
10 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
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,15 @@ | ||
import instance from '../../../config/axios'; | ||
import { CREATE_ADMIN_REQUEST_SUCCESS, CREATE_ADMIN_REQUEST_ERROR } from '../types'; | ||
import { success, isErrored } from '../index'; | ||
|
||
const createAdminRequest = requestData => dispatch => instance | ||
.post('/requests', requestData) | ||
.then((response) => { | ||
if (response.data.errors) { | ||
return dispatch(isErrored(CREATE_ADMIN_REQUEST_ERROR, response.data.errors[0])); | ||
} | ||
return dispatch(success(CREATE_ADMIN_REQUEST_SUCCESS, response.data)); | ||
}) | ||
.catch((error) => dispatch(isErrored(CREATE_ADMIN_REQUEST_ERROR, error))); | ||
|
||
export default createAdminRequest; |
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
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,28 @@ | ||
import { CREATE_ADMIN_REQUEST_SUCCESS, CREATE_ADMIN_REQUEST_ERROR } from '../actions/types'; | ||
|
||
const initialState = { | ||
request: null, | ||
success: false, | ||
error: '' | ||
}; | ||
|
||
const requestReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case CREATE_ADMIN_REQUEST_SUCCESS: | ||
return { | ||
...state, | ||
success: true, | ||
request: action.payload | ||
}; | ||
case CREATE_ADMIN_REQUEST_ERROR: | ||
return { | ||
...state, | ||
error: action.payload, | ||
success: false | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default requestReducer; |
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,15 @@ | ||
const requestsResponse = { | ||
data: { | ||
request: { | ||
id: "cffda78d-d218-49e7-9382-0afba282cd8a", | ||
type: "admin_request", | ||
data: "123", | ||
userId: "76e999da-31b0-4f5e-ba34-b66bc58b93f1", | ||
updatedAt: "2018-10-10T06:53:47.967Z", | ||
createdAt: "2018-10-10T06:53:47.967Z", | ||
teamId: null | ||
} | ||
} | ||
}; | ||
|
||
export default requestsResponse; |
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,48 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import moxios from 'moxios'; | ||
import instance from '../../../../config/axios'; | ||
import { CREATE_ADMIN_REQUEST_SUCCESS, CREATE_ADMIN_REQUEST_ERROR } from '../../../../../src/redux/actions/types'; | ||
import createAdminRequest from '../../../../redux/actions/requests'; | ||
import requestsReponse from '../../../__mockData__/requestsMock'; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
describe('Request Actions', () => { | ||
beforeEach(() => moxios.install(instance)); | ||
afterEach(() => moxios.uninstall(instance)); | ||
it('should dispatch CREATE_ADMIN_REQUEST_SUCCESS after successfull admin request sent', () => { | ||
const store = mockStore({}); | ||
moxios.stubRequest('/requests', { | ||
status: 200, | ||
response: requestsReponse | ||
}); | ||
const expectedAction = { | ||
type: CREATE_ADMIN_REQUEST_SUCCESS, | ||
data: { requestsReponse } | ||
}; | ||
store.dispatch(createAdminRequest({ type: 'admin_request' })) | ||
.then(() => { | ||
const actions = store.getActions(); | ||
expect(actions[0]).toEqual(expectedAction); | ||
}, 3000); | ||
}); | ||
|
||
it('should dispatch CREATE_ADMIN_REQUEST_ERROR if admin request is not successfull', () => { | ||
const store = mockStore({}); | ||
moxios.stubRequest('/requests', { | ||
status: 200, | ||
response: { errors: ['The type field is required.'] } | ||
}); | ||
const expectedAction = { | ||
type: CREATE_ADMIN_REQUEST_ERROR, | ||
data: { errors: ['The type field is required.'] } | ||
}; | ||
store.dispatch(createAdminRequest()) | ||
.then(() => { | ||
const actions = store.getActions(); | ||
expect(actions[0]).toEqual(expectedAction); | ||
}, 3000); | ||
}); | ||
}); |
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,38 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import requestsReducer from '../../../redux/reducers/requestsReducer'; | ||
import { CREATE_ADMIN_REQUEST_SUCCESS, CREATE_ADMIN_REQUEST_ERROR } from '../../../../src/redux/actions/types'; | ||
import { success, isErrored } from '../../../redux/actions'; | ||
import requestsReponse from '../../__mockData__/requestsMock'; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
const store = mockStore({}); | ||
|
||
const initialState = { | ||
request: null, | ||
success: false, | ||
error: '' | ||
}; | ||
|
||
const errorResponse = { | ||
data: { | ||
errors: ['The type field is required.'] | ||
} | ||
}; | ||
|
||
describe('Test requestsReducer', () => { | ||
it(`should update state with request data when admin request is successful`, () => { | ||
const action = success(CREATE_ADMIN_REQUEST_SUCCESS, requestsReponse); | ||
const newState = requestsReducer(initialState, action); | ||
expect(newState.success).toEqual(true); | ||
expect(newState.request.data.request).toEqual(requestsReponse.data.request); | ||
}); | ||
|
||
it(`should update state with error when admin request is not successful`, () => { | ||
const action = isErrored(CREATE_ADMIN_REQUEST_ERROR, errorResponse); | ||
const newState = requestsReducer(initialState, action); | ||
expect(newState.success).toEqual(false); | ||
expect(newState.error).toEqual(errorResponse); | ||
}); | ||
}); |
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,3 @@ | ||
{ | ||
"You have made a request with the same type.": "This request has been made by you" | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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