From 216b9bbf170e476bcc778a72065ce377d7c8b7ab Mon Sep 17 00:00:00 2001 From: Tobiz <232918735+DevTobis@users.noreply.github.com> Date: Mon, 31 Aug 2026 10:24:45 +0100 Subject: [PATCH] test: add horizonAccount.ts coverage Cover fetchBalances resolving to null on a non-ok Horizon response, and horizonUrlFor's mainnet/testnet branch. --- .../src/__tests__/lib/horizonAccount.test.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 apps/web/src/__tests__/lib/horizonAccount.test.ts diff --git a/apps/web/src/__tests__/lib/horizonAccount.test.ts b/apps/web/src/__tests__/lib/horizonAccount.test.ts new file mode 100644 index 00000000..7aaeb679 --- /dev/null +++ b/apps/web/src/__tests__/lib/horizonAccount.test.ts @@ -0,0 +1,53 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; + +import { fetchBalances, horizonUrlFor } from "../../lib/horizonAccount"; + +const PUBLIC_KEY = "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5"; + +describe("horizonUrlFor", () => { + it("returns the Horizon mainnet URL for \"mainnet\"", () => { + expect(horizonUrlFor("mainnet")).toBe("https://horizon.stellar.org"); + }); + + it("returns the Horizon testnet URL for any other value", () => { + expect(horizonUrlFor("testnet")).toBe( + "https://horizon-testnet.stellar.org" + ); + expect(horizonUrlFor("futurenet")).toBe( + "https://horizon-testnet.stellar.org" + ); + }); +}); + +describe("fetchBalances", () => { + beforeEach(() => { + vi.stubGlobal("fetch", vi.fn()); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("resolves to null when the Horizon response isn't ok", async () => { + vi.mocked(fetch).mockResolvedValue({ ok: false } as Response); + + await expect(fetchBalances(PUBLIC_KEY, "testnet")).resolves.toBeNull(); + }); + + it("resolves to the parsed balances on a successful response", async () => { + const balances = [ + { asset_type: "native", balance: "100.0000000" }, + ]; + vi.mocked(fetch).mockResolvedValue({ + ok: true, + json: async () => ({ balances }), + } as Response); + + await expect(fetchBalances(PUBLIC_KEY, "mainnet")).resolves.toEqual({ + balances, + }); + expect(fetch).toHaveBeenCalledWith( + `https://horizon.stellar.org/accounts/${PUBLIC_KEY}` + ); + }); +});