Last active
March 22, 2018 23:27
-
-
Save Nfinley/1add244630a0e3584d0573343e08a649 to your computer and use it in GitHub Desktop.
A jest snippet for testing an action file
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
import configureMockStore from 'redux-mock-store'; | |
import thunk from 'redux-thunk'; | |
import * as $actionFileName$ from './$actionFileName$'; | |
import * as Constants from '../constants/Constants'; | |
const middlewares = [thunk]; | |
const mockStore = configureMockStore(middlewares); | |
describe('$actionFileName$', () => { | |
//Test all actions | |
describe('$actionName1$ Action', () => { | |
it('Should dispatch $actionName1$ action', () => { | |
const store = mockStore({}); | |
const actions = store.getActions(); | |
store.dispatch($actionFileName$.actionCall('test')); | |
const expectedPayload = {logout: 'test', type: Constants.USER_LOGOUT}; | |
expect(actions).toEqual([expectedPayload]); | |
}); | |
}); | |
describe('$actionName2$ Action', () => { | |
it('Should dispatch $actionName2$ action', () => { | |
const store = mockStore({}); | |
const actions = store.getActions(); | |
store.dispatch($actionFileName$.actionCall('test')); | |
const expectedPayload = {logout: 'test', type: Constants.USER_LOGOUT}; | |
expect(actions).toEqual([expectedPayload]); | |
}); | |
}); | |
//Describe all of your thunks | |
describe('$thunkNAame$ Thunk', () => { | |
fetch.mockResponse(JSON.stringify({name: "test"}), {status: 200}); | |
const payload = { | |
"id": "1", | |
"name": "test" | |
}; | |
it('Should call $thunkNAame$ thunk', () => { | |
const store = mockStore({}); | |
const mockUpdateApplicationAction = { | |
"type": "$actionTYPE$", | |
"action": "" | |
}; | |
return store.dispatch($actionFileName$.actionCall(payload)).then(() => { | |
expect(store.getActions()).toEqual(mockUpdateApplicationAction); | |
}); | |
}); | |
}); | |
//Handle any errors from the api response | |
describe('When response Callback throws an error', () => { | |
const payload = { | |
"name": "test" | |
} | |
it('Should throw an error on a > 300 response', () => { | |
// fetch.mockReject(new Error('fake error message')); | |
fetch.mockResponse(JSON.stringify({name: "test"}), {status: 301}); | |
const store = mockStore({}); | |
return store.dispatch($actionFileName$.actionCall(payload)) | |
.catch(err => { | |
expect(err).toEqual(payload); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment