avoriaz icon indicating copy to clipboard operation
avoriaz copied to clipboard

`setMethods` not working for `mounted` methods

Open wesssel opened this issue 6 years ago • 2 comments

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?

wesssel avatar Sep 26 '17 01:09 wesssel

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 😃

eddyerburgh avatar Sep 26 '17 05:09 eddyerburgh

Closing issue was an accident.

Thank you for your answer.

wesssel avatar Sep 26 '17 06:09 wesssel