Last active
January 6, 2018 10:50
-
-
Save RobinMalfait/b212d00f604fed852ff596766bb4ef19 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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