Skip to content

Instantly share code, notes, and snippets.

@RobinMalfait

RobinMalfait/bigtest.js

Last active Jan 6, 2018
Embed
What would you like to do?
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