|
| 1 | +import { act, renderHook } from '@testing-library/react-hooks'; |
| 2 | +import { useMarketingNotifications } from './useMarketingNotifications'; |
| 3 | + |
| 4 | +const notificationInstances: Array<{ |
| 5 | + title: string; |
| 6 | + options?: NotificationOptions; |
| 7 | + onclick: ((event: Event) => void) | null; |
| 8 | +}> = []; |
| 9 | + |
| 10 | +const NotificationMock = vi.fn(function NotificationMock( |
| 11 | + this: { title: string; options?: NotificationOptions; onclick: ((event: Event) => void) | null }, |
| 12 | + title: string, |
| 13 | + options?: NotificationOptions, |
| 14 | +) { |
| 15 | + this.title = title; |
| 16 | + this.options = options; |
| 17 | + this.onclick = null; |
| 18 | + notificationInstances.push(this); |
| 19 | +}); |
| 20 | + |
| 21 | +const notification = { |
| 22 | + id: 'notification-id', |
| 23 | + link: 'https://internxt.com/promo', |
| 24 | + message: 'Promo message', |
| 25 | + expiresAt: null, |
| 26 | + createdAt: '2024-01-01T00:00:00.000Z', |
| 27 | + isRead: false, |
| 28 | + deliveredAt: '2024-01-01T00:00:00.000Z', |
| 29 | + readAt: null, |
| 30 | +}; |
| 31 | + |
| 32 | +describe('useMarketingNotifications', () => { |
| 33 | + beforeEach(() => { |
| 34 | + notificationInstances.length = 0; |
| 35 | + vi.mocked(window.electron.onMarketingNotifications).mockReturnValue(vi.fn()); |
| 36 | + vi.mocked(window.electron.openUrl).mockResolvedValue(undefined); |
| 37 | + vi.stubGlobal('Notification', NotificationMock); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + vi.unstubAllGlobals(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should subscribe to marketing notifications and clean up on unmount', () => { |
| 45 | + const removeListener = vi.fn(); |
| 46 | + vi.mocked(window.electron.onMarketingNotifications).mockReturnValue(removeListener); |
| 47 | + |
| 48 | + const { unmount } = renderHook(() => useMarketingNotifications()); |
| 49 | + |
| 50 | + expect(window.electron.onMarketingNotifications).toHaveBeenCalledWith(expect.any(Function)); |
| 51 | + |
| 52 | + unmount(); |
| 53 | + |
| 54 | + expect(removeListener).toHaveBeenCalledOnce(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should show a native notification for each marketing notification', () => { |
| 58 | + renderHook(() => useMarketingNotifications()); |
| 59 | + const listener = vi.mocked(window.electron.onMarketingNotifications).mock.calls[0][0]; |
| 60 | + |
| 61 | + act(() => { |
| 62 | + listener([notification]); |
| 63 | + }); |
| 64 | + |
| 65 | + expect(NotificationMock).toHaveBeenCalledWith( |
| 66 | + 'Internxt Drive', |
| 67 | + expect.objectContaining({ |
| 68 | + body: 'Promo message', |
| 69 | + icon: expect.stringContaining('256x256.png'), |
| 70 | + }), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should open the marketing link when the notification is clicked', async () => { |
| 75 | + renderHook(() => useMarketingNotifications()); |
| 76 | + const listener = vi.mocked(window.electron.onMarketingNotifications).mock.calls[0][0]; |
| 77 | + |
| 78 | + act(() => { |
| 79 | + listener([notification]); |
| 80 | + }); |
| 81 | + |
| 82 | + await notificationInstances[0].onclick?.(new Event('click')); |
| 83 | + |
| 84 | + expect(window.electron.openUrl).toHaveBeenCalledWith('https://internxt.com/promo'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not open non-https notification links', async () => { |
| 88 | + renderHook(() => useMarketingNotifications()); |
| 89 | + const listener = vi.mocked(window.electron.onMarketingNotifications).mock.calls[0][0]; |
| 90 | + |
| 91 | + act(() => { |
| 92 | + listener([{ ...notification, link: 'internxt://notification/promo' }]); |
| 93 | + }); |
| 94 | + |
| 95 | + await notificationInstances[0].onclick?.(new Event('click')); |
| 96 | + |
| 97 | + expect(window.electron.openUrl).not.toHaveBeenCalled(); |
| 98 | + expect(window.electron.logger.error).toHaveBeenCalledWith( |
| 99 | + expect.objectContaining({ |
| 100 | + msg: '[RENDERER] Error opening marketing notification link', |
| 101 | + link: 'internxt://notification/promo', |
| 102 | + }), |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
0 commit comments