Skip to content

Commit

Permalink
[chore] Add prettier to repo and run on src and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Vohra committed Oct 25, 2017
1 parent 1b19650 commit 59a328f
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 243 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
{
files: 'test/**/*.js',
env: {
jest: true,
jest: true,
},
},
],
Expand Down
4 changes: 4 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"clean": "rimraf lib dist es coverage",
"lint": "eslint src test build",
"lint": "eslint src test build && prettier --write '{src,test}/**/*.js'",
"test": "cross-env BABEL_ENV=commonjs jest",
"test:watch": "npm test -- --watch",
"test:cov": "npm test -- --coverage",
Expand Down Expand Up @@ -107,6 +107,7 @@
"gitbook-cli": "^2.3.2",
"glob": "^7.1.1",
"jest": "^21.2.1",
"prettier": "1.7.4",
"rimraf": "^2.6.2",
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 = store.dispatch
let chain = []
Expand Down
11 changes: 8 additions & 3 deletions src/bindActionCreators.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function bindActionCreator(actionCreator, dispatch) {
return function() { return dispatch(actionCreator.apply(this, arguments)) }
return function() {
return dispatch(actionCreator.apply(this, arguments))
}
}

/**
Expand Down Expand Up @@ -30,8 +32,11 @@ 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"?`
)
}

Expand Down
57 changes: 37 additions & 20 deletions src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import warning from './utils/warning'

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. ` +
Expand All @@ -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 (
Expand All @@ -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 => {
Expand All @@ -62,22 +68,28 @@ 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.`
)
}
})
Expand Down Expand Up @@ -135,7 +147,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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,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?'
)
}

Expand All @@ -172,7 +172,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()
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,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.'
)
}

Expand Down
4 changes: 1 addition & 3 deletions src/utils/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 24 additions & 18 deletions test/applyMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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', () => {
Expand Down Expand Up @@ -92,25 +95,28 @@ describe('applyMiddleware', () => {
})
})

it('passes through all arguments of dispatch calls from within middleware', () => {
const spy = jest.fn()
const testCallArgs = ['test']
function multiArgMiddleware() {
return next => (action, callArgs) => {
if (Array.isArray(callArgs)) {
return action(...callArgs)
}
return next(action)
}
}
function dummyMiddleware({ dispatch }) {
return next => action => dispatch(action, testCallArgs)
it('passes through all arguments of dispatch calls from within middleware', () => {
const spy = jest.fn()
const testCallArgs = ['test']
function multiArgMiddleware() {
return next => (action, callArgs) => {
if (Array.isArray(callArgs)) {
return action(...callArgs)
}
return next(action)
}
}
function dummyMiddleware({ dispatch }) {
return next => action => dispatch(action, testCallArgs)
}

const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware))
store.dispatch(spy)
expect(spy.mock.calls[0]).toEqual(testCallArgs)
})
const store = createStore(
reducers.todos,
applyMiddleware(multiArgMiddleware, dummyMiddleware)
)
store.dispatch(spy)
expect(spy.mock.calls[0]).toEqual(testCallArgs)
})

it('keeps unwrapped dispatch available while middleware is initializing', () => {
// This is documenting the existing behavior in Redux 3.x.
Expand Down
54 changes: 25 additions & 29 deletions test/bindActionCreators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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)
)
})
Expand All @@ -70,17 +68,15 @@ 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', () => {
expect(() => {
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"?'
)
})

Expand All @@ -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"?'
)
})

Expand All @@ -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"?'
)
})
})
Loading

0 comments on commit 59a328f

Please sign in to comment.