Skip to content

Commit

Permalink
Merge pull request #116 from mayank23/master
Browse files Browse the repository at this point in the history
conform more to basic redux behavior for dispatch.
  • Loading branch information
dmitry-zaets authored Nov 5, 2017
2 parents c61d95f + 9945cb2 commit 63ae1c5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 51 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-mock-store",
"version": "1.3.0",
"version": "1.4.0",
"description": "A mock store for testing your redux async action creators and middleware",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -30,5 +30,8 @@
"rimraf": "^2.4.3",
"sinon": "^1.17.2",
"standard": "^7.1.2"
},
"dependencies": {
"lodash.isplainobject": "^4.0.6"
}
}
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { applyMiddleware } from 'redux'
import isPlainObject from 'lodash.isplainobject'

const isFunction = arg => typeof arg === 'function'

Expand All @@ -18,9 +19,10 @@ export default function configureStore (middlewares = []) {
},

dispatch (action) {
if (typeof action === 'undefined') {
if (!isPlainObject(action)) {
throw new Error(
'Actions may not be an undefined.'
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}

Expand Down
109 changes: 61 additions & 48 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import thunk from 'redux-thunk'
import mockMiddleware from './mock/middleware'
import configureStore from '../src'

const mockStore = configureStore([thunk])
const mockStore = configureStore()

describe('redux-mock-store', () => {
describe('getState', () => {
Expand Down Expand Up @@ -40,11 +40,21 @@ describe('redux-mock-store', () => {
})
})

it('should throw an error when action is undefined', () => {
it('should throw an error when the action is undefined', () => {
const store = mockStore({})

expect(() => { store.dispatch(undefined) }).toThrow(
'Actions may not be an undefined.'
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
})

it('should throw an error when the action is not a plain object', () => {
const store = mockStore({})

expect(() => { store.dispatch(() => {}) }).toThrow(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
})

Expand All @@ -70,51 +80,6 @@ describe('redux-mock-store', () => {
expect(first).toBe(action)
})

it('handles async actions', (done) => {
function increment () {
return {
type: 'INCREMENT_COUNTER'
}
}

function incrementAsync () {
return dispatch => {
return Promise.resolve()
.then(() => dispatch(increment()))
}
}

const store = mockStore({})

store.dispatch(incrementAsync())
.then(() => {
expect(store.getActions()[0]).toEqual(increment())
done()
})
})

it('should call the middleware', () => {
const spy = sinon.spy()
const middlewares = [mockMiddleware(spy)]
const mockStoreWithMiddleware = configureStore(middlewares)
const action = { type: 'ADD_ITEM' }

const store = mockStoreWithMiddleware()
store.dispatch(action)
expect(spy.called).toBe(true)
})

it('should handle when test function throws an error', (done) => {
const store = mockStore({})
const error = { error: 'Something went wrong' }

store.dispatch(() => Promise.reject(error))
.catch(err => {
expect(err).toEqual(error)
done()
})
})

it('clears the actions', () => {
const action = { type: 'ADD_ITEM' }
const store = mockStore({})
Expand Down Expand Up @@ -177,4 +142,52 @@ describe('redux-mock-store', () => {
expect(() => store.replaceReducer(123))
.toThrow('Expected the nextReducer to be a function.')
})

describe('store with middleware', () => {
const mockStoreWithMiddleware = configureStore([thunk])
it('handles async actions', (done) => {
function increment () {
return {
type: 'INCREMENT_COUNTER'
}
}

function incrementAsync () {
return dispatch => {
return Promise.resolve()
.then(() => dispatch(increment()))
}
}

const store = mockStoreWithMiddleware({})

store.dispatch(incrementAsync())
.then(() => {
expect(store.getActions()[0]).toEqual(increment())
done()
})
})

it('should handle when test function throws an error', (done) => {
const store = mockStoreWithMiddleware({})
const error = { error: 'Something went wrong' }

store.dispatch(() => Promise.reject(error))
.catch(err => {
expect(err).toEqual(error)
done()
})
})

it('should call the middleware', () => {
const spy = sinon.spy()
const middlewares = [mockMiddleware(spy)]
const mockStoreWithCustomMiddleware = configureStore(middlewares)
const action = { type: 'ADD_ITEM' }

const store = mockStoreWithCustomMiddleware()
store.dispatch(action)
expect(spy.called).toBe(true)
})
})
})

0 comments on commit 63ae1c5

Please sign in to comment.