|
| 1 | +import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; |
| 2 | +import { klona } from 'klona'; |
| 3 | + |
| 4 | +import type { |
| 5 | + GetCallsStatusParams, |
| 6 | + GetCallsStatusResult, |
| 7 | + GetTransactionReceiptsByBatchIdHook, |
| 8 | +} from './wallet-get-calls-status'; |
| 9 | +import { walletGetCallsStatus } from './wallet-get-calls-status'; |
| 10 | + |
| 11 | +const ID_MOCK = '1234-5678'; |
| 12 | + |
| 13 | +const RECEIPT_MOCK = { |
| 14 | + logs: [ |
| 15 | + { |
| 16 | + address: '0x123abc123abc123abc123abc123abc123abc123a', |
| 17 | + data: '0x123abc', |
| 18 | + topics: ['0x123abc'], |
| 19 | + }, |
| 20 | + ], |
| 21 | + status: '0x1', |
| 22 | + chainId: '0x1', |
| 23 | + blockHash: '0x123abc', |
| 24 | + blockNumber: '0x1', |
| 25 | + gasUsed: '0x1', |
| 26 | + transactionHash: '0x123abc', |
| 27 | +}; |
| 28 | + |
| 29 | +const REQUEST_MOCK = { |
| 30 | + params: [ID_MOCK], |
| 31 | +} as unknown as JsonRpcRequest<GetCallsStatusParams>; |
| 32 | + |
| 33 | +describe('wallet_getCallsStatus', () => { |
| 34 | + let request: JsonRpcRequest<GetCallsStatusParams>; |
| 35 | + let params: GetCallsStatusParams; |
| 36 | + let response: PendingJsonRpcResponse<GetCallsStatusResult>; |
| 37 | + let getTransactionReceiptsByBatchIdMock: jest.MockedFunction<GetTransactionReceiptsByBatchIdHook>; |
| 38 | + |
| 39 | + async function callMethod() { |
| 40 | + return walletGetCallsStatus(request, response, { |
| 41 | + getTransactionReceiptsByBatchId: getTransactionReceiptsByBatchIdMock, |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + jest.resetAllMocks(); |
| 47 | + |
| 48 | + request = klona(REQUEST_MOCK); |
| 49 | + params = request.params as GetCallsStatusParams; |
| 50 | + response = {} as PendingJsonRpcResponse<GetCallsStatusResult>; |
| 51 | + |
| 52 | + getTransactionReceiptsByBatchIdMock = jest |
| 53 | + .fn() |
| 54 | + .mockResolvedValue([RECEIPT_MOCK, RECEIPT_MOCK]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('calls hook', async () => { |
| 58 | + await callMethod(); |
| 59 | + expect(getTransactionReceiptsByBatchIdMock).toHaveBeenCalledWith( |
| 60 | + params[0], |
| 61 | + request, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns confirmed status if all receipts available', async () => { |
| 66 | + await callMethod(); |
| 67 | + expect(response.result?.status).toBe('CONFIRMED'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns pending status if missing receipts', async () => { |
| 71 | + getTransactionReceiptsByBatchIdMock = jest |
| 72 | + .fn() |
| 73 | + .mockResolvedValue([RECEIPT_MOCK, undefined]); |
| 74 | + |
| 75 | + await callMethod(); |
| 76 | + expect(response.result?.status).toBe('PENDING'); |
| 77 | + expect(response.result?.receipts).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns receipts', async () => { |
| 81 | + await callMethod(); |
| 82 | + |
| 83 | + expect(response.result?.receipts).toStrictEqual([ |
| 84 | + RECEIPT_MOCK, |
| 85 | + RECEIPT_MOCK, |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns null if no receipts', async () => { |
| 90 | + getTransactionReceiptsByBatchIdMock = jest.fn().mockResolvedValue([]); |
| 91 | + |
| 92 | + await callMethod(); |
| 93 | + expect(response.result).toBeNull(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('throws if no hook', async () => { |
| 97 | + await expect( |
| 98 | + walletGetCallsStatus(request, response, {}), |
| 99 | + ).rejects.toMatchInlineSnapshot(`[Error: Method not supported.]`); |
| 100 | + }); |
| 101 | + |
| 102 | + it('throws if no params', async () => { |
| 103 | + request.params = undefined; |
| 104 | + |
| 105 | + await expect(callMethod()).rejects.toMatchInlineSnapshot(` |
| 106 | + [Error: Invalid params |
| 107 | +
|
| 108 | + Expected an array, but received: undefined] |
| 109 | + `); |
| 110 | + }); |
| 111 | + |
| 112 | + it('throws if wrong type', async () => { |
| 113 | + params[0] = 123 as never; |
| 114 | + |
| 115 | + await expect(callMethod()).rejects.toMatchInlineSnapshot(` |
| 116 | + [Error: Invalid params |
| 117 | +
|
| 118 | + 0 - Expected a string, but received: 123] |
| 119 | + `); |
| 120 | + }); |
| 121 | + |
| 122 | + it('throws if empty', async () => { |
| 123 | + params[0] = ''; |
| 124 | + |
| 125 | + await expect(callMethod()).rejects.toMatchInlineSnapshot(` |
| 126 | + [Error: Invalid params |
| 127 | +
|
| 128 | + 0 - Expected a nonempty string but received an empty one] |
| 129 | + `); |
| 130 | + }); |
| 131 | + |
| 132 | + it('removes excess properties from receipts', async () => { |
| 133 | + getTransactionReceiptsByBatchIdMock.mockResolvedValue([ |
| 134 | + { |
| 135 | + ...RECEIPT_MOCK, |
| 136 | + extra: 'value1', |
| 137 | + logs: [{ ...RECEIPT_MOCK.logs[0], extra2: 'value2' }], |
| 138 | + } as never, |
| 139 | + ]); |
| 140 | + |
| 141 | + await callMethod(); |
| 142 | + |
| 143 | + expect(response.result?.receipts).toStrictEqual([RECEIPT_MOCK]); |
| 144 | + }); |
| 145 | +}); |
0 commit comments