Skip to content

Instantly share code, notes, and snippets.

@AmmarCodes
Created December 10, 2019 13:14
Show Gist options
  • Save AmmarCodes/67390825bebd8f0969c32cb1ad483235 to your computer and use it in GitHub Desktop.
Save AmmarCodes/67390825bebd8f0969c32cb1ad483235 to your computer and use it in GitHub Desktop.
Mock window.confirm in Jest
describe('mocking window.confirm', () => {
// mock window.confirm
let windowSpy;
beforeEach(() => {
windowSpy = jest.spyOn(global, 'window', 'get');
windowSpy.mockImplementation(() => ({
confirm: () => {},
}));
});
afterEach(() => {
windowSpy.mockRestore();
});
it('show confirm when clicking delete card button', () => {
// @TODO do the action that uses window.confirm here.
expect(windowSpy).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment