generated from nholtware/service-template-react
Initial commit
This commit is contained in:
commit
69da1d8f34
34 changed files with 5531 additions and 0 deletions
80
src/app.test.tsx
Normal file
80
src/app.test.tsx
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import { render, screen } from "@testing-library/react";
|
||||
import { createMemoryRouter, RouterProvider } from "react-router";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { routes } from "./routes";
|
||||
|
||||
// Flags come from the Unleash SDK; tests control them directly.
|
||||
const flagsOn = vi.hoisted(() => new Set<string>());
|
||||
vi.mock("@unleash/proxy-client-react", () => ({
|
||||
FlagProvider: ({ children }: { children: React.ReactNode }) => children,
|
||||
useFlag: (name: string) => flagsOn.has(name),
|
||||
useVariant: () => ({ name: "disabled", enabled: false }),
|
||||
}));
|
||||
|
||||
function renderAt(path: string) {
|
||||
const router = createMemoryRouter(routes, { initialEntries: [path] });
|
||||
return render(<RouterProvider router={router} />);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
flagsOn.clear();
|
||||
window.__APP_CONFIG__ = { API_BASE_URL: "/api", APP_NAME: "test-app" };
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("app", () => {
|
||||
it("home shows the app name from runtime config and the build sha", async () => {
|
||||
renderAt("/");
|
||||
expect(
|
||||
await screen.findByRole("heading", { name: "test-app" }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByTestId("build-sha")).toHaveTextContent("dev");
|
||||
});
|
||||
|
||||
it("home hides the banner while hello-banner is off", async () => {
|
||||
renderAt("/");
|
||||
await screen.findByRole("heading", { name: "test-app" });
|
||||
expect(screen.queryByTestId("banner")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("home shows the banner while hello-banner is on", async () => {
|
||||
flagsOn.add("hello-banner");
|
||||
renderAt("/");
|
||||
expect(await screen.findByTestId("banner")).toHaveTextContent(
|
||||
"hello-banner flag is on",
|
||||
);
|
||||
});
|
||||
|
||||
it("status fetches / and /readyz under API_BASE_URL and renders the JSON", async () => {
|
||||
const fetchMock = vi.fn(async (url: string) => {
|
||||
const body =
|
||||
url === "/api/readyz" ? { status: "ready" } : { service: "svc" };
|
||||
return new Response(JSON.stringify(body), { status: 200 });
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
renderAt("/status");
|
||||
|
||||
expect(await screen.findByText(/GET \/readyz/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/"status": "ready"/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/"service": "svc"/)).toBeInTheDocument();
|
||||
expect(fetchMock.mock.calls.map((c) => c[0])).toEqual([
|
||||
"/api/",
|
||||
"/api/readyz",
|
||||
]);
|
||||
});
|
||||
|
||||
it("status shows the error page when the backend is unreachable", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => {
|
||||
throw new Error("network down");
|
||||
}),
|
||||
);
|
||||
renderAt("/status");
|
||||
expect(await screen.findByRole("alert")).toHaveTextContent("network down");
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue