Trying to test my code that uses Notification API.
I don't think I'm mocking it correctly.
global.Notification = ({
requestPermission: jest.fn().mockImplementation(()=> {
console.log('reached here') //never logged
return 'denied';
}),
permission: 'denied',
} as unknown) as jest.Mocked<typeof Notification>;
test('should ask for permission from Notifications API ', async () => {
expect(global.Notification.requestPermission).toBeCalledTimes(1); // WORKS although console.log above never worked
});
This works.
But the following doesn't..
test('should create a new notification ', async () => {
// some code that eventually runs this
new Notification("Test");
expect(global.Notification).toBeCalledTimes(1); // TypeError: Notification is not a constructor
});
});