4

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

1 Answer 1

5

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.

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.