I am currently writing tests for my Vue application. I have a button which calls a logout function. I just want to test if the function gets called when the button gets clicked.
I have tried to mock the function with jest.fn() but i cant get it to work. I also tried to actually trigger the method and put a console.log in the method but that log did not get called. What am I doing wrong?
this is my wrapper setup:
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();
});
I expect the mocked method to be called but actually i get an error saying:
Error: expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called, but it was not called.
I am currently writing tests for my Vue application. I have a button which calls a logout function. I just want to test if the function gets called when the button gets clicked.
I have tried to mock the function with jest.fn() but i cant get it to work. I also tried to actually trigger the method and put a console.log in the method but that log did not get called. What am I doing wrong?
this is my wrapper setup:
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();
});
I expect the mocked method to be called but actually i get an error saying:
Error: expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called, but it was not called.
click
event or a native one, because right now it looks like you're triggering a native click
event.
– Husam Elbashir
Commented
Oct 14, 2019 at 12:07
<v-btn icon id="logout-button" @click.native="logout">
I tried researching again and added the .native
and now my test works.. can you explain why i dont really understand the difference between a native and a normal click
– baxter3000
Commented
Oct 14, 2019 at 12:28
.native
modifier listens for native DOM events, such as a click
event emitted by a button
element. By default when you use v-on
or the@
shorthand vue listens for custom events, ones you trigger using $emit
.
– Husam Elbashir
Commented
Oct 14, 2019 at 13:36
When using shallowMount
the methods of ponent should be stubbed. You can achieve this while creating the wrapper or by using setMethods()
.
You only need to change your unit test:
it('should call logout function on button click', () => {
const submitLogoutMock = jest.fn();
wrapper.setMethods({ submitLogout: submitLogoutMock });
wrapper.find('#logout-button').trigger('click');
expect(submitLogoutMock).toHaveBeenCalled();
});