avoriaz
avoriaz copied to clipboard
`setMethods` not working for `mounted` methods
Hello,
When I try to mock a methods with setMethods
to prevent api call it works only when the function is called after mount.
example:
index.vue:
import axios from 'axios'
{
data () {
return {
items: []
}
},
mounted () {
this.getItems()
},
methods: {
getItems () {
axios.get('/api/items')
.then((res) => {
this.items = res
})
}
}
}
test.spec.js
import { mount } from 'avoriaz'
import Component from './index.vue'
// build component
const vm = mount(Component)
const getItems = () => {
console.log('items')
}
vm.setMethods({getItems})
It still get WARN [web-server]: 404: /api/items
Any advice?
Yes, sorry you'll have to stub the actual component before you mount, which isn't ideal.
import { mount } from 'avoriaz'
import Component from './index.vue'
// build component
Component.methodToStub = () => {}
const vm = mount(Component)
You could deep clone the component before mounting.
import { mount } from 'avoriaz'
import { cloneDeep } from 'lodash'
import Component from './index.vue'
// build component
const ClonedComponent = cloneDeep(Component)
ClonedComponent.methodToStub = () => {}
const vm = mount(ClonedComponent)
Going forward, there should be a better way to stub methods like this. I'll add a feature request label. If anyone has suggestions for an API, I'd love to hear them 😃
Closing issue was an accident.
Thank you for your answer.