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', () => {
21 // Some default values for mocks
22 mockIsWindows.mockReturnValue(false);
23 mockIsMac.mockReturnValue(false);
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]);
37 it('should not change order if no platform is preferred', async () => {
38 await checkOrder([DESKTOP_PLATFORMS.WINDOWS, DESKTOP_PLATFORMS.MACOS]);
41 it('should order by preferred platform', async () => {
42 mockIsMac.mockReturnValue(true);
44 await checkOrder([DESKTOP_PLATFORMS.MACOS, DESKTOP_PLATFORMS.WINDOWS]);
48 describe('fetchDesktopDownloads', () => {
52 console.warn = mockConsoleWarn;
55 mockFetchDesktopVersion.mockResolvedValue({ url: 'url', version: 'version' });
59 console.warn = originalConsoleWarn;
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);
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);
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) => {
86 expect(result).not.toHaveProperty(platform);
88 expect(result).toHaveProperty(platform);
92 expect(mockConsoleWarn).toHaveBeenCalledTimes(appPlatforms.length - 1);