Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setMethods not working for mounted methods #138

Open
wesssel opened this issue Sep 26, 2017 · 2 comments
Open

setMethods not working for mounted methods #138

wesssel opened this issue Sep 26, 2017 · 2 comments

Comments

@wesssel
Copy link

wesssel commented Sep 26, 2017

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?

@eddyerburgh
Copy link
Owner

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 😃

@wesssel wesssel closed this as completed Sep 26, 2017
@wesssel wesssel reopened this Sep 26, 2017
@wesssel
Copy link
Author

wesssel commented Sep 26, 2017

Closing issue was an accident.

Thank you for your answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants