Created
September 20, 2017 07:32
-
-
Save Gomah/2b8c9662ea5dce92116a04509a719194 to your computer and use it in GitHub Desktop.
Unit testing for vuex actions using jest
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
const testAction = (action, payload, state, expectedMutations, done) => { | |
let count = 0; | |
// mock commit | |
const commit = (type, payload) => { | |
const mutation = expectedMutations[count]; | |
try { | |
expect(mutation.type).toEqual(type); | |
if (payload) { | |
expect(mutation.payload).toEqual(payload); | |
} | |
} catch (error) { | |
done(error); | |
} | |
count += 1; | |
if (count >= expectedMutations.length) { | |
done(); | |
} | |
}; | |
// call the action with mocked store and arguments | |
action({ commit, state }, payload); | |
// check if no mutations should have been dispatched | |
if (expectedMutations.length === 0) { | |
expect(count).toEqual(0); | |
done(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment