Skip to content

Instantly share code, notes, and snippets.

@achhunna
Last active May 19, 2019 18:05
Show Gist options
  • Save achhunna/7c9876edff249cfd776cc4b0a4970059 to your computer and use it in GitHub Desktop.
Save achhunna/7c9876edff249cfd776cc4b0a4970059 to your computer and use it in GitHub Desktop.
Mocking methods
// using jest.fn() to mock action and mutation functions
beforeEach(() => {
actions = {
someAction: jest.fn(() => true)
};
mutations = {
someMutation: jest.fn(() => false)
};
state = {
key: {}
};
store = new Vuex.Store({
actions,
mutations,
state,
});
...
});
...
// using jest.fn() to mock component method or another other global functions
test('clicking search button triggers search()', () => {
window.open = jest.fn(); // global function mock to stop actual from executing
wrapper.vm.search = jest.fn();
const searchBtn = '.search-btn';
wrapper.find(searchBtn).trigger('click');
expect(wrapper.vm.search).toBeCalled();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment