Skip to content

Instantly share code, notes, and snippets.

@aphofstede
Created April 13, 2018 11:05
Show Gist options
  • Save aphofstede/180af69331d484d7f16b0e56fd1ce5aa to your computer and use it in GitHub Desktop.
Save aphofstede/180af69331d484d7f16b0e56fd1ce5aa to your computer and use it in GitHub Desktop.
An example test that tests an axios call is made in a Vue component's mounted() function
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import expect from 'expect'
import axios from 'axios'
import moxios from 'moxios'
import sinon from "sinon"
describe ('Test axios in mounted()', () => {
let wrapper
let Mounted = Vue.component('Mounted',
{
template: `<div></div>`,
data() {
return {
data: {}
}
},
mounted() {
this.fetch()
},
methods: {
async fetch() {
let data = await axios.post('/test', {param: 'example'})
this.data = data
}
}
}
)
beforeEach(() => {
// Ajax stub
moxios.install()
wrapper = mount(Mounted)
})
afterEach(() => {
// Ajax stub
moxios.uninstall()
})
it ('should send off a request when mounted', (done) => {
// Given we set up axios to return something
moxios.stubRequest('/test', {
status: 200,
response: []
})
// When the wrapper was mounted
wrapper = mount(Mounted)
moxios.wait(() => {
// It should have called axios
done()
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment