Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / utils / appPlatforms.test.ts
blob4348394a48be65a2f821ad687460d673a8d79b0a
1 import { fetchDesktopVersion } from '@proton/shared/lib/apps/desktopVersions';
2 import { DESKTOP_PLATFORMS } from '@proton/shared/lib/constants';
3 import { isMac, isWindows } from '@proton/shared/lib/helpers/browser';
5 import { appPlatforms, fetchDesktopDownloads } from './appPlatforms';
7 jest.mock('@proton/shared/lib/apps/desktopVersions');
8 const mockFetchDesktopVersion = jest.mocked(fetchDesktopVersion);
10 jest.mock('@proton/shared/lib/helpers/browser');
11 const mockIsWindows = jest.mocked(isWindows);
12 const mockIsMac = jest.mocked(isMac);
14 const originalConsoleWarn = console.warn;
15 const mockConsoleWarn = jest.fn();
17 describe('appPlatforms', () => {
18     beforeEach(() => {
19         jest.resetAllMocks();
21         // Some default values for mocks
22         mockIsWindows.mockReturnValue(false);
23         mockIsMac.mockReturnValue(false);
24     });
26     const checkOrder = async (order: DESKTOP_PLATFORMS[]) => {
27         await jest.isolateModulesAsync(async () => {
28             const { appPlatforms } = await import('./appPlatforms');
30             expect(appPlatforms.length === order.length);
31             appPlatforms.forEach(({ platform }, i) => {
32                 expect(platform).toBe(order[i]);
33             });
34         });
35     };
37     it('should not change order if no platform is preferred', async () => {
38         await checkOrder([DESKTOP_PLATFORMS.WINDOWS, DESKTOP_PLATFORMS.MACOS]);
39     });
41     it('should order by preferred platform', async () => {
42         mockIsMac.mockReturnValue(true);
44         await checkOrder([DESKTOP_PLATFORMS.MACOS, DESKTOP_PLATFORMS.WINDOWS]);
45     });
46 });
48 describe('fetchDesktopDownloads', () => {
49     beforeEach(() => {
50         jest.resetAllMocks();
52         console.warn = mockConsoleWarn;
54         // Default values
55         mockFetchDesktopVersion.mockResolvedValue({ url: 'url', version: 'version' });
56     });
58     afterEach(() => {
59         console.warn = originalConsoleWarn;
60     });
62     it('should return a map of platforms to url', async () => {
63         const result = await fetchDesktopDownloads();
65         appPlatforms.forEach(({ platform }) => {
66             expect(result).toHaveProperty(platform);
67         });
68     });
70     it('should return empty object on failure', async () => {
71         mockFetchDesktopVersion.mockRejectedValue(new Error('oh no'));
73         const result = await fetchDesktopDownloads();
75         expect(result).toStrictEqual({});
76         expect(mockConsoleWarn).toHaveBeenCalledTimes(appPlatforms.length);
77     });
79     it('should not include failed calls', async () => {
80         mockFetchDesktopVersion.mockRejectedValueOnce(new Error('oh no'));
82         const result = await fetchDesktopDownloads();
84         appPlatforms.forEach(({ platform }, index) => {
85             if (index === 0) {
86                 expect(result).not.toHaveProperty(platform);
87             } else {
88                 expect(result).toHaveProperty(platform);
89             }
90         });
92         expect(mockConsoleWarn).toHaveBeenCalledTimes(appPlatforms.length - 1);
93     });
94 });