Skip to content

Commit 707c9bb

Browse files
args in testAction, payload as array, check for no dispatches
I reused the testAction for all my action tests, so I made it a little more robust so I can pass in mocked arguments into the action as an array, and also check for more than one item in the payload. Lastly, I added a check at the end to handle scenarios where an action is called that leads to no mutations being dispatched.
1 parent adaa2c3 commit 707c9bb

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

docs/en/testing.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ const actions = actionsInjector({
6363
})
6464

6565
// helper for testing action with expected mutations
66-
const testAction = (action, state, expectedMutations, done) => {
66+
const testAction = (action, args, state, expectedMutations, done) => {
6767
let count = 0
6868
// mock dispatch
69-
const dispatch = (name, payload) => {
69+
const dispatch = (name, ...payload) => {
7070
const mutation = expectedMutations[count]
7171
expect(mutation.name).to.equal(name)
7272
if (payload) {
@@ -77,16 +77,19 @@ const testAction = (action, state, expectedMutations, done) => {
7777
done()
7878
}
7979
}
80-
// call the action with mocked store
81-
action({
82-
dispatch,
83-
state
84-
})
80+
// call the action with mocked store and arguments
81+
action({dispatch, state}, ...args)
82+
83+
// check if no mutations should have been dispatched
84+
if (count === 0) {
85+
expect(expectedMutations.length).to.equal(0)
86+
done()
87+
}
8588
}
8689

8790
describe('actions', () => {
8891
it('getAllProducts', done => {
89-
testAction(actions.getAllProducts, {}, [
92+
testAction(actions.getAllProducts, [], {}, [
9093
{ name: 'REQUEST_PRODUCTS' },
9194
{ name: 'RECEIVE_PRODUCTS', payload: [ /* mocked response */ ] }
9295
], done)

0 commit comments

Comments
 (0)