user9875
user9875

Reputation: 133

Vue utils - Check if a method is being called with jest

I am doing some tests and I want to check if a method is called after I trigger a click in an element, but the test keeps saying that is wasn't called and I don't know why

Here is my test:

test('some test', () => {
        const somethingChanged= jest.spyOn(Component.methods, 'somethingChanged')

        const wrapper = mount(Component, {
            propsData: data

        })

        const element= wrapper.find('.c-element').trigger('click')

        expect(somethingChanged).toBeCalled()
    })

This keeps saying that the number of calls is 0 and I don't know what I'm doing wrong This method is triggered in the component so I know it works

Upvotes: 4

Views: 5532

Answers (1)

Estus Flask
Estus Flask

Reputation: 222474

As trigger documentation states,

Triggers an event asynchronously on the Wrapper DOM node.

It should be:

const element= wrapper.find('.c-element').trigger('click')
await wrapper.vm.$nextTick()
expect(somethingChanged).toBeCalled()

The use of await assumes that test function should be async.

Upvotes: 5

Related Questions