Skip to content

Instantly share code, notes, and snippets.

@mdahlstrom
Created August 9, 2017 04:16
Show Gist options
  • Save mdahlstrom/4e9ab8f11f5ddca437f3df809fe6ed9a to your computer and use it in GitHub Desktop.
Save mdahlstrom/4e9ab8f11f5ddca437f3df809fe6ed9a to your computer and use it in GitHub Desktop.
Chai based class for testing vuex actions. Based on the samples available at https://vuex.vuejs.org/en/testing.html but accepts functions instead of object to validate payload for dispatched actions and mutations.
import {expect} from 'chai'
export default class VuexActionTester {
constructor(action, payload, mutations, dispatches, done) {
this.action = action
this.payload = payload
this.state = {}
this.mutations = mutations
this.mutationsCount = 0
this.dispatchesCount = 0
this.dispatches = dispatches
this.done = done
}
async run () {
const dispatch = this.dispatch.bind(this)
const commit = this.commit.bind(this)
const error = undefined
try {
await this.action({dispatch,commit}, this.payload)
expect(this.mutationsCount).to.equal(this.mutations.length)
expect(this.dispatchesCount).to.equal(this.dispatches.length)
this.done()
} catch (e) {
this.done(e)
}
}
commit (type,payload) {
if(this.mutations.length === 0) {
throw Error('Expected no mutations')
}
const mutation = this.mutations[this.mutationsCount]
if(mutation === undefined) {
throw Error('Received more mutations then expected')
}
expect(mutation.type).to.equal(type)
expect(mutation.validation(payload)).to.not.throw
this.mutationsCount++
}
dispatch (type,payload) {
if(this.dispatches.length === 0) {
throw Error('Expected no dispatches')
}
const dispatch = this.dispatches[this.dispatchesCount]
if(dispatch === undefined) {
throw Error('Received more dispatches then expected')
}
expect(dispatch.type).to.equal(type)
expect(dispatch.validation(payload)).to.not.throw
this.dispatchesCount++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment