diff --git a/e2e/tests/home.spec.js b/e2e/tests/home.spec.js index 8d0e55c5..42b9ab2b 100644 --- a/e2e/tests/home.spec.js +++ b/e2e/tests/home.spec.js @@ -18,5 +18,5 @@ test("has Vite.js link", async ({ page }) => { test("shows server status", async ({ page }) => { await page.goto("/"); - await expect(page.getByText("Server status: OK")).toBeAttached(); + await expect(page.getByText("Server says: Hello, world!")).toBeAttached(); }); diff --git a/web/src/components/ServerStatus.jsx b/web/src/components/ServerStatus.jsx index dcb4fbd9..bfb88396 100644 --- a/web/src/components/ServerStatus.jsx +++ b/web/src/components/ServerStatus.jsx @@ -1,14 +1,18 @@ import { useEffect, useState } from "react"; +import { getMessage } from "../services/messageService.js"; + export default function ServerStatus() { + const [loading, setLoading] = useState(false); const [state, setState] = useState(); useEffect(() => { - fetch("/healthz") - .then((res) => res.text()) + setLoading(true); + getMessage() .then(setState) - .catch((err) => setState(err.message)); + .catch((err) => console.error(err.message)) + .finally(() => setLoading(false)); }, []); - return
Server status: {state ?? "unknown"}
; + return loading ? null :
Server says: {state ?? "unknown"}
; } diff --git a/web/src/components/ServerStatus.test.jsx b/web/src/components/ServerStatus.test.jsx index a5426a8a..2b4090b8 100644 --- a/web/src/components/ServerStatus.test.jsx +++ b/web/src/components/ServerStatus.test.jsx @@ -8,22 +8,22 @@ import ServerStatus from "./ServerStatus"; describe("ServerStatus component", () => { it("fetches the right thing", async () => { - server.use(http.get("/healthz", () => HttpResponse.text("OK"))); + server.use(http.get("/api/message", () => HttpResponse.text("hi!"))); render(); await expect( - screen.findByText(/server status: ok/i), + screen.findByText(/server says: hi!/i), ).resolves.toBeInTheDocument(); }); it("shows message if server errors", async () => { - server.use(http.get("/healthz", () => HttpResponse.error())); + server.use(http.get("/api/message", () => HttpResponse.error())); render(); await expect( - screen.findByText(/server status: failed to fetch/i), + screen.findByText(/server says: unknown/i), ).resolves.toBeInTheDocument(); }); }); diff --git a/web/src/pages/Home.test.jsx b/web/src/pages/Home.test.jsx index be1bf35e..c83b71bb 100644 --- a/web/src/pages/Home.test.jsx +++ b/web/src/pages/Home.test.jsx @@ -8,7 +8,7 @@ import Home from "./Home.jsx"; describe("Home component", () => { beforeEach(() => - server.use(http.get("/healthz", () => HttpResponse.text("OK"))), + server.use(http.get("/api/message", () => HttpResponse.text(""))), ); it("shows a link", () => { diff --git a/web/src/services/messageService.js b/web/src/services/messageService.js new file mode 100644 index 00000000..437f53c9 --- /dev/null +++ b/web/src/services/messageService.js @@ -0,0 +1,7 @@ +export async function getMessage() { + const res = await fetch("/api/message"); + if (res.ok) { + return res.text(); + } + throw new Error(res.statusText); +}