|
| 1 | +import { Platform } from 'react-native'; |
| 2 | + |
| 3 | +import { Directory } from 'expo-file-system'; |
| 4 | +import * as Sharing from 'expo-sharing'; |
| 5 | + |
| 6 | +import { type AllLabelsForAccount } from '@suite-common/suite-sync'; |
| 7 | + |
| 8 | +import { exportBip329 } from './exportBip329'; |
| 9 | + |
| 10 | +const mockDirectoryWrite = jest.fn(); |
| 11 | +const mockCreateFile = jest.fn(() => ({ write: mockDirectoryWrite })); |
| 12 | + |
| 13 | +jest.mock('expo-file-system', () => ({ |
| 14 | + File: jest.fn().mockImplementation(() => ({ |
| 15 | + exists: false, |
| 16 | + create: jest.fn(), |
| 17 | + write: jest.fn(), |
| 18 | + uri: 'file:///cache/account_labels.jsonl', |
| 19 | + })), |
| 20 | + Paths: { cache: 'file:///cache' }, |
| 21 | + Directory: { |
| 22 | + pickDirectoryAsync: jest.fn(), |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('expo-sharing', () => ({ |
| 27 | + shareAsync: jest.fn(), |
| 28 | +})); |
| 29 | + |
| 30 | +const mockPickDirectoryAsync = jest.mocked(Directory.pickDirectoryAsync); |
| 31 | +const mockShareAsync = jest.mocked(Sharing.shareAsync); |
| 32 | + |
| 33 | +const originalPlatformOS = Platform.OS; |
| 34 | +const setPlatformOS = (os: typeof Platform.OS) => { |
| 35 | + Platform.OS = os; |
| 36 | +}; |
| 37 | + |
| 38 | +const LABELS: AllLabelsForAccount = { accountLabel: null, addressLabels: [], outputLabels: [] }; |
| 39 | + |
| 40 | +describe('exportBip329', () => { |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + afterAll(() => { |
| 46 | + setPlatformOS(originalPlatformOS); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('on android', () => { |
| 50 | + beforeEach(() => { |
| 51 | + setPlatformOS('android'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('writes the file into the picked directory and returns success', async () => { |
| 55 | + mockPickDirectoryAsync.mockResolvedValue({ createFile: mockCreateFile } as never); |
| 56 | + |
| 57 | + const result = await exportBip329(null, LABELS); |
| 58 | + |
| 59 | + expect(result).toEqual({ success: true }); |
| 60 | + expect(mockCreateFile).toHaveBeenCalledTimes(1); |
| 61 | + expect(mockDirectoryWrite).toHaveBeenCalledTimes(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns the cancelled reason when the user dismisses the directory picker', async () => { |
| 65 | + mockPickDirectoryAsync.mockRejectedValue( |
| 66 | + Object.assign(new Error('The file picker was cancelled by the user'), { |
| 67 | + code: 'ERR_PICKER_CANCELLED', |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + const result = await exportBip329(null, LABELS); |
| 72 | + |
| 73 | + expect(result).toEqual({ success: false, reason: 'cancelled' }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns the exportFailed reason for any other picker error', async () => { |
| 77 | + mockPickDirectoryAsync.mockRejectedValue(new Error('boom')); |
| 78 | + |
| 79 | + const result = await exportBip329(null, LABELS); |
| 80 | + |
| 81 | + expect(result).toEqual({ success: false, reason: 'exportFailed' }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('on ios', () => { |
| 86 | + beforeEach(() => { |
| 87 | + setPlatformOS('ios'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('shares the cached file and returns success', async () => { |
| 91 | + mockShareAsync.mockResolvedValue(undefined); |
| 92 | + |
| 93 | + const result = await exportBip329(null, LABELS); |
| 94 | + |
| 95 | + expect(result).toEqual({ success: true }); |
| 96 | + expect(mockShareAsync).toHaveBeenCalledWith( |
| 97 | + 'file:///cache/account_labels.jsonl', |
| 98 | + expect.objectContaining({ UTI: 'public.jsonl' }), |
| 99 | + ); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('on an unsupported platform', () => { |
| 104 | + beforeEach(() => { |
| 105 | + setPlatformOS('web'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('returns the fileSavingNotSupported reason', async () => { |
| 109 | + const result = await exportBip329(null, LABELS); |
| 110 | + |
| 111 | + expect(result).toEqual({ success: false, reason: 'fileSavingNotSupported' }); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments