Currently, I am in the process of writing tests for my Vue application. One particular test involves a button that triggers a logout function. The goal is to determine if the function is executed when the button is clicked.
Initially, I attempted to mock the function using jest.fn() but encountered difficulties getting it to work as expected. Another approach I tried was directly triggering the method and adding a console.log statement within the method. However, the log did not display. What could be causing this issue?
Below is my setup for the wrapper:
let wrapper;
beforeEach(() => {
Vue.use(Vuetify);
Vue.prototype.$router = new VueRouter();
wrapper = shallowMount(Navigation);
});
afterEach(() => {
wrapper.destroy();
});
it('should call logout function on button click', function() {
let submitLogoutMock = jest.fn();
wrapper.vm.submitLogout = submitLogoutMock;
wrapper
.find('#logout-button')
.trigger('click');
expect(submitLogoutMock).toHaveBeenCalled();
});
The expectation is that the mocked method should be called, however, an error message stating:
Error: expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called, but it was not called.