4

I am trying to simulate a button click of a quasar QBtn component in Jest(using vue-test-utils). I need to test if the @click method gets called when the button is clicked so I did the following

  it("Expects createAccount to be called", async () => {
    const button = wrapper.findComponent(QBtn);
    await button.trigger('click');

    expect(methods.createAccount).toBeCalled();
  })

And I also mocked createAccount function using jest.fn() But I always get 0 calls of the function, although it works if I directly use

wrapper.vm.createAccount()

And just check if the function got called... Any ideas how I can trigger the click event on the QBtn? I also tried using find('button') and triggering click, did not work either

Dobby Dimov
  • 944
  • 8
  • 10
myoda999
  • 335
  • 2
  • 11

2 Answers2

0

I would do it this way Hope it helps.

  it("expects createAccount to be called when button is clicked", async () => {
    // Arrange
    const button = wrapper.find('BUTTON CLASS NAME');
    const createAccount = jest.spyOn(wrapper.vm, 'createAccount');

    // Action
    await button.trigger('click');
    await wrapper.vm.$nextTick();

    // Assert
    expect(createAccount).toHaveBeenCalled();
  })
som
  • 1
-1

For those types of tests, I use cypress; personnaly I prefer to use jest to test methods, computed, etc...