I have this code:
const browserNotification = new Notification(title, options);
browserNotification.onshow = () => this.props.sendTracking('in-app-chat-notification-start');
browserNotification.onclick = event => {
const notification = event.target;
this.props.router.push(notification.data.url.split('.com')[1]);
this.props.sendTracking('in-app-chat-notification-complete');
notification.close();
};
I'm trying to mock window Notification because I need to call these two methods (onshow and onclick):
it('should call onshow method', () => {
window.Notification = () => ({
onshow: jest.fn(),
onclick: jest.fn()
});
const instance = shallow(<MyComponent />).instance();
instance.myMethod();
//window.Notification.onShow();
expect(sendTracking).toHaveBeenCalledTimes(1);
});
I know that if I do that, I'm losing the original method but, since tests does not have window and Notification, I have to mock it.
How can do it properly with Jest in order to pass this code?
I have tried also mocking with global, but at the end, is the same as window.
The coverage:
Thanks