|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { MoolahApiClient } from "../api/client"; |
| 3 | + |
| 4 | +function makeSuccessResponse<T>(data: T): Response { |
| 5 | + return { |
| 6 | + ok: true, |
| 7 | + status: 200, |
| 8 | + statusText: "OK", |
| 9 | + json: async () => ({ code: "000000000", msg: "ok", data }), |
| 10 | + } as unknown as Response; |
| 11 | +} |
| 12 | + |
| 13 | +describe("MoolahApiClient list query serialization", () => { |
| 14 | + let fetchMock: ReturnType<typeof vi.fn>; |
| 15 | + let client: MoolahApiClient; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + fetchMock = vi.fn().mockResolvedValue(makeSuccessResponse({ total: 0, list: [] })); |
| 19 | + client = new MoolahApiClient({ |
| 20 | + baseUrl: "https://api.lista.org", |
| 21 | + fetch: fetchMock as unknown as typeof fetch, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should serialize vault filters using bracket array params", async () => { |
| 26 | + await client.getVaultList({ |
| 27 | + page: 1, |
| 28 | + pageSize: 10, |
| 29 | + sort: "depositsUsd", |
| 30 | + order: "desc", |
| 31 | + zone: 0, |
| 32 | + keyword: "123", |
| 33 | + chain: ["bsc", "ethereum"], |
| 34 | + assets: ["USDT", "BTCB"], |
| 35 | + curators: ["Pangolins", "MEV Capital"], |
| 36 | + }); |
| 37 | + |
| 38 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 39 | + const url = new URL(String(fetchMock.mock.calls[0][0])); |
| 40 | + |
| 41 | + expect(url.pathname).toBe("/api/moolah/vault/list"); |
| 42 | + expect(url.searchParams.get("chain")).toBe("bsc,ethereum"); |
| 43 | + expect(url.searchParams.getAll("assets[]")).toEqual(["USDT", "BTCB"]); |
| 44 | + expect(url.searchParams.getAll("curators[]")).toEqual([ |
| 45 | + "Pangolins", |
| 46 | + "MEV Capital", |
| 47 | + ]); |
| 48 | + expect(url.searchParams.get("assets")).toBeNull(); |
| 49 | + expect(url.searchParams.get("curators")).toBeNull(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should serialize market filters with smartLendingChecked", async () => { |
| 53 | + await client.getMarketList({ |
| 54 | + page: 1, |
| 55 | + pageSize: 10, |
| 56 | + sort: "liquidity", |
| 57 | + order: "desc", |
| 58 | + zone: 3, |
| 59 | + keyword: "", |
| 60 | + chain: ["bsc", "ethereum"], |
| 61 | + loans: ["BTCB", "USDT"], |
| 62 | + collaterals: ["BTCB", "SolvBTC"], |
| 63 | + smartLendingChecked: true, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 67 | + const url = new URL(String(fetchMock.mock.calls[0][0])); |
| 68 | + |
| 69 | + expect(url.pathname).toBe("/api/moolah/borrow/markets"); |
| 70 | + expect(url.searchParams.get("chain")).toBe("bsc,ethereum"); |
| 71 | + expect(url.searchParams.getAll("loans[]")).toEqual(["BTCB", "USDT"]); |
| 72 | + expect(url.searchParams.getAll("collaterals[]")).toEqual([ |
| 73 | + "BTCB", |
| 74 | + "SolvBTC", |
| 75 | + ]); |
| 76 | + expect(url.searchParams.get("smartLendingChecked")).toBe("true"); |
| 77 | + expect(url.searchParams.get("loans")).toBeNull(); |
| 78 | + expect(url.searchParams.get("collaterals")).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should serialize holdings params for vault and market types", async () => { |
| 82 | + await client.getHoldings({ |
| 83 | + userAddress: "0x05e3a7a66945ca9af73f66660f22ffb36332fa54", |
| 84 | + type: "vault", |
| 85 | + }); |
| 86 | + await client.getHoldings({ |
| 87 | + userAddress: "0x05e3a7a66945ca9af73f66660f22ffb36332fa54", |
| 88 | + type: "market", |
| 89 | + }); |
| 90 | + |
| 91 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 92 | + |
| 93 | + const vaultUrl = new URL(String(fetchMock.mock.calls[0][0])); |
| 94 | + expect(vaultUrl.pathname).toBe("/api/moolah/one/holding"); |
| 95 | + expect(vaultUrl.searchParams.get("userAddress")).toBe( |
| 96 | + "0x05e3a7a66945ca9af73f66660f22ffb36332fa54", |
| 97 | + ); |
| 98 | + expect(vaultUrl.searchParams.get("type")).toBe("vault"); |
| 99 | + |
| 100 | + const marketUrl = new URL(String(fetchMock.mock.calls[1][0])); |
| 101 | + expect(marketUrl.pathname).toBe("/api/moolah/one/holding"); |
| 102 | + expect(marketUrl.searchParams.get("userAddress")).toBe( |
| 103 | + "0x05e3a7a66945ca9af73f66660f22ffb36332fa54", |
| 104 | + ); |
| 105 | + expect(marketUrl.searchParams.get("type")).toBe("market"); |
| 106 | + }); |
| 107 | +}); |
0 commit comments