|
| 1 | +import * as WebBrowser from 'expo-web-browser'; |
| 2 | + |
| 3 | +import { postRequestToWallet } from './postRequestToWallet'; |
| 4 | +import { decodeResponseURLParams, encodeRequestURLParams } from './utils/encoding'; |
| 5 | +import { RPCRequestMessage, RPCResponseMessage } from ':core/message'; |
| 6 | +import { Wallet } from ':core/wallet'; |
| 7 | + |
| 8 | +jest.mock('expo-web-browser', () => ({ |
| 9 | + openAuthSessionAsync: jest.fn(), |
| 10 | + dismissBrowser: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('./utils/encoding', () => ({ |
| 14 | + ...jest.requireActual('./utils/encoding'), |
| 15 | + decodeResponseURLParams: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockAppCustomScheme = 'myapp://'; |
| 19 | +const mockWalletScheme = 'https://example.com'; |
| 20 | + |
| 21 | +describe('postRequestToWallet', () => { |
| 22 | + const mockRequest: RPCRequestMessage = { |
| 23 | + id: '1-2-3-4-5', |
| 24 | + sdkVersion: '1.0.0', |
| 25 | + content: { |
| 26 | + handshake: { |
| 27 | + method: 'eth_requestAccounts', |
| 28 | + params: { appName: 'test' }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + callbackUrl: 'https://example.com', |
| 32 | + sender: 'Sender', |
| 33 | + timestamp: new Date(), |
| 34 | + }; |
| 35 | + const mockResponse: RPCResponseMessage = { |
| 36 | + id: '2-2-3-4-5', |
| 37 | + requestId: '1-2-3-4-5', |
| 38 | + content: { |
| 39 | + encrypted: { |
| 40 | + iv: new Uint8Array([1]), |
| 41 | + cipherText: new Uint8Array([2]), |
| 42 | + }, |
| 43 | + }, |
| 44 | + sender: 'some-sender', |
| 45 | + timestamp: new Date(), |
| 46 | + }; |
| 47 | + let requestUrl: URL; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + requestUrl = new URL(mockWalletScheme); |
| 51 | + requestUrl.search = encodeRequestURLParams(mockRequest); |
| 52 | + jest.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should successfully post request to a web-based wallet', async () => { |
| 56 | + const webWallet: Wallet = { type: 'web', scheme: mockWalletScheme } as Wallet; |
| 57 | + (WebBrowser.openAuthSessionAsync as jest.Mock).mockResolvedValue({ |
| 58 | + type: 'success', |
| 59 | + url: 'https://example.com/response', |
| 60 | + }); |
| 61 | + (decodeResponseURLParams as jest.Mock).mockResolvedValue(mockResponse); |
| 62 | + |
| 63 | + const result = await postRequestToWallet(mockRequest, mockAppCustomScheme, webWallet); |
| 64 | + |
| 65 | + expect(WebBrowser.openAuthSessionAsync).toHaveBeenCalledWith( |
| 66 | + requestUrl.toString(), |
| 67 | + mockAppCustomScheme, |
| 68 | + { |
| 69 | + preferEphemeralSession: false, |
| 70 | + } |
| 71 | + ); |
| 72 | + expect(result).toEqual(mockResponse); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw an error if the user cancels the request', async () => { |
| 76 | + const webWallet: Wallet = { type: 'web', scheme: mockWalletScheme } as Wallet; |
| 77 | + (WebBrowser.openAuthSessionAsync as jest.Mock).mockResolvedValue({ |
| 78 | + type: 'cancel', |
| 79 | + }); |
| 80 | + |
| 81 | + await expect(postRequestToWallet(mockRequest, mockAppCustomScheme, webWallet)).rejects.toThrow( |
| 82 | + 'User rejected the request' |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should throw an error for native wallet type', async () => { |
| 87 | + const nativeWallet: Wallet = { type: 'native', scheme: mockWalletScheme } as Wallet; |
| 88 | + |
| 89 | + await expect( |
| 90 | + postRequestToWallet(mockRequest, mockAppCustomScheme, nativeWallet) |
| 91 | + ).rejects.toThrow('Native wallet not supported yet'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should throw an error for unsupported wallet type', async () => { |
| 95 | + const unsupportedWallet: Wallet = { |
| 96 | + type: 'unsupported' as any, |
| 97 | + scheme: mockWalletScheme, |
| 98 | + } as Wallet; |
| 99 | + |
| 100 | + await expect( |
| 101 | + postRequestToWallet(mockRequest, mockAppCustomScheme, unsupportedWallet) |
| 102 | + ).rejects.toThrow('Unsupported wallet type'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should pass through any errors from WebBrowser', async () => { |
| 106 | + const webWallet: Wallet = { type: 'web', scheme: mockWalletScheme } as Wallet; |
| 107 | + const mockError = new Error('Communication error'); |
| 108 | + (WebBrowser.openAuthSessionAsync as jest.Mock).mockRejectedValue(mockError); |
| 109 | + |
| 110 | + await expect(postRequestToWallet(mockRequest, mockAppCustomScheme, webWallet)).rejects.toThrow( |
| 111 | + 'User rejected the request' |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments