Skip to content

Instantly share code, notes, and snippets.

@Gomah
Created September 20, 2017 07:32
Show Gist options
  • Save Gomah/2b8c9662ea5dce92116a04509a719194 to your computer and use it in GitHub Desktop.
Save Gomah/2b8c9662ea5dce92116a04509a719194 to your computer and use it in GitHub Desktop.
Unit testing for vuex actions using jest
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