0

After a lot of research I was finally able to test an method with the follow code

  it('should call myFunction - on Desktop', async () => {
    const mockMethod = jest.spyOn(myComponent.methods, 'myFunction')
    const wrapper = shallowMount(
      myComponent,
      {
        store,
        localVue,
        propsData,
        stubs,
      },
    )
    await wrapper.findAll('.container button-stub').at(0).trigger('click')
    expect(mockMethod).toHaveBeenCalled()
  })

And it worked :pray-pep: But know I need to test an method passing an parameter and comparing the result. This method changes my data, my data is like follows

  data () {
    return {
      myOptions: [],
    }
  },

This method adds the argument, that is an number, in myOptions. That is what an want to test, call the function and compare if the parameter passed was added to myOptions

Sorry for my english and talks for the help

1 Answer 1

0

Assuming myFunction is the method you speak of, and it's called like this:

this.myFunction(123)

You could use Jest's toHaveBeenCalledWith() assertion to verify it was called with specific arguments, and then check the value of myOptions:

expect(mockMethod).toHaveBeenCalledWith(123)
expect(wrapper.vm.myOptions).toInclude(123)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.