Skip to content

Instantly share code, notes, and snippets.

@edmulraney
Last active October 2, 2017 11:56
Show Gist options
  • Select an option

  • Save edmulraney/a0e439a291261076a4967d24dffa8e8a to your computer and use it in GitHub Desktop.

Select an option

Save edmulraney/a0e439a291261076a4967d24dffa8e8a to your computer and use it in GitHub Desktop.
import createThunkStore from './create-thunk-store'
import {
POST_USER_REQUESTED,
POST_USER_SUCCEEDED,
POST_USER_FAILED,
} from './action-types'
const API_SERVICE_PATH = './api-service'
describe('postUser', () => {
beforeEach(() => {
jest.resetModules()
})
it('dispatches failure actions', () => {
const userPayload = { userId: 'MOCK_ID', name: 'John' }
const response = 'MOCK_ERROR_RESPONSE'
const store = createThunkStore()
jest.doMock(API_SERVICE_PATH, () => ({
post: () => Promise.reject(response),
}))
const { postUser } = require('./actions') // eslint-disable-line
return store.dispatch(postUser(userPayload)).then(() => {
const actions = store.getActions()
const expectedFailResponse = { userId: userPayload.userId, errorResponse: response }
const expectedActions = [
{ type: POST_USER_REQUESTED, payload: userPayload },
{ type: POST_USER_FAILED, payload: expectedFailResponse },
]
expect(actions).toEqual(expectedActions)
})
})
it('dispatches success actions', () => {
const userPayload = { userId: 'MOCK_ID', name: 'John' }
const response = 'MOCK_SUCCESS_RESPONSE'
const store = createThunkStore()
jest.doMock(API_SERVICE_PATH, () => ({
post: () => Promise.resolve(response),
}))
const { postUser } = require('./actions') // eslint-disable-line
return store.dispatch(postUser(userPayload)).then(() => {
const actions = store.getActions()
const expectedActions = [
{ type: POST_USER_REQUESTED, payload: userPayload },
{ type: POST_USER_SUCCEEDED, payload: response },
]
expect(actions).toEqual(expectedActions)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment