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.

Revisions

  1. edmulraney renamed this gist Oct 2, 2017. 1 changed file with 0 additions and 0 deletions.
  2. edmulraney created this gist Oct 2, 2017.
    59 changes: 59 additions & 0 deletions Redux thunk test using Jest
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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)
    })
    })
    })