|
| 1 | +import { JsonRpcEngine } from '@metamask/json-rpc-engine'; |
| 2 | +import { type GetCurrencyRateResult } from '@metamask/snaps-sdk'; |
| 3 | +import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; |
| 4 | + |
| 5 | +import type { GetCurrencyRateParameters } from './getCurrencyRate'; |
| 6 | +import { getCurrencyRateHandler } from './getCurrencyRate'; |
| 7 | + |
| 8 | +describe('snap_getCurrencyRate', () => { |
| 9 | + describe('getCurrencyRateHandler', () => { |
| 10 | + it('has the expected shape', () => { |
| 11 | + expect(getCurrencyRateHandler).toMatchObject({ |
| 12 | + methodNames: ['snap_getCurrencyRate'], |
| 13 | + implementation: expect.any(Function), |
| 14 | + hookNames: { |
| 15 | + getCurrencyRate: true, |
| 16 | + }, |
| 17 | + }); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('implementation', () => { |
| 22 | + it('returns the result from the `getCurrencyRate` hook', async () => { |
| 23 | + const { implementation } = getCurrencyRateHandler; |
| 24 | + |
| 25 | + const getCurrencyRate = jest.fn().mockReturnValue({ |
| 26 | + currency: 'usd', |
| 27 | + conversionRate: 1, |
| 28 | + conversionDate: 1, |
| 29 | + usdConversionRate: 1, |
| 30 | + }); |
| 31 | + |
| 32 | + const hooks = { |
| 33 | + getCurrencyRate, |
| 34 | + }; |
| 35 | + |
| 36 | + const engine = new JsonRpcEngine(); |
| 37 | + |
| 38 | + engine.push((request, response, next, end) => { |
| 39 | + const result = implementation( |
| 40 | + request as JsonRpcRequest<GetCurrencyRateParameters>, |
| 41 | + response as PendingJsonRpcResponse<GetCurrencyRateResult>, |
| 42 | + next, |
| 43 | + end, |
| 44 | + hooks, |
| 45 | + ); |
| 46 | + |
| 47 | + result?.catch(end); |
| 48 | + }); |
| 49 | + |
| 50 | + const response = await engine.handle({ |
| 51 | + jsonrpc: '2.0', |
| 52 | + id: 1, |
| 53 | + method: 'snap_getCurrencyRate', |
| 54 | + params: { |
| 55 | + currency: 'btc', |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(response).toStrictEqual({ |
| 60 | + jsonrpc: '2.0', |
| 61 | + id: 1, |
| 62 | + result: { |
| 63 | + currency: 'usd', |
| 64 | + conversionRate: 1, |
| 65 | + conversionDate: 1, |
| 66 | + usdConversionRate: 1, |
| 67 | + }, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns null if there is no rate available', async () => { |
| 72 | + const { implementation } = getCurrencyRateHandler; |
| 73 | + |
| 74 | + const getCurrencyRate = jest.fn().mockReturnValue(undefined); |
| 75 | + |
| 76 | + const hooks = { |
| 77 | + getCurrencyRate, |
| 78 | + }; |
| 79 | + |
| 80 | + const engine = new JsonRpcEngine(); |
| 81 | + |
| 82 | + engine.push((request, response, next, end) => { |
| 83 | + const result = implementation( |
| 84 | + request as JsonRpcRequest<GetCurrencyRateParameters>, |
| 85 | + response as PendingJsonRpcResponse<GetCurrencyRateResult>, |
| 86 | + next, |
| 87 | + end, |
| 88 | + hooks, |
| 89 | + ); |
| 90 | + |
| 91 | + result?.catch(end); |
| 92 | + }); |
| 93 | + |
| 94 | + const response = await engine.handle({ |
| 95 | + jsonrpc: '2.0', |
| 96 | + id: 1, |
| 97 | + method: 'snap_getCurrencyRate', |
| 98 | + params: { |
| 99 | + currency: 'btc', |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(response).toStrictEqual({ |
| 104 | + jsonrpc: '2.0', |
| 105 | + id: 1, |
| 106 | + result: null, |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('throws on invalid params', async () => { |
| 111 | + const { implementation } = getCurrencyRateHandler; |
| 112 | + |
| 113 | + const getCurrencyRate = jest.fn().mockReturnValue({ |
| 114 | + currency: 'usd', |
| 115 | + conversionRate: 1, |
| 116 | + conversionDate: 1, |
| 117 | + usdConversionRate: 1, |
| 118 | + }); |
| 119 | + |
| 120 | + const hooks = { |
| 121 | + getCurrencyRate, |
| 122 | + }; |
| 123 | + |
| 124 | + const engine = new JsonRpcEngine(); |
| 125 | + |
| 126 | + engine.push((request, response, next, end) => { |
| 127 | + const result = implementation( |
| 128 | + request as JsonRpcRequest<GetCurrencyRateParameters>, |
| 129 | + response as PendingJsonRpcResponse<GetCurrencyRateResult>, |
| 130 | + next, |
| 131 | + end, |
| 132 | + hooks, |
| 133 | + ); |
| 134 | + |
| 135 | + result?.catch(end); |
| 136 | + }); |
| 137 | + |
| 138 | + const response = await engine.handle({ |
| 139 | + jsonrpc: '2.0', |
| 140 | + id: 1, |
| 141 | + method: 'snap_getCurrencyRate', |
| 142 | + params: { |
| 143 | + currency: 'eth', |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + expect(response).toStrictEqual({ |
| 148 | + error: { |
| 149 | + code: -32602, |
| 150 | + message: |
| 151 | + 'Invalid params: At path: currency -- Expected the value to satisfy a union of `literal`, but received: "eth".', |
| 152 | + stack: expect.any(String), |
| 153 | + }, |
| 154 | + id: 1, |
| 155 | + jsonrpc: '2.0', |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments