forked from reduxjs/redux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
213 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'; | ||
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER'; | ||
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER' | ||
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER' | ||
|
||
export function increment() { | ||
return { | ||
type: INCREMENT_COUNTER | ||
}; | ||
} | ||
} | ||
|
||
export function decrement() { | ||
return { | ||
type: DECREMENT_COUNTER | ||
}; | ||
} | ||
} | ||
|
||
export function incrementIfOdd() { | ||
return (dispatch, getState) => { | ||
const { counter } = getState(); | ||
const { counter } = getState() | ||
|
||
if (counter % 2 === 0) { | ||
return; | ||
return | ||
} | ||
|
||
dispatch(increment()); | ||
}; | ||
dispatch(increment()) | ||
} | ||
} | ||
|
||
export function incrementAsync(delay = 1000) { | ||
return dispatch => { | ||
setTimeout(() => { | ||
dispatch(increment()); | ||
}, delay); | ||
}; | ||
dispatch(increment()) | ||
}, delay) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import Counter from '../components/Counter'; | ||
import * as CounterActions from '../actions/counter'; | ||
import { bindActionCreators } from 'redux' | ||
import { connect } from 'react-redux' | ||
import Counter from '../components/Counter' | ||
import * as CounterActions from '../actions/counter' | ||
|
||
function mapStateToProps(state) { | ||
return { | ||
counter: state.counter | ||
}; | ||
} | ||
} | ||
|
||
function mapDispatchToProps(dispatch) { | ||
return bindActionCreators(CounterActions, dispatch); | ||
return bindActionCreators(CounterActions, dispatch) | ||
} | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Counter); | ||
export default connect(mapStateToProps, mapDispatchToProps)(Counter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import { Provider } from 'react-redux'; | ||
import App from './containers/App'; | ||
import configureStore from './store/configureStore'; | ||
import React from 'react' | ||
import { render } from 'react-dom' | ||
import { Provider } from 'react-redux' | ||
import App from './containers/App' | ||
import configureStore from './store/configureStore' | ||
|
||
const store = configureStore(); | ||
const store = configureStore() | ||
|
||
render( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider>, | ||
document.getElementById('root') | ||
); | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions/counter'; | ||
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions/counter' | ||
|
||
export default function counter(state = 0, action) { | ||
switch (action.type) { | ||
case INCREMENT_COUNTER: | ||
return state + 1; | ||
case DECREMENT_COUNTER: | ||
return state - 1; | ||
default: | ||
return state; | ||
case INCREMENT_COUNTER: | ||
return state + 1 | ||
case DECREMENT_COUNTER: | ||
return state - 1 | ||
default: | ||
return state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { combineReducers } from 'redux'; | ||
import counter from './counter'; | ||
import { combineReducers } from 'redux' | ||
import counter from './counter' | ||
|
||
const rootReducer = combineReducers({ | ||
counter | ||
}); | ||
}) | ||
|
||
export default rootReducer; | ||
export default rootReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
var webpack = require('webpack'); | ||
var webpackDevMiddleware = require('webpack-dev-middleware'); | ||
var webpackHotMiddleware = require('webpack-hot-middleware'); | ||
var config = require('./webpack.config'); | ||
var webpack = require('webpack') | ||
var webpackDevMiddleware = require('webpack-dev-middleware') | ||
var webpackHotMiddleware = require('webpack-hot-middleware') | ||
var config = require('./webpack.config') | ||
|
||
var app = new require('express')(); | ||
var port = 3000; | ||
var app = new require('express')() | ||
var port = 3000 | ||
|
||
var compiler = webpack(config); | ||
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath })); | ||
app.use(webpackHotMiddleware(compiler)); | ||
var compiler = webpack(config) | ||
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath })) | ||
app.use(webpackHotMiddleware(compiler)) | ||
|
||
app.get("/", function(req, res) { | ||
res.sendFile(__dirname + '/index.html'); | ||
}); | ||
res.sendFile(__dirname + '/index.html') | ||
}) | ||
|
||
app.listen(port, function(error) { | ||
if (error) { | ||
console.error(error); | ||
console.error(error) | ||
} else { | ||
console.info("==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port); | ||
console.info("==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port) | ||
} | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import thunk from 'redux-thunk'; | ||
import reducer from '../reducers'; | ||
import { createStore, applyMiddleware } from 'redux' | ||
import thunk from 'redux-thunk' | ||
import reducer from '../reducers' | ||
|
||
const createStoreWithMiddleware = applyMiddleware( | ||
thunk | ||
)(createStore); | ||
)(createStore) | ||
|
||
export default function configureStore(initialState) { | ||
const store = createStoreWithMiddleware(reducer, initialState); | ||
const store = createStoreWithMiddleware(reducer, initialState) | ||
|
||
if (module.hot) { | ||
// Enable Webpack hot module replacement for reducers | ||
module.hot.accept('../reducers', () => { | ||
const nextReducer = require('../reducers'); | ||
store.replaceReducer(nextReducer); | ||
}); | ||
const nextReducer = require('../reducers') | ||
store.replaceReducer(nextReducer) | ||
}) | ||
} | ||
|
||
return store; | ||
return store | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,76 @@ | ||
import expect from 'expect'; | ||
import { applyMiddleware } from 'redux'; | ||
import thunk from 'redux-thunk'; | ||
import * as actions from '../../actions/counter'; | ||
import expect from 'expect' | ||
import { applyMiddleware } from 'redux' | ||
import thunk from 'redux-thunk' | ||
import * as actions from '../../actions/counter' | ||
|
||
const middlewares = [thunk]; | ||
const middlewares = [ thunk ] | ||
|
||
/* | ||
* Creates a mock of Redux store with middleware. | ||
*/ | ||
function mockStore(getState, expectedActions, onLastAction) { | ||
if (!Array.isArray(expectedActions)) { | ||
throw new Error('expectedActions should be an array of expected actions.'); | ||
throw new Error('expectedActions should be an array of expected actions.') | ||
} | ||
if (typeof onLastAction !== 'undefined' && typeof onLastAction !== 'function') { | ||
throw new Error('onLastAction should either be undefined or function.'); | ||
throw new Error('onLastAction should either be undefined or function.') | ||
} | ||
|
||
function mockStoreWithoutMiddleware() { | ||
return { | ||
getState() { | ||
return typeof getState === 'function' ? | ||
getState() : | ||
getState; | ||
getState | ||
}, | ||
|
||
dispatch(action) { | ||
const expectedAction = expectedActions.shift(); | ||
expect(action).toEqual(expectedAction); | ||
const expectedAction = expectedActions.shift() | ||
expect(action).toEqual(expectedAction) | ||
if (onLastAction && !expectedActions.length) { | ||
onLastAction(); | ||
onLastAction() | ||
} | ||
return action; | ||
return action | ||
} | ||
}; | ||
} | ||
} | ||
|
||
const mockStoreWithMiddleware = applyMiddleware( | ||
...middlewares | ||
)(mockStoreWithoutMiddleware); | ||
)(mockStoreWithoutMiddleware) | ||
|
||
return mockStoreWithMiddleware(); | ||
return mockStoreWithMiddleware() | ||
} | ||
|
||
describe('actions', () => { | ||
it('increment should create increment action', () => { | ||
expect(actions.increment()).toEqual({ type: actions.INCREMENT_COUNTER }); | ||
}); | ||
expect(actions.increment()).toEqual({ type: actions.INCREMENT_COUNTER }) | ||
}) | ||
|
||
it('decrement should create decrement action', () => { | ||
expect(actions.decrement()).toEqual({ type: actions.DECREMENT_COUNTER }); | ||
}); | ||
expect(actions.decrement()).toEqual({ type: actions.DECREMENT_COUNTER }) | ||
}) | ||
|
||
it('incrementIfOdd should create increment action', (done) => { | ||
const expectedActions = [ | ||
{ type: actions.INCREMENT_COUNTER } | ||
]; | ||
const store = mockStore({ counter: 1 }, expectedActions, done); | ||
store.dispatch(actions.incrementIfOdd()); | ||
}); | ||
] | ||
const store = mockStore({ counter: 1 }, expectedActions, done) | ||
store.dispatch(actions.incrementIfOdd()) | ||
}) | ||
|
||
it('incrementIfOdd shouldnt create increment action if counter is even', (done) => { | ||
const expectedActions = []; | ||
const store = mockStore({ counter: 2 }, expectedActions); | ||
store.dispatch(actions.incrementIfOdd()); | ||
done(); | ||
}); | ||
const expectedActions = [] | ||
const store = mockStore({ counter: 2 }, expectedActions) | ||
store.dispatch(actions.incrementIfOdd()) | ||
done() | ||
}) | ||
|
||
it('incrementAsync should create increment action', (done) => { | ||
const expectedActions = [ | ||
{ type: actions.INCREMENT_COUNTER } | ||
]; | ||
const store = mockStore({ counter: 0 }, expectedActions, done); | ||
store.dispatch(actions.incrementAsync(100)); | ||
}); | ||
}); | ||
] | ||
const store = mockStore({ counter: 0 }, expectedActions, done) | ||
store.dispatch(actions.incrementAsync(100)) | ||
}) | ||
}) |
Oops, something went wrong.