1 import { renderHook } from '@testing-library/react-hooks';
3 import useInboxDesktopBadgeCount from '@proton/components/hooks/useInboxDesktopBadgeCount';
4 import { useConversationCounts } from '@proton/mail/counts/conversationCounts';
5 import { useMessageCounts } from '@proton/mail/counts/messageCounts';
6 import { useMailSettings } from '@proton/mail/mailSettings/hooks';
7 import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants';
8 import * as desktopHelpers from '@proton/shared/lib/helpers/desktop';
9 import { VIEW_MODE } from '@proton/shared/lib/mail/mailSettings';
11 jest.mock('@proton/mail/mailSettings/hooks');
12 jest.mock('@proton/mail/counts/conversationCounts');
13 jest.mock('@proton/mail/counts/messageCounts');
14 jest.mock('@proton/shared/lib/helpers/desktop');
15 const desktopHelpersMock = desktopHelpers as jest.MockedObject<typeof desktopHelpers>;
17 declare const global: {
18 ipcInboxMessageBroker?: any;
21 const originalWindow = { ...window };
23 describe('useInboxDesktopBadgeCount', () => {
24 const useConversationCountsMock = useConversationCounts as jest.Mock;
25 const useMessageCountsMock = useMessageCounts as jest.Mock;
26 const useMailSettingsMock = useMailSettings as jest.Mock;
27 const ipcInboxMessageBrokerMock = {
33 const windowSpy = jest.spyOn(global, 'window', 'get');
34 windowSpy.mockReturnValue({ ...originalWindow, ipcInboxMessageBroker: ipcInboxMessageBrokerMock });
41 it('should not call when not on desktop', () => {
42 desktopHelpersMock.isElectronMail = false;
43 useConversationCountsMock.mockReturnValue([]);
44 useMessageCountsMock.mockReturnValue([]);
46 renderHook(() => useInboxDesktopBadgeCount());
47 expect(ipcInboxMessageBrokerMock.send).not.toHaveBeenCalled();
50 it('should call with 0 when no count', () => {
51 desktopHelpersMock.isElectronMail = true;
52 useMailSettingsMock.mockReturnValue([{ ViewMode: VIEW_MODE.GROUP }]);
53 useConversationCountsMock.mockReturnValue([[]]);
54 useMessageCountsMock.mockReturnValue([]);
56 renderHook(() => useInboxDesktopBadgeCount());
57 expect(ipcInboxMessageBrokerMock.send).toHaveBeenCalledWith('updateNotification', 0);
60 it('should call with 0 when negative count', () => {
61 desktopHelpersMock.isElectronMail = true;
62 useMailSettingsMock.mockReturnValue([{ ViewMode: VIEW_MODE.GROUP }]);
63 useConversationCountsMock.mockReturnValue([[{ LabelID: MAILBOX_LABEL_IDS.INBOX, Unread: -1, Total: 1 }]]);
64 useMessageCountsMock.mockReturnValue([]);
66 renderHook(() => useInboxDesktopBadgeCount());
67 expect(ipcInboxMessageBrokerMock.send).toHaveBeenCalledWith('updateNotification', 0);
70 it('should call with 1 when 1 unread conversation', () => {
71 desktopHelpersMock.isElectronMail = true;
72 useMailSettingsMock.mockReturnValue([{ ViewMode: VIEW_MODE.GROUP }]);
73 useConversationCountsMock.mockReturnValue([[{ LabelID: MAILBOX_LABEL_IDS.INBOX, Unread: 1, Total: 1 }]]);
74 useMessageCountsMock.mockReturnValue([]);
76 renderHook(() => useInboxDesktopBadgeCount());
77 expect(ipcInboxMessageBrokerMock.send).toHaveBeenCalledWith('updateNotification', 1);
80 it('should call with 100 when 100 unread conversation', () => {
81 desktopHelpersMock.isElectronMail = true;
82 useMailSettingsMock.mockReturnValue([{ ViewMode: VIEW_MODE.GROUP }]);
83 useConversationCountsMock.mockReturnValue([[{ LabelID: MAILBOX_LABEL_IDS.INBOX, Unread: 100, Total: 100 }]]);
84 useMessageCountsMock.mockReturnValue([]);
86 renderHook(() => useInboxDesktopBadgeCount());
87 expect(ipcInboxMessageBrokerMock.send).toHaveBeenCalledWith('updateNotification', 100);
90 it('should call with 1 when 1 unread message', () => {
91 desktopHelpersMock.isElectronMail = true;
92 useMailSettingsMock.mockReturnValue([{ ViewMode: VIEW_MODE.SINGLE }]);
93 useConversationCountsMock.mockReturnValue([]);
94 useMessageCountsMock.mockReturnValue([[{ LabelID: MAILBOX_LABEL_IDS.INBOX, Unread: 1, Total: 1 }]]);
96 renderHook(() => useInboxDesktopBadgeCount());
97 expect(ipcInboxMessageBrokerMock.send).toHaveBeenCalledWith('updateNotification', 1);
100 it('should call with 100 when 100 unread message', () => {
101 desktopHelpersMock.isElectronMail = true;
102 useMailSettingsMock.mockReturnValue([{ ViewMode: VIEW_MODE.SINGLE }]);
103 useConversationCountsMock.mockReturnValue([]);
104 useMessageCountsMock.mockReturnValue([[{ LabelID: MAILBOX_LABEL_IDS.INBOX, Unread: 100, Total: 100 }]]);
106 renderHook(() => useInboxDesktopBadgeCount());
107 expect(ipcInboxMessageBrokerMock.send).toHaveBeenCalledWith('updateNotification', 100);