Skip to content

Instantly share code, notes, and snippets.

@RobinMalfait
Last active January 6, 2018 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinMalfait/b212d00f604fed852ff596766bb4ef19 to your computer and use it in GitHub Desktop.
Save RobinMalfait/b212d00f604fed852ff596766bb4ef19 to your computer and use it in GitHub Desktop.
it("should create new state when an action is namespaced", async () => {
// Types
const ACCUMULATOR = "ACCUMULATOR";
const ADD = "ADD";
const SUBTRACT = "SUBTRACT";
const MULTIPLY = "MULTIPLY";
// Action creators
function add(value) {
return createAction(namespace(ACCUMULATOR, ADD), value);
}
function subtract(value) {
return createAction(namespace(ACCUMULATOR, SUBTRACT), value);
}
function multiply(value) {
return createAction(namespace(ACCUMULATOR, MULTIPLY), value);
}
const defaultState = {
result: 0
};
// Reducer
const myReducer = testReducer(
createReducer(
{
/* Nested */
[ACCUMULATOR]: {
[ADD](state, action) {
return Object.assign({}, state, {
result: state.result + action.payload
});
},
[SUBTRACT](state, action) {
return Object.assign({}, state, {
result: state.result - action.payload
});
}
},
// In the root
[namespace(ACCUMULATOR, MULTIPLY)](state, action) {
return Object.assign({}, state, {
result: state.result * action.payload
});
}
},
defaultState
)
);
expect(
await myReducer(dispatchAll(add(2), add(2), subtract(1), multiply(2)))
).toMatchSnapshot();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment