redux-mock-store
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -66,2 +66,7 @@ 'use strict'; | ||
}; | ||
}, | ||
replaceReducer: function replaceReducer(nextReducer) { | ||
if (!isFunction(nextReducer)) { | ||
throw new Error('Expected the nextReducer to be a function.'); | ||
} | ||
} | ||
@@ -68,0 +73,0 @@ }; |
{ | ||
"name": "redux-mock-store", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"prepublish": "rimraf lib && babel src --out-dir lib", | ||
"lint": "standard src/*.js", | ||
"lint": "standard src/*.js test/*.js", | ||
"pretest": "npm run lint", | ||
@@ -11,0 +11,0 @@ "test": "mocha --compilers js:babel-core/register --reporter spec test/*.js" |
@@ -74,3 +74,3 @@ [![Circle CI](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master.svg?style=svg)](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master) | ||
``` | ||
- configureStore(middlewares?: Array) => mockStore: function | ||
- configureStore(middlewares?: Array) => mockStore: Function | ||
- mockStore(getState?: Object,Function) => store: Function | ||
@@ -81,3 +81,4 @@ - store.dispatch(action) => action | ||
- store.clearActions() | ||
- store.subscribe() | ||
- store.subscribe(callback: Function) => unsubscribe: Function | ||
- store.replaceReducer(nextReducer: Function) | ||
``` | ||
@@ -84,0 +85,0 @@ |
@@ -1,73 +0,72 @@ | ||
import expect from 'expect'; | ||
import sinon from 'sinon'; | ||
import thunk from 'redux-thunk'; | ||
/* eslint-env mocha */ | ||
import expect from 'expect' | ||
import sinon from 'sinon' | ||
import thunk from 'redux-thunk' | ||
import mockMiddleware from './mock/middleware'; | ||
import configureStore from '../src'; | ||
import mockMiddleware from './mock/middleware' | ||
import configureStore from '../src' | ||
const mockStore = configureStore([thunk]); | ||
const mockStore = configureStore([thunk]) | ||
describe('redux-mock-store', () => { | ||
it('calls getState if it is a function', () => { | ||
const getState = sinon.spy(); | ||
const store = mockStore(getState, []); | ||
const getState = sinon.spy() | ||
const store = mockStore(getState, []) | ||
store.getState(); | ||
expect(getState.called).toBe(true); | ||
}); | ||
store.getState() | ||
expect(getState.called).toBe(true) | ||
}) | ||
it('returns the initial state', () => { | ||
const initialState = { items: [], count: 0 }; | ||
const store = mockStore(initialState); | ||
const initialState = { items: [], count: 0 } | ||
const store = mockStore(initialState) | ||
expect(store.getState()).toEqual(initialState); | ||
}); | ||
expect(store.getState()).toEqual(initialState) | ||
}) | ||
it('should throw an error when action is undefined', () => { | ||
const store = mockStore({}); | ||
it('should throw an error when action is undefined', () => { | ||
const store = mockStore({}) | ||
expect(() => { store.dispatch(undefined); }).toThrow( | ||
expect(() => { store.dispatch(undefined) }).toThrow( | ||
'Actions may not be an undefined.' | ||
); | ||
}); | ||
) | ||
}) | ||
it('should throw an error when action type is undefined', () => { | ||
const action = { types: 'ADD_ITEM' }; | ||
const store = mockStore({}); | ||
const action = { types: 'ADD_ITEM' } | ||
const store = mockStore({}) | ||
expect(() => { store.dispatch(action); }).toThrow( | ||
expect(() => { store.dispatch(action) }).toThrow( | ||
'Actions may not have an undefined "type" property. ' + | ||
'Have you misspelled a constant? '+ | ||
'Have you misspelled a constant? ' + | ||
'Action: ' + | ||
'{"types":"ADD_ITEM"}' | ||
); | ||
}); | ||
) | ||
}) | ||
it('should return if the tests is successful', () => { | ||
const action = { type: 'ADD_ITEM' }; | ||
const store = mockStore({}); | ||
const action = { type: 'ADD_ITEM' } | ||
const store = mockStore({}) | ||
store.dispatch(action); | ||
store.dispatch(action) | ||
const [first] = store.getActions(); | ||
expect(first).toBe(action); | ||
}); | ||
const [first] = store.getActions() | ||
expect(first).toBe(action) | ||
}) | ||
it('handles async actions', (done) => { | ||
function increment() { | ||
function increment () { | ||
return { | ||
type: 'INCREMENT_COUNTER' | ||
}; | ||
} | ||
} | ||
function incrementAsync() { | ||
function incrementAsync () { | ||
return dispatch => { | ||
return Promise.resolve() | ||
.then(() => dispatch(increment())); | ||
}; | ||
.then(() => dispatch(increment())) | ||
} | ||
} | ||
const store = mockStore({}); | ||
const store = mockStore({}) | ||
@@ -77,80 +76,90 @@ store.dispatch(incrementAsync()) | ||
expect(store.getActions()[0]).toEqual(increment()) | ||
done(); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
it('should call the middleware', () => { | ||
const spy = sinon.spy(); | ||
const middlewares = [mockMiddleware(spy)]; | ||
const mockStoreWithMiddleware = configureStore(middlewares); | ||
const action = { type: 'ADD_ITEM' }; | ||
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); | ||
}); | ||
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' }; | ||
const store = mockStore({}) | ||
const error = { error: 'Something went wrong' } | ||
store.dispatch(() => Promise.reject(error)) | ||
.catch(err => { | ||
expect(err).toEqual(error); | ||
done(); | ||
expect(err).toEqual(error) | ||
done() | ||
}) | ||
}); | ||
}) | ||
it('clears the actions', () => { | ||
const action = { type: 'ADD_ITEM' }; | ||
const store = mockStore({}); | ||
const action = { type: 'ADD_ITEM' } | ||
const store = mockStore({}) | ||
store.dispatch(action); | ||
expect(store.getActions()).toEqual([action]); | ||
store.dispatch(action) | ||
expect(store.getActions()).toEqual([action]) | ||
store.clearActions(); | ||
expect(store.getActions()).toEqual([]); | ||
store.clearActions() | ||
expect(store.getActions()).toEqual([]) | ||
}) | ||
it('handles multiple actions', () => { | ||
const store = mockStore(); | ||
const store = mockStore() | ||
const actions = [ | ||
{ type: 'ADD_ITEM' }, | ||
{ type: 'REMOVE_ITEM' } | ||
]; | ||
] | ||
store.dispatch(actions[0]); | ||
store.dispatch(actions[1]); | ||
store.dispatch(actions[0]) | ||
store.dispatch(actions[1]) | ||
expect(store.getActions()).toEqual(actions); | ||
}); | ||
expect(store.getActions()).toEqual(actions) | ||
}) | ||
it('subscribes to dispatched actions', (done) => { | ||
const store = mockStore(); | ||
const action = { type: 'ADD_ITEM' }; | ||
const store = mockStore() | ||
const action = { type: 'ADD_ITEM' } | ||
store.subscribe(() => { | ||
expect(store.getActions()[0]).toEqual(action); | ||
done(); | ||
}); | ||
store.dispatch(action); | ||
}); | ||
expect(store.getActions()[0]).toEqual(action) | ||
done() | ||
}) | ||
store.dispatch(action) | ||
}) | ||
it('can unsubscribe subscribers', function (done) { | ||
const store = mockStore(); | ||
const action = { type: 'ADD_ITEM' }; | ||
const waitForMS = 25; | ||
const testWaitsAnotherMS = 25; | ||
const store = mockStore() | ||
const action = { type: 'ADD_ITEM' } | ||
const waitForMS = 25 | ||
const testWaitsAnotherMS = 25 | ||
this.timeout(waitForMS + testWaitsAnotherMS); | ||
const timeoutId = setTimeout(done, waitForMS); | ||
this.timeout(waitForMS + testWaitsAnotherMS) | ||
const timeoutId = setTimeout(done, waitForMS) | ||
const unsubscribe = store.subscribe(() => { | ||
clearTimeout(timeoutId); | ||
done(new Error('should never be called')); | ||
}); | ||
clearTimeout(timeoutId) | ||
done(new Error('should never be called')) | ||
}) | ||
unsubscribe(); | ||
store.dispatch(action); | ||
}); | ||
}); | ||
unsubscribe() | ||
store.dispatch(action) | ||
}) | ||
it('has replace reducer function', () => { | ||
const store = mockStore({}) | ||
expect(() => store.replaceReducer(() => {})) | ||
.toNotThrow('Expected the nextReducer to be a function.') | ||
expect(() => store.replaceReducer(123)) | ||
.toThrow('Expected the nextReducer to be a function.') | ||
}) | ||
}) |
export default spy => store => next => action => { | ||
spy(); | ||
return next(action); | ||
}; | ||
spy() | ||
return next(action) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10909
195
87
1