Skip to content

Commit

Permalink
Add more client-side structure
Browse files Browse the repository at this point in the history
  • Loading branch information
textbook committed Feb 16, 2024
1 parent 2d5df0e commit c0a7d76
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion e2e/tests/home.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
12 changes: 8 additions & 4 deletions web/src/components/ServerStatus.jsx
Original file line number Diff line number Diff line change
@@ -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 <div>Server status: {state ?? "unknown"}</div>;
return loading ? null : <div>Server says: {state ?? "unknown"}</div>;
}
8 changes: 4 additions & 4 deletions web/src/components/ServerStatus.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ServerStatus />);

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(<ServerStatus />);

await expect(
screen.findByText(/server status: failed to fetch/i),
screen.findByText(/server says: unknown/i),
).resolves.toBeInTheDocument();
});
});
2 changes: 1 addition & 1 deletion web/src/pages/Home.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
7 changes: 7 additions & 0 deletions web/src/services/messageService.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c0a7d76

Please sign in to comment.