-
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.
- Loading branch information
Showing
7 changed files
with
3,696 additions
and
3,455 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 |
---|---|---|
@@ -1,15 +1,43 @@ | ||
import instance from '../../../config/axios'; | ||
import { CREATE_ADMIN_REQUEST_SUCCESS, CREATE_ADMIN_REQUEST_ERROR } from '../types'; | ||
import { success, isErrored } from '../index'; | ||
import { CREATE_ADMIN_REQUEST_SUCCESS, CREATE_ADMIN_REQUEST_ERROR, FETCH_REQUESTS } from '../types'; | ||
import { success, isErrored, isLoading } 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(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 const loadRequests = (requestType = '', limit = '', offset = 0) => dispatch => { | ||
dispatch(isLoading(true)); | ||
return instance.get(`/requests?type=${requestType}&@limit=${limit}&@offset=${offset}`) | ||
.then(response => response.data) | ||
.then(data => { | ||
if (data.errorrs) { | ||
return dispatch(isErrored( | ||
`${FETCH_REQUESTS}_ERROR`, | ||
data.errors[0] | ||
)); | ||
} | ||
console.log(data, 'the data response'); | ||
const payload = { | ||
requests: data.data.requests, | ||
meta: data.meta | ||
}; | ||
|
||
return dispatch(success(`${FETCH_REQUESTS}_SUCCESS`, payload)); | ||
}).catch(errors => { | ||
dispatch(isErrored(`${FETCH_REQUESTS}_ERROR`, errors.response)); | ||
dispatch(isLoading(false)); | ||
}); | ||
}; | ||
|
||
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 |
---|---|---|
@@ -1,48 +1,159 @@ | ||
/* eslint max-nested-callbacks : off */ | ||
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 { | ||
CREATE_ADMIN_REQUEST_SUCCESS, IS_LOADING, IS_LOGGED_IN, | ||
CREATE_ADMIN_REQUEST_ERROR, FETCH_REQUESTS | ||
} from '../../../../../src/redux/actions/types'; | ||
import createAdminRequest, { loadRequests } from '../../../../redux/actions/requests'; | ||
import requestsReponse from '../../../__mocks__/__mockData__/requestsMock'; | ||
import mockLocalStorage from "../../../__mocks__/mockLocalStorage"; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
global.localStorage = mockLocalStorage; | ||
describe('Request Actions', () => { | ||
beforeEach(() => moxios.install(instance)); | ||
beforeEach(() => { | ||
// localStorage.setItem('aTeamsToken', ''); | ||
// localStorage.setItem('userId', ''); | ||
// localStorage.setItem('role', ''); | ||
}); | ||
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 | ||
|
||
|
||
describe('createAdminRequest', () => { | ||
it('should dispatch CREATE_ADMIN_REQUEST_SUCCESS after successfull admin request sent', (done) => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 200, | ||
response: requestsReponse | ||
}); | ||
}); | ||
return store.dispatch(createAdminRequest({ type: 'admin_request' })) | ||
.then(() => { | ||
const actions = store.getActions(); | ||
expect(actions[0].type).toEqual(CREATE_ADMIN_REQUEST_SUCCESS); | ||
expect(actions[0].payload).toEqual(requestsReponse); | ||
done(); | ||
}, 3000); | ||
}); | ||
|
||
it('should dispatch CREATE_ADMIN_REQUEST_ERROR if admin request is not successfull', (done) => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 400, | ||
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(); | ||
const actionData = actions[0].payload.response.data; | ||
expect(actions[0].type) | ||
.toEqual(CREATE_ADMIN_REQUEST_ERROR); | ||
expect(actionData) | ||
.toEqual(expectedAction.data); | ||
done(); | ||
}, 3000); | ||
}); | ||
|
||
|
||
it('should dispatch CREATE_ADMIN_REQUEST_ERROR if admin request is not successfull', (done) => { | ||
const store = mockStore({}); | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 400, | ||
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(); | ||
const actionData = actions[0].payload.response.data; | ||
expect(actions[0].type) | ||
.toEqual(CREATE_ADMIN_REQUEST_ERROR); | ||
expect(actionData) | ||
.toEqual(expectedAction.data); | ||
done(); | ||
}, 3000); | ||
}); | ||
}); | ||
describe('loadRequests', () => { | ||
it('should dispatch the correct actions once called when FETCH_REQUEST_SUCCESS', (done) => { | ||
const store = mockStore({}); | ||
mockLocalStorage.setItem('aTeamsToken', 'userToken'); | ||
mockLocalStorage.setItem('userId', 'id'); | ||
mockLocalStorage.setItem('role', 'admin'); | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 200, | ||
response: { | ||
data: [ | ||
{ id: 'request-id', type: 'admin' } | ||
] | ||
} | ||
}); | ||
}); | ||
return store.dispatch(loadRequests('admin_request', 10, 10)) | ||
.then(() => { | ||
const actions = store.getActions(); | ||
console.log(actions); | ||
|
||
expect(actions[0].type).toEqual(IS_LOGGED_IN); | ||
expect(actions[0].payload).toEqual(false); | ||
|
||
expect(actions[1].type).toEqual(IS_LOADING); | ||
expect(actions[1].payload).toEqual(true); | ||
|
||
expect(actions[2].type).toEqual(`${FETCH_REQUESTS}_SUCCESS`); | ||
done(); | ||
}, 3000); | ||
}); | ||
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', () => { | ||
it('should dispatch the correct actions once called FETCH_REQUEST_ERR0R', (done) => { | ||
const store = mockStore({}); | ||
moxios.stubRequest('/requests', { | ||
status: 200, | ||
response: { errors: ['The type field is required.'] } | ||
mockLocalStorage.setItem('aTeamsToken', 'userToken'); | ||
mockLocalStorage.setItem('userId', 'id'); | ||
mockLocalStorage.setItem('role', 'admin'); | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 400, | ||
response: { | ||
errors: {} | ||
} | ||
}); | ||
}); | ||
const expectedAction = { | ||
type: CREATE_ADMIN_REQUEST_ERROR, | ||
data: { errors: ['The type field is required.'] } | ||
}; | ||
store.dispatch(createAdminRequest()) | ||
return store.dispatch(loadRequests('admin_request', 10, 10)) | ||
.then(() => { | ||
const actions = store.getActions(); | ||
expect(actions[0]).toEqual(expectedAction); | ||
|
||
expect(actions[0].type).toEqual(IS_LOGGED_IN); | ||
expect(actions[0].payload).toEqual(false); | ||
|
||
expect(actions[1].type).toEqual(IS_LOADING); | ||
expect(actions[1].payload).toEqual(true); | ||
|
||
expect(actions[2].type).toEqual(`${FETCH_REQUESTS}_ERROR`); | ||
done(); | ||
}, 3000); | ||
}); | ||
}); |
Oops, something went wrong.