Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test("should call the `onDismiss` callback when escape key is pressed and `visible` prop is true", () => {
const onDismiss: any = jest.fn();
const map: any = {};
// Mock window.addEventListener
window.addEventListener = jest.fn((event: string, callback: any) => {
map[event] = callback;
});
const rendered: any = mount(
<dialog>
);
map.keydown({ keyCode: KeyCodes.escape });
expect(onDismiss).toHaveBeenCalledTimes(0);
// set visible prop
rendered.setProps({ visible: true });
map.keydown({ keyCode: KeyCodes.escape });
expect(onDismiss).toHaveBeenCalledTimes(1);
});
</dialog>
window.addEventListener = jest.fn((event: string, callback: any) => {
map[event] = callback;
});
const rendered: any = mount(
<dialog>
);
map.keydown({ keyCode: KeyCodes.escape });
expect(onDismiss).toHaveBeenCalledTimes(0);
// set visible prop
rendered.setProps({ visible: true });
map.keydown({ keyCode: KeyCodes.escape });
expect(onDismiss).toHaveBeenCalledTimes(1);
});
</dialog>