|
1 | 1 | import { handleResponse } from './handleResponse'; |
2 | 2 | import { WebBasedWalletCommunicator } from './webBased/Communicator'; |
3 | 3 | import { MWP_RESPONSE_PATH } from ':core/constants'; |
4 | | -import { Wallet } from ':core/wallet'; |
5 | 4 |
|
6 | | -jest.mock('./webBased/Communicator'); |
7 | | -jest.mock('expo-web-browser', () => ({ |
8 | | - openBrowserAsync: jest.fn(), |
9 | | - WebBrowserPresentationStyle: { |
10 | | - FORM_SHEET: 'FORM_SHEET', |
| 5 | +jest.mock('./webBased/Communicator', () => ({ |
| 6 | + WebBasedWalletCommunicator: { |
| 7 | + handleResponse: jest.fn(), |
11 | 8 | }, |
12 | | - dismissBrowser: jest.fn(), |
13 | 9 | })); |
14 | 10 |
|
15 | 11 | describe('handleResponse', () => { |
16 | | - const mockWebBasedWallet = { type: 'webBased', scheme: 'https://example.com' } as Wallet; |
17 | | - const mockNativeWallet = { type: 'native' } as Wallet; |
18 | | - const mockOtherWallet = { type: 'other' as any } as Wallet; |
19 | | - |
20 | 12 | beforeEach(() => { |
21 | 13 | jest.clearAllMocks(); |
22 | 14 | }); |
23 | 15 |
|
24 | | - test('returns false for non-MWP response URLs', () => { |
25 | | - const url = 'https://example.com/some-path'; |
26 | | - expect(handleResponse(url, mockWebBasedWallet)).toBe(false); |
| 16 | + it('should return false if the pathname does not include MWP_RESPONSE_PATH', () => { |
| 17 | + const responseUrl = 'https://example.com/some-other-path'; |
| 18 | + const result = handleResponse(responseUrl); |
| 19 | + expect(result).toBe(false); |
| 20 | + expect(WebBasedWalletCommunicator.handleResponse).not.toHaveBeenCalled(); |
27 | 21 | }); |
28 | 22 |
|
29 | | - test('handles web-based wallet response correctly', () => { |
30 | | - const url = `https://example.com/${MWP_RESPONSE_PATH}/some-params`; |
31 | | - const mockHandleResponse = jest.fn().mockReturnValue(true); |
32 | | - (WebBasedWalletCommunicator.getInstance as jest.Mock).mockReturnValue({ |
33 | | - handleResponse: mockHandleResponse, |
34 | | - }); |
| 23 | + it('should return true if WebBasedWalletCommunicator handles the response successfully', () => { |
| 24 | + const responseUrl = `https://example.com/${MWP_RESPONSE_PATH}/some-params`; |
| 25 | + (WebBasedWalletCommunicator.handleResponse as jest.Mock).mockReturnValue(true); |
35 | 26 |
|
36 | | - const result = handleResponse(url, mockWebBasedWallet); |
| 27 | + const result = handleResponse(responseUrl); |
37 | 28 |
|
38 | | - expect(WebBasedWalletCommunicator.getInstance).toHaveBeenCalledWith(mockWebBasedWallet.scheme); |
39 | | - expect(mockHandleResponse).toHaveBeenCalledWith(url); |
40 | 29 | expect(result).toBe(true); |
| 30 | + expect(WebBasedWalletCommunicator.handleResponse).toHaveBeenCalledWith(responseUrl); |
41 | 31 | }); |
42 | 32 |
|
43 | | - test('throws error for native wallet', () => { |
44 | | - const url = `https://example.com/${MWP_RESPONSE_PATH}/some-params`; |
45 | | - expect(() => handleResponse(url, mockNativeWallet)).toThrow('Native wallet not supported yet'); |
46 | | - }); |
| 33 | + it('should return false if WebBasedWalletCommunicator does not handle the response', () => { |
| 34 | + const responseUrl = `https://example.com/${MWP_RESPONSE_PATH}/some-params`; |
| 35 | + (WebBasedWalletCommunicator.handleResponse as jest.Mock).mockReturnValue(false); |
| 36 | + |
| 37 | + const result = handleResponse(responseUrl); |
47 | 38 |
|
48 | | - test('returns false for unsupported wallet types', () => { |
49 | | - const url = `https://example.com/${MWP_RESPONSE_PATH}/some-params`; |
50 | | - expect(handleResponse(url, mockOtherWallet)).toBe(false); |
| 39 | + expect(result).toBe(false); |
| 40 | + expect(WebBasedWalletCommunicator.handleResponse).toHaveBeenCalledWith(responseUrl); |
51 | 41 | }); |
52 | 42 |
|
53 | | - test('WebBasedWalletCommunicator.handleResponse returns false', () => { |
54 | | - const url = `https://example.com/${MWP_RESPONSE_PATH}/some-params`; |
55 | | - const mockHandleResponse = jest.fn().mockReturnValue(false); |
56 | | - (WebBasedWalletCommunicator.getInstance as jest.Mock).mockReturnValue({ |
57 | | - handleResponse: mockHandleResponse, |
| 43 | + it('should handle different URL formats correctly', () => { |
| 44 | + const responseUrls = [ |
| 45 | + `https://example.com/${MWP_RESPONSE_PATH}`, |
| 46 | + `https://example.com/${MWP_RESPONSE_PATH}/`, |
| 47 | + `https://example.com/${MWP_RESPONSE_PATH}?param=value`, |
| 48 | + `https://example.com/${MWP_RESPONSE_PATH}/?param=value`, |
| 49 | + ]; |
| 50 | + |
| 51 | + responseUrls.forEach((url) => { |
| 52 | + (WebBasedWalletCommunicator.handleResponse as jest.Mock).mockReturnValue(true); |
| 53 | + expect(handleResponse(url)).toBe(true); |
| 54 | + expect(WebBasedWalletCommunicator.handleResponse).toHaveBeenCalledWith(url); |
58 | 55 | }); |
| 56 | + }); |
59 | 57 |
|
60 | | - const result = handleResponse(url, mockWebBasedWallet); |
61 | | - |
62 | | - expect(mockHandleResponse).toHaveBeenCalledWith(url); |
63 | | - expect(result).toBe(false); |
| 58 | + it('should throw an error for invalid URLs', () => { |
| 59 | + const invalidUrl = 'not-a-valid-url'; |
| 60 | + expect(() => handleResponse(invalidUrl)).toThrow('Invalid URL'); |
64 | 61 | }); |
65 | 62 | }); |
0 commit comments