diff --git a/.eslintrc.js b/.eslintrc.js index c52febb261..c8eb29c3a5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,7 +5,7 @@ module.exports = { { files: 'test/**/*.js', env: { - jest: true, + jest: true, }, }, ], diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000000..b2095be81e --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,4 @@ +{ + "semi": false, + "singleQuote": true +} diff --git a/examples/todos-flow/package-lock.json b/examples/todos-flow/package-lock.json index 4cc8b75603..74fbebfbcd 100644 --- a/examples/todos-flow/package-lock.json +++ b/examples/todos-flow/package-lock.json @@ -4421,22 +4421,22 @@ } } }, - "string-width": { - "version": "1.0.2", + "string_decoder": { + "version": "1.0.1", "bundled": true, "dev": true, "requires": { - "code-point-at": "1.1.0", - "is-fullwidth-code-point": "1.0.0", - "strip-ansi": "3.0.1" + "safe-buffer": "5.0.1" } }, - "string_decoder": { - "version": "1.0.1", + "string-width": { + "version": "1.0.2", "bundled": true, "dev": true, "requires": { - "safe-buffer": "5.0.1" + "code-point-at": "1.1.0", + "is-fullwidth-code-point": "1.0.0", + "strip-ansi": "3.0.1" } }, "stringstream": { @@ -9740,6 +9740,15 @@ "integrity": "sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=", "dev": true }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "dev": true, + "requires": { + "safe-buffer": "5.1.1" + } + }, "string-length": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/string-length/-/string-length-1.0.1.tgz", @@ -9758,15 +9767,6 @@ "strip-ansi": "3.0.1" } }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "dev": true, - "requires": { - "safe-buffer": "5.1.1" - } - }, "stringstream": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz", diff --git a/package-lock.json b/package-lock.json index 99e5b0cac8..847694596d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10599,6 +10599,12 @@ "integrity": "sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=", "dev": true }, + "prettier": { + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.8.2.tgz", + "integrity": "sha512-fHWjCwoRZgjP1rvLP7OGqOznq7xH1sHMQUFLX8qLRO79hI57+6xbc5vB904LxEkCfgFgyr3vv06JkafgCSzoZg==", + "dev": true + }, "pretty-format": { "version": "21.2.1", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-21.2.1.tgz", diff --git a/package.json b/package.json index 20ba986c1f..a96167ac3a 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ ], "scripts": { "clean": "rimraf lib dist es coverage", + "format": "prettier --write '{src,test}/**/*.js'", + "format:check": "prettier --list-different '{src,test}/**/*.js'", "lint": "eslint src test build", "pretest": "npm run build:commonjs", "test": "cross-env BABEL_ENV=commonjs jest", @@ -24,7 +26,7 @@ "build:umd": "cross-env BABEL_ENV=es NODE_ENV=development rollup -c -o dist/redux.js", "build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup -c -o dist/redux.min.js", "build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min", - "prepare": "npm run clean && npm run lint && npm test && npm run build", + "prepare": "npm run clean && npm run format:check && npm run lint && npm test && npm run build", "examples:lint": "eslint examples", "examples:test": "cross-env CI=true babel-node examples/testAll.js", "docs:clean": "rimraf _book", @@ -82,6 +84,7 @@ "gitbook-cli": "^2.3.2", "glob": "^7.1.1", "jest": "^21.2.1", + "prettier": "1.8.2", "rimraf": "^2.6.2", "rollup": "^0.51.8", "rollup-plugin-babel": "^3.0.2", diff --git a/src/applyMiddleware.js b/src/applyMiddleware.js index 73c8266d71..fd32a79c62 100644 --- a/src/applyMiddleware.js +++ b/src/applyMiddleware.js @@ -17,12 +17,12 @@ import compose from './compose' * @returns {Function} A store enhancer applying the middleware. */ export default function applyMiddleware(...middlewares) { - return (createStore) => (...args) => { + return createStore => (...args) => { const store = createStore(...args) let dispatch = () => { throw new Error( `Dispatching while constructing your middleware is not allowed. ` + - `Other middleware would not be applied to this dispatch.` + `Other middleware would not be applied to this dispatch.` ) } let chain = [] diff --git a/src/bindActionCreators.js b/src/bindActionCreators.js index da650d3b57..7686bfff72 100644 --- a/src/bindActionCreators.js +++ b/src/bindActionCreators.js @@ -1,5 +1,7 @@ function bindActionCreator(actionCreator, dispatch) { - return function() { return dispatch(actionCreator.apply(this, arguments)) } + return function() { + return dispatch(actionCreator.apply(this, arguments)) + } } /** @@ -30,8 +32,10 @@ export default function bindActionCreators(actionCreators, dispatch) { if (typeof actionCreators !== 'object' || actionCreators === null) { throw new Error( - `bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` + - `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?` + `bindActionCreators expected an object or a function, instead received ${ + actionCreators === null ? 'null' : typeof actionCreators + }. ` + + `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?` ) } diff --git a/src/combineReducers.js b/src/combineReducers.js index 872a92109a..b5de20aecf 100644 --- a/src/combineReducers.js +++ b/src/combineReducers.js @@ -4,7 +4,8 @@ import isPlainObject from './utils/isPlainObject' function getUndefinedStateErrorMessage(key, action) { const actionType = action && action.type - const actionDescription = (actionType && `action "${String(actionType)}"`) || 'an action' + const actionDescription = + (actionType && `action "${String(actionType)}"`) || 'an action' return ( `Given ${actionDescription}, reducer "${key}" returned undefined. ` + @@ -13,11 +14,17 @@ function getUndefinedStateErrorMessage(key, action) { ) } -function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) { +function getUnexpectedStateShapeWarningMessage( + inputState, + reducers, + action, + unexpectedKeyCache +) { const reducerKeys = Object.keys(reducers) - const argumentName = action && action.type === ActionTypes.INIT ? - 'preloadedState argument passed to createStore' : - 'previous state received by the reducer' + const argumentName = + action && action.type === ActionTypes.INIT + ? 'preloadedState argument passed to createStore' + : 'previous state received by the reducer' if (reducerKeys.length === 0) { return ( @@ -29,15 +36,14 @@ function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, une if (!isPlainObject(inputState)) { return ( `The ${argumentName} has unexpected type of "` + - ({}).toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] + + {}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] + `". Expected argument to be an object with the following ` + `keys: "${reducerKeys.join('", "')}"` ) } - const unexpectedKeys = Object.keys(inputState).filter(key => - !reducers.hasOwnProperty(key) && - !unexpectedKeyCache[key] + const unexpectedKeys = Object.keys(inputState).filter( + key => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key] ) unexpectedKeys.forEach(key => { @@ -64,22 +70,30 @@ function assertReducerShape(reducers) { if (typeof initialState === 'undefined') { throw new Error( `Reducer "${key}" returned undefined during initialization. ` + - `If the state passed to the reducer is undefined, you must ` + - `explicitly return the initial state. The initial state may ` + - `not be undefined. If you don't want to set a value for this reducer, ` + - `you can use null instead of undefined.` + `If the state passed to the reducer is undefined, you must ` + + `explicitly return the initial state. The initial state may ` + + `not be undefined. If you don't want to set a value for this reducer, ` + + `you can use null instead of undefined.` ) } - const type = '@@redux/PROBE_UNKNOWN_ACTION_' + Math.random().toString(36).substring(7).split('').join('.') + const type = + '@@redux/PROBE_UNKNOWN_ACTION_' + + Math.random() + .toString(36) + .substring(7) + .split('') + .join('.') if (typeof reducer(undefined, { type }) === 'undefined') { throw new Error( `Reducer "${key}" returned undefined when probed with a random type. ` + - `Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` + - `namespace. They are considered private. Instead, you must return the ` + - `current state for any unknown actions, unless it is undefined, ` + - `in which case you must return the initial state, regardless of the ` + - `action type. The initial state may not be undefined, but can be null.` + `Don't try to handle ${ + ActionTypes.INIT + } or other actions in "redux/*" ` + + `namespace. They are considered private. Instead, you must return the ` + + `current state for any unknown actions, unless it is undefined, ` + + `in which case you must return the initial state, regardless of the ` + + `action type. The initial state may not be undefined, but can be null.` ) } }) @@ -137,7 +151,12 @@ export default function combineReducers(reducers) { } if (process.env.NODE_ENV !== 'production') { - const warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache) + const warningMessage = getUnexpectedStateShapeWarningMessage( + state, + finalReducers, + action, + unexpectedKeyCache + ) if (warningMessage) { warning(warningMessage) } diff --git a/src/createStore.js b/src/createStore.js index 9b56cb3072..f3f30a1930 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -67,8 +67,8 @@ export default function createStore(reducer, preloadedState, enhancer) { if (isDispatching) { throw new Error( 'You may not call store.getState() while the reducer is executing. ' + - 'The reducer has already received the state as an argument. ' + - 'Pass it down from the top reducer instead of reading it from the store.' + 'The reducer has already received the state as an argument. ' + + 'Pass it down from the top reducer instead of reading it from the store.' ) } @@ -106,9 +106,9 @@ export default function createStore(reducer, preloadedState, enhancer) { if (isDispatching) { throw new Error( 'You may not call store.subscribe() while the reducer is executing. ' + - 'If you would like to be notified after the store has been updated, subscribe from a ' + - 'component and invoke store.getState() in the callback to access the latest state. ' + - 'See http://redux.js.org/docs/api/Store.html#subscribe for more details.' + 'If you would like to be notified after the store has been updated, subscribe from a ' + + 'component and invoke store.getState() in the callback to access the latest state. ' + + 'See http://redux.js.org/docs/api/Store.html#subscribe for more details.' ) } @@ -125,7 +125,7 @@ export default function createStore(reducer, preloadedState, enhancer) { if (isDispatching) { throw new Error( 'You may not unsubscribe from a store listener while the reducer is executing. ' + - 'See http://redux.js.org/docs/api/Store.html#subscribe for more details.' + 'See http://redux.js.org/docs/api/Store.html#subscribe for more details.' ) } @@ -166,14 +166,14 @@ export default function createStore(reducer, preloadedState, enhancer) { if (!isPlainObject(action)) { throw new Error( 'Actions must be plain objects. ' + - 'Use custom middleware for async actions.' + 'Use custom middleware for async actions.' ) } if (typeof action.type === 'undefined') { throw new Error( 'Actions may not have an undefined "type" property. ' + - 'Have you misspelled a constant?' + 'Have you misspelled a constant?' ) } @@ -188,7 +188,7 @@ export default function createStore(reducer, preloadedState, enhancer) { isDispatching = false } - const listeners = currentListeners = nextListeners + const listeners = (currentListeners = nextListeners) for (let i = 0; i < listeners.length; i++) { const listener = listeners[i] listener() diff --git a/src/index.js b/src/index.js index 929d9573bf..bac914de17 100644 --- a/src/index.js +++ b/src/index.js @@ -18,11 +18,11 @@ if ( isCrushed.name !== 'isCrushed' ) { warning( - 'You are currently using minified code outside of NODE_ENV === \'production\'. ' + - 'This means that you are running a slower development build of Redux. ' + - 'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' + - 'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' + - 'to ensure you have the correct code for your production build.' + "You are currently using minified code outside of NODE_ENV === 'production'. " + + 'This means that you are running a slower development build of Redux. ' + + 'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' + + 'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' + + 'to ensure you have the correct code for your production build.' ) } diff --git a/src/utils/actionTypes.js b/src/utils/actionTypes.js index 3e087d106c..6e0ffb098a 100644 --- a/src/utils/actionTypes.js +++ b/src/utils/actionTypes.js @@ -5,8 +5,20 @@ * Do not reference these action types directly in your code. */ const ActionTypes = { - INIT: '@@redux/INIT' + Math.random().toString(36).substring(7).split('').join('.'), - REPLACE: '@@redux/REPLACE' + Math.random().toString(36).substring(7).split('').join('.') + INIT: + '@@redux/INIT' + + Math.random() + .toString(36) + .substring(7) + .split('') + .join('.'), + REPLACE: + '@@redux/REPLACE' + + Math.random() + .toString(36) + .substring(7) + .split('') + .join('.') } export default ActionTypes diff --git a/src/utils/warning.js b/src/utils/warning.js index d10bb11d7c..d35e99b726 100644 --- a/src/utils/warning.js +++ b/src/utils/warning.js @@ -15,7 +15,5 @@ export default function warning(message) { // "break on all exceptions" in your console, // it would pause the execution at this line. throw new Error(message) - /* eslint-disable no-empty */ - } catch (e) { } - /* eslint-enable no-empty */ + } catch (e) {} // eslint-disable-line no-empty } diff --git a/test/applyMiddleware.spec.js b/test/applyMiddleware.spec.js index fef3121c5d..2efdc9f83f 100644 --- a/test/applyMiddleware.spec.js +++ b/test/applyMiddleware.spec.js @@ -34,7 +34,10 @@ describe('applyMiddleware', () => { expect(spy.mock.calls[0][0]).toHaveProperty('getState') expect(spy.mock.calls[0][0]).toHaveProperty('dispatch') - expect(store.getState()).toEqual([ { id: 1, text: 'Use Redux' }, { id: 2, text: 'Flux FTW!' } ]) + expect(store.getState()).toEqual([ + { id: 1, text: 'Use Redux' }, + { id: 2, text: 'Flux FTW!' } + ]) }) it('passes recursive dispatches through the middleware chain', () => { @@ -120,7 +123,10 @@ describe('applyMiddleware', () => { return next => action => dispatch(action, testCallArgs) } - const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware)) + const store = createStore( + reducers.todos, + applyMiddleware(multiArgMiddleware, dummyMiddleware) + ) store.dispatch(spy) expect(spy.mock.calls[0]).toEqual(testCallArgs) diff --git a/test/bindActionCreators.spec.js b/test/bindActionCreators.spec.js index e773776268..a9e2f2a6ea 100644 --- a/test/bindActionCreators.spec.js +++ b/test/bindActionCreators.spec.js @@ -17,20 +17,17 @@ describe('bindActionCreators', () => { }) it('wraps the action creators with the dispatch function', () => { - const boundActionCreators = bindActionCreators(actionCreators, store.dispatch) - expect( - Object.keys(boundActionCreators) - ).toEqual( + const boundActionCreators = bindActionCreators( + actionCreators, + store.dispatch + ) + expect(Object.keys(boundActionCreators)).toEqual( Object.keys(actionCreatorFunctions) ) const action = boundActionCreators.addTodo('Hello') - expect(action).toEqual( - actionCreators.addTodo('Hello') - ) - expect(store.getState()).toEqual([ - { id: 1, text: 'Hello' } - ]) + expect(action).toEqual(actionCreators.addTodo('Hello')) + expect(store.getState()).toEqual([{ id: 1, text: 'Hello' }]) }) it('wraps action creators transparently', () => { @@ -41,25 +38,26 @@ describe('bindActionCreators', () => { } const boundActionCreator = bindActionCreators(actionCreator, store.dispatch) - const boundAction = boundActionCreator.apply(uniqueThis,argArray) - const action = actionCreator.apply(uniqueThis,argArray) + const boundAction = boundActionCreator.apply(uniqueThis, argArray) + const action = actionCreator.apply(uniqueThis, argArray) expect(boundAction).toEqual(action) expect(boundAction.this).toBe(uniqueThis) expect(action.this).toBe(uniqueThis) }) it('skips non-function values in the passed object', () => { - const boundActionCreators = bindActionCreators({ - ...actionCreators, - foo: 42, - bar: 'baz', - wow: undefined, - much: {}, - test: null - }, store.dispatch) - expect( - Object.keys(boundActionCreators) - ).toEqual( + const boundActionCreators = bindActionCreators( + { + ...actionCreators, + foo: 42, + bar: 'baz', + wow: undefined, + much: {}, + test: null + }, + store.dispatch + ) + expect(Object.keys(boundActionCreators)).toEqual( Object.keys(actionCreatorFunctions) ) }) @@ -70,9 +68,7 @@ describe('bindActionCreators', () => { const action = boundActionCreator('Hello') expect(action).toEqual(actionCreator('Hello')) - expect(store.getState()).toEqual([ - { id: 1, text: 'Hello' } - ]) + expect(store.getState()).toEqual([{ id: 1, text: 'Hello' }]) }) it('throws for an undefined actionCreator', () => { @@ -80,7 +76,7 @@ describe('bindActionCreators', () => { bindActionCreators(undefined, store.dispatch) }).toThrow( 'bindActionCreators expected an object or a function, instead received undefined. ' + - 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' + 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' ) }) @@ -89,7 +85,7 @@ describe('bindActionCreators', () => { bindActionCreators(null, store.dispatch) }).toThrow( 'bindActionCreators expected an object or a function, instead received null. ' + - 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' + 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' ) }) @@ -98,7 +94,7 @@ describe('bindActionCreators', () => { bindActionCreators('string', store.dispatch) }).toThrow( 'bindActionCreators expected an object or a function, instead received string. ' + - 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' + 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?' ) }) }) diff --git a/test/combineReducers.spec.js b/test/combineReducers.spec.js index 9be774753b..321010a598 100644 --- a/test/combineReducers.spec.js +++ b/test/combineReducers.spec.js @@ -1,20 +1,24 @@ /* eslint-disable no-console */ -import { createStore, combineReducers, __DO_NOT_USE__ActionTypes as ActionTypes } from '../' +import { + createStore, + combineReducers, + __DO_NOT_USE__ActionTypes as ActionTypes +} from '../' describe('Utils', () => { describe('combineReducers', () => { it('returns a composite reducer that maps the state keys to given reducers', () => { const reducer = combineReducers({ counter: (state = 0, action) => - action.type === 'increment' ? state + 1 : state, + action.type === 'increment' ? state + 1 : state, stack: (state = [], action) => - action.type === 'push' ? [ ...state, action.value ] : state + action.type === 'push' ? [...state, action.value] : state }) const s1 = reducer({}, { type: 'increment' }) expect(s1).toEqual({ counter: 1, stack: [] }) const s2 = reducer(s1, { type: 'push', value: 'a' }) - expect(s2).toEqual({ counter: 1, stack: [ 'a' ] }) + expect(s2).toEqual({ counter: 1, stack: ['a'] }) }) it('ignores all props which are not a function', () => { @@ -25,9 +29,7 @@ describe('Utils', () => { stack: (state = []) => state }) - expect( - Object.keys(reducer({ }, { type: 'push' })) - ).toEqual([ 'stack' ]) + expect(Object.keys(reducer({}, { type: 'push' }))).toEqual(['stack']) }) it('warns if a reducer prop is undefined', () => { @@ -69,21 +71,13 @@ describe('Utils', () => { } }) - expect( - () => reducer({ counter: 0 }, { type: 'whatever' }) - ).toThrow( - /"whatever".*"counter"/ + expect(() => reducer({ counter: 0 }, { type: 'whatever' })).toThrow( + /"whatever".*"counter"/ ) - expect( - () => reducer({ counter: 0 }, null) - ).toThrow( - /"counter".*an action/ - ) - expect( - () => reducer({ counter: 0 }, { }) - ).toThrow( - /"counter".*an action/ + expect(() => reducer({ counter: 0 }, null)).toThrow( + /"counter".*an action/ ) + expect(() => reducer({ counter: 0 }, {})).toThrow(/"counter".*an action/) }) it('throws an error on first call if a reducer returns undefined initializing', () => { @@ -99,9 +93,7 @@ describe('Utils', () => { } } }) - expect(() => reducer({ })).toThrow( - /"counter".*initialization/ - ) + expect(() => reducer({})).toThrow(/"counter".*initialization/) }) it('catches error thrown in reducer when initializing and re-throw', () => { @@ -110,9 +102,7 @@ describe('Utils', () => { throw new Error('Error thrown in reducer') } }) - expect(() => reducer({ })).toThrow( - /Error thrown in reducer/ - ) + expect(() => reducer({})).toThrow(/Error thrown in reducer/) }) it('allows a symbol to be used as an action type', () => { @@ -134,13 +124,13 @@ describe('Utils', () => { it('maintains referential equality if the reducers it is combining do', () => { const reducer = combineReducers({ - child1(state = { }) { + child1(state = {}) { return state }, - child2(state = { }) { + child2(state = {}) { return state }, - child3(state = { }) { + child3(state = {}) { return state } }) @@ -151,7 +141,7 @@ describe('Utils', () => { it('does not have referential equality if one of the reducers changes something', () => { const reducer = combineReducers({ - child1(state = { }) { + child1(state = {}) { return state }, child2(state = { count: 0 }, action) { @@ -162,13 +152,15 @@ describe('Utils', () => { return state } }, - child3(state = { }) { + child3(state = {}) { return state } }) const initialState = reducer(undefined, '@@INIT') - expect(reducer(initialState, { type: 'increment' })).not.toBe(initialState) + expect(reducer(initialState, { type: 'increment' })).not.toBe( + initialState + ) }) it('throws an error on first call if a reducer attempts to handle a private action', () => { @@ -187,9 +179,7 @@ describe('Utils', () => { } } }) - expect(() => reducer()).toThrow( - /"counter".*private/ - ) + expect(() => reducer()).toThrow(/"counter".*private/) }) it('warns if no reducers are passed to combineReducers', () => { @@ -197,8 +187,8 @@ describe('Utils', () => { const spy = jest.fn() console.error = spy - const reducer = combineReducers({ }) - reducer({ }) + const reducer = combineReducers({}) + reducer({}) expect(spy.mock.calls[0][0]).toMatch( /Store does not have a valid reducer/ ) diff --git a/test/createStore.spec.js b/test/createStore.spec.js index 9583d8bc61..d11e389e6e 100644 --- a/test/createStore.spec.js +++ b/test/createStore.spec.js @@ -25,21 +25,13 @@ describe('createStore', () => { }) it('throws if reducer is not a function', () => { - expect(() => - createStore() - ).toThrow() + expect(() => createStore()).toThrow() - expect(() => - createStore('test') - ).toThrow() + expect(() => createStore('test')).toThrow() - expect(() => - createStore({}) - ).toThrow() + expect(() => createStore({})).toThrow() - expect(() => - createStore(() => {}) - ).not.toThrow() + expect(() => createStore(() => {})).not.toThrow() }) it('passes the initial state', () => { @@ -77,7 +69,8 @@ describe('createStore', () => { { id: 1, text: 'Hello' - }, { + }, + { id: 2, text: 'World' } @@ -111,7 +104,8 @@ describe('createStore', () => { { id: 1, text: 'Hello' - }, { + }, + { id: 2, text: 'World' } @@ -138,7 +132,8 @@ describe('createStore', () => { { id: 1, text: 'Hello' - }, { + }, + { id: 2, text: 'World' } @@ -299,19 +294,20 @@ describe('createStore', () => { const store = createStore(reducers.todos) const unsubscribeHandles = [] - const doUnsubscribeAll = () => unsubscribeHandles.forEach( - unsubscribe => unsubscribe() - ) + const doUnsubscribeAll = () => + unsubscribeHandles.forEach(unsubscribe => unsubscribe()) const listener1 = jest.fn() const listener2 = jest.fn() const listener3 = jest.fn() unsubscribeHandles.push(store.subscribe(() => listener1())) - unsubscribeHandles.push(store.subscribe(() => { - listener2() - doUnsubscribeAll() - })) + unsubscribeHandles.push( + store.subscribe(() => { + listener2() + doUnsubscribeAll() + }) + ) unsubscribeHandles.push(store.subscribe(() => listener3())) store.dispatch(unknownAction()) @@ -415,7 +411,7 @@ describe('createStore', () => { it('does not leak private listeners array', done => { const store = createStore(reducers.todos) - store.subscribe(function () { + store.subscribe(function() { expect(this).toBe(undefined) done() }) @@ -424,15 +420,11 @@ describe('createStore', () => { it('only accepts plain object actions', () => { const store = createStore(reducers.todos) - expect(() => - store.dispatch(unknownAction()) - ).not.toThrow() + expect(() => store.dispatch(unknownAction())).not.toThrow() - function AwesomeMap() { } - [ null, undefined, 42, 'hey', new AwesomeMap() ].forEach(nonObject => - expect(() => - store.dispatch(nonObject) - ).toThrow(/plain/) + function AwesomeMap() {} + ;[null, undefined, 42, 'hey', new AwesomeMap()].forEach(nonObject => + expect(() => store.dispatch(nonObject)).toThrow(/plain/) ) }) @@ -465,7 +457,9 @@ describe('createStore', () => { const store = createStore(reducers.dispatchInTheMiddleOfReducer) expect(() => - store.dispatch(dispatchInMiddle(store.dispatch.bind(store, unknownAction()))) + store.dispatch( + dispatchInMiddle(store.dispatch.bind(store, unknownAction())) + ) ).toThrow(/may not dispatch/) }) @@ -496,43 +490,31 @@ describe('createStore', () => { it('recovers from an error within a reducer', () => { const store = createStore(reducers.errorThrowingReducer) - expect(() => - store.dispatch(throwError()) - ).toThrow() + expect(() => store.dispatch(throwError())).toThrow() - expect(() => - store.dispatch(unknownAction()) - ).not.toThrow() + expect(() => store.dispatch(unknownAction())).not.toThrow() }) it('throws if action type is missing', () => { const store = createStore(reducers.todos) - expect(() => - store.dispatch({}) - ).toThrow(/Actions may not have an undefined "type" property/) + expect(() => store.dispatch({})).toThrow( + /Actions may not have an undefined "type" property/ + ) }) it('throws if action type is undefined', () => { const store = createStore(reducers.todos) - expect(() => - store.dispatch({ type: undefined }) - ).toThrow(/Actions may not have an undefined "type" property/) + expect(() => store.dispatch({ type: undefined })).toThrow( + /Actions may not have an undefined "type" property/ + ) }) it('does not throw if action type is falsy', () => { const store = createStore(reducers.todos) - expect(() => - store.dispatch({ type: false }) - ).not.toThrow() - expect(() => - store.dispatch({ type: 0 }) - ).not.toThrow() - expect(() => - store.dispatch({ type: null }) - ).not.toThrow() - expect(() => - store.dispatch({ type: '' }) - ).not.toThrow() + expect(() => store.dispatch({ type: false })).not.toThrow() + expect(() => store.dispatch({ type: 0 })).not.toThrow() + expect(() => store.dispatch({ type: null })).not.toThrow() + expect(() => store.dispatch({ type: '' })).not.toThrow() }) it('accepts enhancer as the third argument', () => { @@ -585,73 +567,47 @@ describe('createStore', () => { }) it('throws if enhancer is neither undefined nor a function', () => { - expect(() => - createStore(reducers.todos, undefined, {}) - ).toThrow() + expect(() => createStore(reducers.todos, undefined, {})).toThrow() - expect(() => - createStore(reducers.todos, undefined, []) - ).toThrow() + expect(() => createStore(reducers.todos, undefined, [])).toThrow() - expect(() => - createStore(reducers.todos, undefined, null) - ).toThrow() + expect(() => createStore(reducers.todos, undefined, null)).toThrow() - expect(() => - createStore(reducers.todos, undefined, false) - ).toThrow() + expect(() => createStore(reducers.todos, undefined, false)).toThrow() expect(() => createStore(reducers.todos, undefined, undefined) ).not.toThrow() - expect(() => - createStore(reducers.todos, undefined, x => x) - ).not.toThrow() + expect(() => createStore(reducers.todos, undefined, x => x)).not.toThrow() - expect(() => - createStore(reducers.todos, x => x) - ).not.toThrow() + expect(() => createStore(reducers.todos, x => x)).not.toThrow() - expect(() => - createStore(reducers.todos, []) - ).not.toThrow() + expect(() => createStore(reducers.todos, [])).not.toThrow() - expect(() => - createStore(reducers.todos, {}) - ).not.toThrow() + expect(() => createStore(reducers.todos, {})).not.toThrow() }) it('throws if nextReducer is not a function', () => { const store = createStore(reducers.todos) - expect(() => - store.replaceReducer() - ).toThrow('Expected the nextReducer to be a function.') + expect(() => store.replaceReducer()).toThrow( + 'Expected the nextReducer to be a function.' + ) - expect(() => - store.replaceReducer(() => {}) - ).not.toThrow() + expect(() => store.replaceReducer(() => {})).not.toThrow() }) it('throws if listener is not a function', () => { const store = createStore(reducers.todos) - expect(() => - store.subscribe() - ).toThrow() + expect(() => store.subscribe()).toThrow() - expect(() => - store.subscribe('') - ).toThrow() + expect(() => store.subscribe('')).toThrow() - expect(() => - store.subscribe(null) - ).toThrow() + expect(() => store.subscribe(null)).toThrow() - expect(() => - store.subscribe(undefined) - ).toThrow() + expect(() => store.subscribe(undefined)).toThrow() }) describe('Symbol.observable interop point', () => { @@ -671,15 +627,15 @@ describe('createStore', () => { const store = createStore(() => {}) const obs = store[$$observable]() - expect(function () { + expect(function() { obs.subscribe() }).toThrow() - expect(function () { + expect(function() { obs.subscribe(() => {}) }).toThrow() - expect(function () { + expect(function() { obs.subscribe({}) }).not.toThrow() }) @@ -714,7 +670,11 @@ describe('createStore', () => { store.dispatch({ type: 'foo' }) store.dispatch({ type: 'bar' }) - expect(results).toEqual([ { foo: 0, bar: 0 }, { foo: 1, bar: 0 }, { foo: 1, bar: 2 } ]) + expect(results).toEqual([ + { foo: 0, bar: 0 }, + { foo: 1, bar: 0 }, + { foo: 1, bar: 2 } + ]) }) it('should pass an integration test with an unsubscribe', () => { @@ -740,7 +700,7 @@ describe('createStore', () => { sub.unsubscribe() store.dispatch({ type: 'bar' }) - expect(results).toEqual([ { foo: 0, bar: 0 }, { foo: 1, bar: 0 } ]) + expect(results).toEqual([{ foo: 0, bar: 0 }, { foo: 1, bar: 0 }]) }) it('should pass an integration test with a common library (RxJS)', () => { @@ -764,7 +724,10 @@ describe('createStore', () => { sub.unsubscribe() store.dispatch({ type: 'bar' }) - expect(results).toEqual([ { foo: 0, bar: 0, fromRx: true }, { foo: 1, bar: 0, fromRx: true } ]) + expect(results).toEqual([ + { foo: 0, bar: 0, fromRx: true }, + { foo: 1, bar: 0, fromRx: true } + ]) }) }) @@ -774,19 +737,19 @@ describe('createStore', () => { const store = createStore( combineReducers({ - x: (s=0, a) => s, + x: (s = 0, a) => s, y: combineReducers({ - z: (s=0, a) => s, - w: (s=0, a) => s, - }), + z: (s = 0, a) => s, + w: (s = 0, a) => s + }) }) ) store.replaceReducer( combineReducers({ y: combineReducers({ - z: (s=0, a) => s, - }), + z: (s = 0, a) => s + }) }) ) diff --git a/test/helpers/actionCreators.js b/test/helpers/actionCreators.js index 5c26cdcc41..c75a7c3084 100644 --- a/test/helpers/actionCreators.js +++ b/test/helpers/actionCreators.js @@ -1,11 +1,11 @@ -import { - ADD_TODO, - DISPATCH_IN_MIDDLE, - GET_STATE_IN_MIDDLE, +import { + ADD_TODO, + DISPATCH_IN_MIDDLE, + GET_STATE_IN_MIDDLE, SUBSCRIBE_IN_MIDDLE, - UNSUBSCRIBE_IN_MIDDLE, - THROW_ERROR, - UNKNOWN_ACTION + UNSUBSCRIBE_IN_MIDDLE, + THROW_ERROR, + UNKNOWN_ACTION } from './actionTypes' export function addTodo(text) { @@ -13,10 +13,13 @@ export function addTodo(text) { } export function addTodoAsync(text) { - return dispatch => new Promise(resolve => setImmediate(() => { - dispatch(addTodo(text)) - resolve() - })) + return dispatch => + new Promise(resolve => + setImmediate(() => { + dispatch(addTodo(text)) + resolve() + }) + ) } export function addTodoIfEmpty(text) { diff --git a/test/helpers/middleware.js b/test/helpers/middleware.js index 945f412cc9..06f074fffa 100644 --- a/test/helpers/middleware.js +++ b/test/helpers/middleware.js @@ -1,6 +1,4 @@ export function thunk({ dispatch, getState }) { return next => action => - typeof action === 'function' ? - action(dispatch, getState) : - next(action) + typeof action === 'function' ? action(dispatch, getState) : next(action) } diff --git a/test/helpers/reducers.js b/test/helpers/reducers.js index 31ce7b99e1..f0030fb2dd 100644 --- a/test/helpers/reducers.js +++ b/test/helpers/reducers.js @@ -1,24 +1,23 @@ -import { - ADD_TODO, - DISPATCH_IN_MIDDLE, - GET_STATE_IN_MIDDLE, +import { + ADD_TODO, + DISPATCH_IN_MIDDLE, + GET_STATE_IN_MIDDLE, SUBSCRIBE_IN_MIDDLE, UNSUBSCRIBE_IN_MIDDLE, - THROW_ERROR + THROW_ERROR } from './actionTypes' - function id(state = []) { - return state.reduce((result, item) => ( - item.id > result ? item.id : result - ), 0) + 1 + return ( + state.reduce((result, item) => (item.id > result ? item.id : result), 0) + 1 + ) } export function todos(state = [], action) { switch (action.type) { case ADD_TODO: - return [ - ...state, + return [ + ...state, { id: id(state), text: action.text @@ -36,7 +35,8 @@ export function todosReverse(state = [], action) { { id: id(state), text: action.text - }, ...state + }, + ...state ] default: return state diff --git a/test/typescript.spec.js b/test/typescript.spec.js index de86902bb0..7d0aa42bc9 100644 --- a/test/typescript.spec.js +++ b/test/typescript.spec.js @@ -1,8 +1,7 @@ import * as tt from 'typescript-definition-tester' - -describe('TypeScript definitions', function () { - it('should compile against index.d.ts', (done) => { +describe('TypeScript definitions', function() { + it('should compile against index.d.ts', done => { tt.compileDirectory( __dirname + '/typescript', fileName => fileName.match(/\.ts$/), diff --git a/test/utils/isPlainObject.spec.js b/test/utils/isPlainObject.spec.js index 3314c5ff6e..ddb7775b46 100644 --- a/test/utils/isPlainObject.spec.js +++ b/test/utils/isPlainObject.spec.js @@ -14,9 +14,9 @@ describe('isPlainObject', () => { expect(isPlainObject(sandbox.fromAnotherRealm)).toBe(true) expect(isPlainObject(new Test())).toBe(false) expect(isPlainObject(new Date())).toBe(false) - expect(isPlainObject([ 1, 2, 3 ])).toBe(false) + expect(isPlainObject([1, 2, 3])).toBe(false) expect(isPlainObject(null)).toBe(false) expect(isPlainObject()).toBe(false) - expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true) + expect(isPlainObject({ x: 1, y: 2 })).toBe(true) }) })