Skip to content

Instantly share code, notes, and snippets.

@chudaol
Created November 20, 2016 17:33
Show Gist options
  • Save chudaol/addb6657095406234bc6f659970f3eb8 to your computer and use it in GitHub Desktop.
Save chudaol/addb6657095406234bc6f659970f3eb8 to your computer and use it in GitHub Desktop.
Asynchronous test that tests dispatch method being called with mocked server's response
import actions from 'src/vuex/actions'
import { CHANGE_TITLE, POPULATE_SHOPPING_LISTS, ADD_SHOPPING_LIST, DELETE_SHOPPING_LIST } from 'src/vuex/mutation_types'
describe('actions.js', () => {
var lists, store, server
beforeEach(() => {
// mock shopping lists
lists = [{
id: '1',
title: 'Groceries'
}, {
id: '2',
title: 'Clothes'
}]
// mock store commit method
store = {
commit: (method, data) => {},
state: {
shoppinglists: lists
}
}
sinon.stub(store, 'commit')
// mock server
server = sinon.fakeServer.create()
server.respondWith('GET', /shoppinglists/, xhr => {
xhr.respond(200, {'Content-Type': 'application/json'}, JSON.stringify(lists))
})
server.autoRespond = true
})
afterEach(() => {
// restore stubs and server mock
store.commit.restore()
server.restore()
})
describe('populateShoppingLists', () => {
it('should call commit method with POPULATE_SHOPPING_LIST string parameter', done => {
actions.populateShoppingLists(store).then(() => {
expect(store.commit).to.have.been.calledWith(POPULATE_SHOPPING_LISTS, lists)
done()
}).catch(done)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment