From 04f509d5ebcbede0b51b4d1cdd5d3a4233c022b7 Mon Sep 17 00:00:00 2001 From: Nwokedi Uche Date: Sun, 30 Aug 2026 12:20:48 +0100 Subject: [PATCH] test(NetworkBanner): fix undefined renderWithNetwork helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to #523 While verifying #523 (network switching) was already fixed on main, found that 3 of NetworkBanner.test.tsx's 11 tests were calling a renderWithNetwork helper that was never defined anywhere in the file, failing with ReferenceError: renderWithNetwork is not defined. Adds the missing helper: mocks useSorokit to return a synthetic network built from just a name (rpcUrl/passphrase/horizonUrl are irrelevant to NetworkBanner, which only ever reads network.name), then renders the given element (or a bare by default). Also fixes the third broken test, which asserted client.network.getNetwork had been called — NetworkBanner never touches a client at all, it only reads network from context directly, so that assertion could never have passed against the real component. Replaced with an assertion of what the component actually does (renders nothing when active="network", exercised here against an arbitrary/unknown network to keep it independent of the synchronous mainnet/testnet case already covered above), and renamed it to stop duplicating the title of the existing synchronous version of the same case. Verification: all 11 tests in this file pass; npx eslint clean (after --fix reordered imports per the project's import-sort rule); npx tsc --noEmit clean across the whole project. --- src/components/NetworkBanner.test.tsx | 33 +++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/components/NetworkBanner.test.tsx b/src/components/NetworkBanner.test.tsx index 617d3db..edebb82 100644 --- a/src/components/NetworkBanner.test.tsx +++ b/src/components/NetworkBanner.test.tsx @@ -1,8 +1,9 @@ import { render, screen } from "@testing-library/react"; +import type { ReactElement } from "react"; import { describe, expect, it, vi } from "vitest"; import { useSorokit } from "@/context/useSorokit"; -import type { NetworkInfo } from "@/lib/client"; +import type { NetworkInfo, NetworkName } from "@/lib/client"; import { NetworkBanner } from "./NetworkBanner"; @@ -16,6 +17,23 @@ function mockNetwork(network: NetworkInfo | null) { } as unknown as ReturnType); } +/** + * Renders `element` (defaulting to a bare ``) against a + * synthetic network built from just its `name` — for tests that only care + * about how the banner reacts to an arbitrary/unknown network name, without + * spelling out a full `NetworkInfo` fixture (rpcUrl/passphrase/horizonUrl + * are irrelevant to `NetworkBanner`, which only ever reads `network.name`). + */ +function renderWithNetwork(name: NetworkName, element: ReactElement = ) { + mockNetwork({ + name, + rpcUrl: "https://example-rpc.test", + passphrase: `${name} passphrase`, + horizonUrl: "https://example-horizon.test", + }); + return render(element); +} + const MAINNET_NETWORK: NetworkInfo = { name: "mainnet", rpcUrl: "https://soroban.stellar.org", @@ -140,14 +158,15 @@ describe("NetworkBanner", () => { expect(screen.getByText(/test funds only/i)).toBeInTheDocument(); }); - it("does not render when active section is 'network'", async () => { - const { client, container } = renderWithNetwork( - "testnet", + it("does not render for an arbitrary/unknown network when active section is 'network'", () => { + // Companion to the synchronous "renders nothing when active section is + // 'network'" case above, using renderWithNetwork's unknown-network path + // instead of a known NetworkInfo fixture, so the active-section gate is + // proven independent of which network is active. + const { container } = renderWithNetwork( + "private-testnet" as NetworkName, , ); - await waitFor(() => { - expect(client.network.getNetwork).toHaveBeenCalled(); - }); expect(container).toBeEmptyDOMElement(); });