|
| 1 | +import type RNFSType from 'react-native-fs'; |
| 2 | +import type { WidgetTokenPayload } from './buildWidgetPayload'; |
| 3 | +import type { syncWidgetLogos as SyncWidgetLogos } from './syncWidgetLogos'; |
| 4 | + |
| 5 | +jest.mock('react-native-fs', () => ({ |
| 6 | + exists: jest.fn(), |
| 7 | + downloadFile: jest.fn(), |
| 8 | + unlink: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('../NativeModules', () => ({ |
| 12 | + WidgetBridge: { getLogosDirectoryPath: jest.fn() }, |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../../util/Logger', () => ({ error: jest.fn() })); |
| 16 | + |
| 17 | +const LOGOS_DIR = '/group/TokenLogos'; |
| 18 | + |
| 19 | +const download = (statusCode: number) => |
| 20 | + ({ promise: Promise.resolve({ statusCode }) }) as ReturnType< |
| 21 | + typeof RNFSType.downloadFile |
| 22 | + >; |
| 23 | + |
| 24 | +const makePayload = ( |
| 25 | + tokens: Partial<WidgetTokenPayload['tokens'][number]>[], |
| 26 | +): WidgetTokenPayload => ({ |
| 27 | + tokens: tokens.map((t) => ({ |
| 28 | + symbol: t.symbol ?? 'TKN', |
| 29 | + priceFormatted: t.priceFormatted ?? '$1.00', |
| 30 | + logoUrl: t.logoUrl ?? 'https://logos/TKN.png', |
| 31 | + ...t, |
| 32 | + })), |
| 33 | +}); |
| 34 | + |
| 35 | +// The SUT memoizes the logos-dir path at module scope, so re-require it fresh |
| 36 | +// per test to keep cases independent. |
| 37 | +let syncWidgetLogos: typeof SyncWidgetLogos; |
| 38 | +let mockExists: jest.MockedFunction<typeof RNFSType.exists>; |
| 39 | +let mockDownload: jest.MockedFunction<typeof RNFSType.downloadFile>; |
| 40 | +let mockUnlink: jest.MockedFunction<typeof RNFSType.unlink>; |
| 41 | +let mockGetDir: jest.MockedFunction<() => Promise<string>>; |
| 42 | + |
| 43 | +describe('syncWidgetLogos', () => { |
| 44 | + beforeEach(() => { |
| 45 | + jest.resetModules(); |
| 46 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 47 | + const RNFS = require('react-native-fs'); |
| 48 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 49 | + const { WidgetBridge } = require('../NativeModules'); |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 51 | + ({ syncWidgetLogos } = require('./syncWidgetLogos')); |
| 52 | + |
| 53 | + mockExists = RNFS.exists; |
| 54 | + mockDownload = RNFS.downloadFile; |
| 55 | + mockUnlink = RNFS.unlink; |
| 56 | + mockGetDir = WidgetBridge.getLogosDirectoryPath; |
| 57 | + |
| 58 | + mockGetDir.mockResolvedValue(LOGOS_DIR); |
| 59 | + mockExists.mockResolvedValue(false); |
| 60 | + mockDownload.mockReturnValue(download(200)); |
| 61 | + mockUnlink.mockResolvedValue(undefined); |
| 62 | + }); |
| 63 | + |
| 64 | + it('downloads a logo and rewrites logoUrl to logoFile', async () => { |
| 65 | + const { tokens } = await syncWidgetLogos( |
| 66 | + makePayload([{ symbol: 'ETH', logoUrl: 'https://logos/eth.png' }]), |
| 67 | + ); |
| 68 | + |
| 69 | + expect(mockDownload).toHaveBeenCalledWith({ |
| 70 | + fromUrl: 'https://logos/eth.png', |
| 71 | + toFile: `${LOGOS_DIR}/ETH.png`, |
| 72 | + }); |
| 73 | + expect(tokens[0]).toMatchObject({ symbol: 'ETH', logoFile: 'ETH.png' }); |
| 74 | + expect(tokens[0]).not.toHaveProperty('logoUrl'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('skips the download when the file already exists', async () => { |
| 78 | + mockExists.mockResolvedValue(true); |
| 79 | + |
| 80 | + const { tokens } = await syncWidgetLogos(makePayload([{ symbol: 'ETH' }])); |
| 81 | + |
| 82 | + expect(mockDownload).not.toHaveBeenCalled(); |
| 83 | + expect(tokens[0].logoFile).toBe('ETH.png'); |
| 84 | + }); |
| 85 | + |
| 86 | + it.each([ |
| 87 | + ['empty', ''], |
| 88 | + ['non-http', 'data:image/png;base64,abc'], |
| 89 | + ['svg', 'https://logos/eth.svg'], |
| 90 | + ['svg with query', 'https://logos/eth.svg?v=2'], |
| 91 | + ])('does not download a %s logo url', async (_label, logoUrl) => { |
| 92 | + const { tokens } = await syncWidgetLogos( |
| 93 | + makePayload([{ symbol: 'ETH', logoUrl }]), |
| 94 | + ); |
| 95 | + |
| 96 | + expect(mockDownload).not.toHaveBeenCalled(); |
| 97 | + expect(tokens[0]).not.toHaveProperty('logoFile'); |
| 98 | + expect(tokens[0]).not.toHaveProperty('logoUrl'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('drops logoFile and cleans up on a non-2xx response', async () => { |
| 102 | + mockDownload.mockReturnValue(download(404)); |
| 103 | + |
| 104 | + const { tokens } = await syncWidgetLogos(makePayload([{ symbol: 'ETH' }])); |
| 105 | + |
| 106 | + expect(tokens[0]).not.toHaveProperty('logoFile'); |
| 107 | + expect(mockUnlink).toHaveBeenCalledWith(`${LOGOS_DIR}/ETH.png`); |
| 108 | + }); |
| 109 | + |
| 110 | + it('treats a download error as a missing logo', async () => { |
| 111 | + mockDownload.mockReturnValue({ |
| 112 | + promise: Promise.reject(new Error('network')), |
| 113 | + } as ReturnType<typeof RNFSType.downloadFile>); |
| 114 | + |
| 115 | + const { tokens } = await syncWidgetLogos(makePayload([{ symbol: 'ETH' }])); |
| 116 | + |
| 117 | + expect(tokens[0]).not.toHaveProperty('logoFile'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('sanitizes the symbol into a safe filename', async () => { |
| 121 | + await syncWidgetLogos(makePayload([{ symbol: 'a/b c' }])); |
| 122 | + |
| 123 | + expect(mockDownload).toHaveBeenCalledWith( |
| 124 | + expect.objectContaining({ toFile: `${LOGOS_DIR}/a_b_c.png` }), |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns the payload without logos when the container is unavailable', async () => { |
| 129 | + mockGetDir.mockRejectedValue(new Error('app_group_unavailable')); |
| 130 | + |
| 131 | + const { tokens } = await syncWidgetLogos( |
| 132 | + makePayload([{ symbol: 'ETH' }, { symbol: 'USDC' }]), |
| 133 | + ); |
| 134 | + |
| 135 | + expect(mockDownload).not.toHaveBeenCalled(); |
| 136 | + expect(tokens.map((t) => t.symbol)).toEqual(['ETH', 'USDC']); |
| 137 | + expect(tokens[0]).not.toHaveProperty('logoUrl'); |
| 138 | + expect(tokens[0]).not.toHaveProperty('logoFile'); |
| 139 | + }); |
| 140 | +}); |
0 commit comments