1

I have a problem with unit test to trigger click. Error: Expected: 1, Received: 0 I'm using Vue, Jest and Vue test utils. I wanna test if the button is triggered

Search.vue

<v-btn id="searchBtn" @click="searchItem"></v-btn>

methods: {
    searchItem() {}
        ...
    }

test.spec.js

import Search from '...'

describle(Search, () => {
    it('trigger button', () => {
       const wrapper = shallowMount(Search)

        const clickMethodStub = jest.fn()

        wrapper.setMethods({ searchItem: clickMethodStub })
        wrapper.find('#searchBtn').trigger('click') 

        expect(clickMethodStub.mock.calls.length).toBe(1) 
        })
    })
}

Error: Expected: 1 Received: 0

1
  • Could you please post ur wrapper.html()? Commented Feb 14, 2021 at 19:26

1 Answer 1

1

Because it is vuetify button, you need to use to shallowMount to mount.

import Search from '...'

describle(Search, () => {
    it('trigger button', () => {
       const wrapper = mount(Search)

        const clickMethodStub = jest.fn()

        wrapper.setMethods({ searchItem: clickMethodStub })
        wrapper.find('#searchBtn').trigger('click') 

        expect(clickMethodStub.mock.calls.length).toBe(1) 
        })
    })
}
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.