Skip to content

Instantly share code, notes, and snippets.

@takahirohonda
Created October 14, 2021 03:24
Show Gist options
  • Save takahirohonda/8d40cc97f912dfc0cc60a606747e9a4a to your computer and use it in GitHub Desktop.
Save takahirohonda/8d40cc97f912dfc0cc60a606747e9a4a to your computer and use it in GitHub Desktop.
MyButton.test.jsx
// mock async function first
const someAsyncFunc = jest.fn().mockResolvedValue({ success: true });
jest.spyOn(hooks, 'someHook').mockImplementation(() => ({ someAsyncFunc }));
...
await act(async() => {
wrapper
.findWhere((node) => node.text() === 'Elenberg Fraser')
.find('button')
.simulate('click');
});
// This one is not necessary unless we are asserting the change in UI
// wrapper.update();
expect(someAsyncFunc).toHaveBeenCalledTimes(1);
// Add window.open = jest.fn(); in setup.js file for jest global config
expect(window.open).toHaveBeenCalledTimes(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment