[Vuejs]-Mocking a simple function in VueJS, JEST

0👍

I think you have to make some changes to your original test case

  1. Change jest.fn() to jest.spyOn(Todo.methods, ‘deleteItem’) since you have to track calls to methods object in Todo component. Refer: https://jestjs.io/docs/jest-object
  2. Wait for the click event to be triggered with await
  3. Use toHaveBeenCalledTimes not toHaveBeenCalledWith("1")

So your final test case will look like this

describe("delete a todo", () => {
    test("should have todo removed", async () => {
      const removeItem = jest.spyOn(Todo.methods, 'removeItem')
      const items = [{ id: 1, name: "ana", isComplete: false }];
      const wrapper = shallowMount(Todo, items)
      await wrapper.find('.delete').trigger('click')
      expect(removeItem).toHaveBeenCalledTimes(1);
    });
});

Leave a comment