test: use testids instead of text queries for unit tests (#5)
CI / Generate Certificates (push) Successful in 28s
CI / Frontend CI (push) Successful in 1m3s
CI / E2E Tests (push) Has been cancelled
CI / Backend CI (push) Has been cancelled

Co-authored-by: me <ramvignesh-b@github.com>
Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
2026-05-07 23:54:12 +00:00
parent 7e53229308
commit d625cbb1fb
12 changed files with 220 additions and 223 deletions
+18 -18
View File
@@ -9,8 +9,8 @@ function renderGuard(ui: React.ReactNode, mountPath: "/protected" | "/public") {
return render( return render(
<MemoryRouter initialEntries={[mountPath]}> <MemoryRouter initialEntries={[mountPath]}>
<Routes> <Routes>
<Route path="/login" element={<div>Login Page</div>} /> <Route path="/login" element={<div data-testid="login-page">Login Page</div>} />
<Route path="/drawer" element={<div>Drawer Page</div>} /> <Route path="/drawer" element={<div data-testid="drawer-page">Drawer Page</div>} />
<Route path="/protected" element={ui} /> <Route path="/protected" element={ui} />
<Route path="/public" element={ui} /> <Route path="/public" element={ui} />
</Routes> </Routes>
@@ -35,13 +35,13 @@ describe("ProtectedRoute", () => {
}); });
renderGuard( renderGuard(
<ProtectedRoute> <ProtectedRoute>
<div>Secret</div> <div data-testid="secret-page">Secret</div>
</ProtectedRoute>, </ProtectedRoute>,
"/protected", "/protected",
); );
expect(screen.getByText(/Unsealing/i)).toBeInTheDocument(); expect(screen.getByTestId("splash-screen")).toBeInTheDocument();
expect(screen.queryByText("Secret")).not.toBeInTheDocument(); expect(screen.queryByTestId("secret-page")).not.toBeInTheDocument();
}); });
it("should redirect unauthenticated users to /login", () => { it("should redirect unauthenticated users to /login", () => {
@@ -52,12 +52,12 @@ describe("ProtectedRoute", () => {
}); });
renderGuard( renderGuard(
<ProtectedRoute> <ProtectedRoute>
<div>Secret</div> <div data-testid="secret-page">Secret</div>
</ProtectedRoute>, </ProtectedRoute>,
"/protected", "/protected",
); );
expect(screen.getByText("Login Page")).toBeInTheDocument(); expect(screen.getByTestId("login-page")).toBeInTheDocument();
expect(screen.queryByText("Secret")).not.toBeInTheDocument(); expect(screen.queryByTestId("secret-page")).not.toBeInTheDocument();
}); });
it("should render page for authenticated users", () => { it("should render page for authenticated users", () => {
@@ -68,12 +68,12 @@ describe("ProtectedRoute", () => {
}); });
renderGuard( renderGuard(
<ProtectedRoute> <ProtectedRoute>
<div>Secret</div> <div data-testid="secret-page">Secret</div>
</ProtectedRoute>, </ProtectedRoute>,
"/protected", "/protected",
); );
expect(screen.getByText("Secret")).toBeInTheDocument(); expect(screen.getByTestId("secret-page")).toBeInTheDocument();
}); });
}); });
@@ -86,12 +86,12 @@ describe("PublicRoute", () => {
}); });
renderGuard( renderGuard(
<PublicRoute> <PublicRoute>
<div>Login Page</div> <div data-testid="mock-login-page">Login Page</div>
</PublicRoute>, </PublicRoute>,
"/public", "/public",
); );
expect(screen.getByText(/Unsealing/i)).toBeInTheDocument(); expect(screen.getByTestId("splash-screen")).toBeInTheDocument();
expect(screen.queryByText("Login Page")).not.toBeInTheDocument(); expect(screen.queryByTestId("mock-login-page")).not.toBeInTheDocument();
}); });
it("should redirect authenticated users to /drawer", () => { it("should redirect authenticated users to /drawer", () => {
@@ -102,12 +102,12 @@ describe("PublicRoute", () => {
}); });
renderGuard( renderGuard(
<PublicRoute> <PublicRoute>
<div>Login Form</div> <div data-testid="login-form">Login Form</div>
</PublicRoute>, </PublicRoute>,
"/public", "/public",
); );
expect(screen.getByText("Drawer Page")).toBeInTheDocument(); expect(screen.getByTestId("drawer-page")).toBeInTheDocument();
expect(screen.queryByText("Login Form")).not.toBeInTheDocument(); expect(screen.queryByTestId("login-form")).not.toBeInTheDocument();
}); });
it("should render page for unauthenticated users", () => { it("should render page for unauthenticated users", () => {
@@ -118,10 +118,10 @@ describe("PublicRoute", () => {
}); });
renderGuard( renderGuard(
<PublicRoute> <PublicRoute>
<div>Login Form</div> <div data-testid="login-form">Login Form</div>
</PublicRoute>, </PublicRoute>,
"/public", "/public",
); );
expect(screen.getByText("Login Form")).toBeInTheDocument(); expect(screen.getByTestId("login-form")).toBeInTheDocument();
}); });
}); });
+1 -1
View File
@@ -3,7 +3,7 @@ import Logo from "./Logo";
export default function SplashScreen() { export default function SplashScreen() {
return ( return (
<div className="fixed w-screen h-screen inset-0 flex flex-col items-center justify-center z-9999 before:absolute before:top-0 before:left-0 before:w-full before:h-full before:content-[''] before:opacity-[0.03] before:z-10 before:pointer-events-none before:bg-[url('assets/noise.gif')"> <div data-testid="splash-screen" className="fixed w-screen h-screen inset-0 flex flex-col items-center justify-center z-9999 before:absolute before:top-0 before:left-0 before:w-full before:h-full before:content-[''] before:opacity-[0.03] before:z-10 before:pointer-events-none before:bg-[url('assets/noise.gif')">
<div className="flex flex-col items-center gap-6 animate-pulse"> <div className="flex flex-col items-center gap-6 animate-pulse">
<Logo /> <Logo />
@@ -40,6 +40,7 @@ export function DrawerSection({
> >
<div className="flex-1"> <div className="flex-1">
<div <div
data-testid="drawer-section-title"
className={`font-sans text-xs tracking-widester uppercase transition-colors duration-800 ${ className={`font-sans text-xs tracking-widester uppercase transition-colors duration-800 ${
isOpen isOpen
? "text-base-content" ? "text-base-content"
@@ -12,7 +12,7 @@ export function PasskeyModal() {
className="text-primary mx-auto mb-8 animate-pulse" className="text-primary mx-auto mb-8 animate-pulse"
weight="duotone" weight="duotone"
/> />
<h3 className="font-bold text-lg font-display text-primary"> <h3 data-testid="passkey-modal-title" className="font-bold text-lg font-display text-primary">
You've been away a while. You've been away a while.
</h3> </h3>
<p className="py-4 font-sans"> <p className="py-4 font-sans">
@@ -123,7 +123,7 @@ export function EnvelopeReveal({
<span className={"text-neutral-content/60 font-xs font-display"}> <span className={"text-neutral-content/60 font-xs font-display"}>
to to
</span> </span>
<h1 className="text-3xl font-bold text-base-content">{recipient}</h1> <h1 data-testid="envelope-recipient" className="text-3xl font-bold text-base-content">{recipient}</h1>
<p className="text-base-content/60 font-display mt-8">{date}</p> <p className="text-base-content/60 font-display mt-8">{date}</p>
<img <img
src={stamp} src={stamp}
+1 -1
View File
@@ -24,7 +24,7 @@ export const LogModal = ({
{status === "WARN" && ( {status === "WARN" && (
<WarningIcon className="text-warning" size={16} weight="duotone" /> <WarningIcon className="text-warning" size={16} weight="duotone" />
)} )}
{message} <span data-testid="log-modal-message">{message}</span>
{log && ( {log && (
<> <>
<div className="divider text-primary-content text-xs uppercase tracking-widest"> <div className="divider text-primary-content text-xs uppercase tracking-widest">
+6 -6
View File
@@ -48,10 +48,10 @@ describe("Drawer Page", () => {
</MemoryRouter>, </MemoryRouter>,
); );
expect(screen.getByText(/Drafts/i)).toBeInTheDocument(); expect(screen.getByTestId("drawer-section-drafts")).toBeInTheDocument();
expect(screen.getAllByText(/Kept/i).length).toBeGreaterThanOrEqual(1); expect(screen.getAllByTestId("drawer-section-title").length).toBeGreaterThanOrEqual(1);
expect(screen.getByText(/Vault/i)).toBeInTheDocument(); expect(screen.getByTestId("drawer-section-vault")).toBeInTheDocument();
expect(screen.getByText(/This drawer remains silent/i)).toBeInTheDocument(); expect(screen.getByTestId("empty-drawer-message-drafts")).toBeInTheDocument();
}); });
it("renders the loading state", () => { it("renders the loading state", () => {
@@ -70,7 +70,7 @@ describe("Drawer Page", () => {
</MemoryRouter>, </MemoryRouter>,
); );
expect(screen.getByText(/Opening your cabinet/i)).toBeInTheDocument(); expect(screen.getByTestId("drawer-loading-state")).toBeInTheDocument();
}); });
it("renders the authentication required modal when api requires auth", () => { it("renders the authentication required modal when api requires auth", () => {
@@ -89,7 +89,7 @@ describe("Drawer Page", () => {
</MemoryRouter>, </MemoryRouter>,
); );
expect(screen.getByText(/You've been away a while./i)).toBeInTheDocument(); expect(screen.getByTestId("passkey-modal-title")).toBeInTheDocument();
expect(screen.getByPlaceholderText(/password/i)).toBeInTheDocument(); expect(screen.getByPlaceholderText(/password/i)).toBeInTheDocument();
}); });
+7 -2
View File
@@ -68,7 +68,7 @@ export default function Drawer() {
{loading ? ( {loading ? (
<div className="flex-1 flex flex-col items-center justify-center p-12 gap-4"> <div className="flex-1 flex flex-col items-center justify-center p-12 gap-4">
<span className="loading loading-ring loading-lg text-primary opacity-20"></span> <span className="loading loading-ring loading-lg text-primary opacity-20"></span>
<span className="text-xxs uppercase tracking-widester font-sans text-base-content/20 animate-pulse"> <span data-testid="drawer-loading-state" className="text-xxs uppercase tracking-widester font-sans text-base-content/20 animate-pulse">
Opening your cabinet... Opening your cabinet...
</span> </span>
</div> </div>
@@ -90,6 +90,11 @@ export default function Drawer() {
timestamp={formatRelativeDate(draft.updated_at)} timestamp={formatRelativeDate(draft.updated_at)}
/> />
))} ))}
{drafts.length === 0 && (
<p data-testid="empty-drawer-message-drafts" className="text-center text-base-content/20 mt-4">
This drawer remains silent
</p>
)}
</DrawerSection> </DrawerSection>
<DrawerSection <DrawerSection
@@ -126,7 +131,7 @@ export default function Drawer() {
/> />
))} ))}
{sent.length === 0 && ( {sent.length === 0 && (
<p className="text-center text-base-content/20 mt-4"> <p className="empty-drawer-message-sent text-center text-base-content/20 mt-4">
This drawer remains silent This drawer remains silent
</p> </p>
)} )}
+5 -13
View File
@@ -1,4 +1,4 @@
import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { fireEvent, render, screen, waitForElementToBeRemoved } from "@testing-library/react";
import { HttpResponse, http } from "msw"; import { HttpResponse, http } from "msw";
import { MemoryRouter, Route, Routes } from "react-router-dom"; import { MemoryRouter, Route, Routes } from "react-router-dom";
import { beforeEach, describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -79,9 +79,7 @@ describe("Editor Page", () => {
); );
// Wait for initial load to complete // Wait for initial load to complete
await waitFor(() => { await waitForElementToBeRemoved(() => screen.queryByTestId("opening-draft-overlay"));
expect(screen.queryByText(/Opening your draft/i)).not.toBeInTheDocument();
});
const canvas = screen.getByTestId("canvas"); const canvas = screen.getByTestId("canvas");
expect(canvas.getAttribute("data-readonly")).toBe("false"); expect(canvas.getAttribute("data-readonly")).toBe("false");
@@ -107,9 +105,7 @@ describe("Editor Page", () => {
fireEvent.click(confirmVaultBtn); fireEvent.click(confirmVaultBtn);
// Wait for save to complete and check readOnly // Wait for save to complete and check readOnly
await waitFor(() => { expect(await screen.findByTestId("save-success-toast")).toBeInTheDocument();
expect(screen.getByText(/Your letter is saved/i)).toBeInTheDocument();
});
expect(canvas.getAttribute("data-readonly")).toBe("true"); expect(canvas.getAttribute("data-readonly")).toBe("true");
expect(screen.getByLabelText(/recipient/i)).toBeDisabled(); expect(screen.getByLabelText(/recipient/i)).toBeDisabled();
@@ -140,9 +136,7 @@ describe("Editor Page", () => {
</MemoryRouter>, </MemoryRouter>,
); );
await waitFor(() => { await waitForElementToBeRemoved(() => screen.queryByTestId("opening-draft-overlay"));
expect(screen.queryByText(/Opening your draft/i)).not.toBeInTheDocument();
});
const canvas = screen.getByTestId("canvas"); const canvas = screen.getByTestId("canvas");
@@ -156,9 +150,7 @@ describe("Editor Page", () => {
if (!secondarySealBtn) throw new Error("Secondary seal button not found"); if (!secondarySealBtn) throw new Error("Secondary seal button not found");
fireEvent.click(secondarySealBtn); fireEvent.click(secondarySealBtn);
await waitFor(() => { expect(await screen.findByTestId("save-success-toast")).toBeInTheDocument();
expect(screen.getByText(/Your letter is saved/i)).toBeInTheDocument();
});
expect(canvas.getAttribute("data-readonly")).toBe("true"); expect(canvas.getAttribute("data-readonly")).toBe("true");
expect(screen.getByLabelText(/recipient/i)).toBeDisabled(); expect(screen.getByLabelText(/recipient/i)).toBeDisabled();
+5 -4
View File
@@ -31,7 +31,7 @@ describe("Login Page", () => {
await userEvent.type(screen.getByLabelText(/password/i), "password123"); await userEvent.type(screen.getByLabelText(/password/i), "password123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i })); await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
expect(await screen.findByText(/technical issues/i)).toBeInTheDocument(); expect(await screen.findByTestId("login-error-message")).toHaveTextContent(/technical issues/i);
}); });
it.each([ it.each([
@@ -73,8 +73,8 @@ describe("Login Page", () => {
> >
<Routes> <Routes>
<Route path="/login" element={<Login />} /> <Route path="/login" element={<Login />} />
<Route path="/drawer" element={<div>Drawer</div>} /> <Route path="/drawer" element={<div data-testid="drawer-page">Drawer</div>} />
<Route path="/read/:publicId" element={<div>Reader</div>} /> <Route path="/read/:publicId" element={<div data-testid="reader-page">Reader</div>} />
</Routes> </Routes>
</MemoryRouter>, </MemoryRouter>,
); );
@@ -83,6 +83,7 @@ describe("Login Page", () => {
await userEvent.type(screen.getByLabelText(/password/i), "password123"); await userEvent.type(screen.getByLabelText(/password/i), "password123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i })); await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
expect(await screen.findByText(nextRoute)).toBeInTheDocument(); const expectedTestId = nextRoute.toLowerCase() === "drawer" ? "drawer-page" : "reader-page";
expect(await screen.findByTestId(expectedTestId)).toBeInTheDocument();
}); });
}); });
+1 -1
View File
@@ -89,7 +89,7 @@ export default function Login() {
{apiError && ( {apiError && (
<div className="alert alert-error text-xs py-2 rounded-md"> <div className="alert alert-error text-xs py-2 rounded-md">
<span>{apiError}</span> <span data-testid="login-error-message">{apiError}</span>
</div> </div>
)} )}
+4 -6
View File
@@ -1,4 +1,4 @@
import { render, screen, waitFor } from "@testing-library/react"; import { render, screen } from "@testing-library/react";
import { HttpResponse, http } from "msw"; import { HttpResponse, http } from "msw";
import { MemoryRouter, Route, Routes, useLocation } from "react-router-dom"; import { MemoryRouter, Route, Routes, useLocation } from "react-router-dom";
import { beforeEach, describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -76,9 +76,7 @@ describe("Reader Page", () => {
</Routes> </Routes>
</MemoryRouter>, </MemoryRouter>,
); );
await waitFor(() => { expect(await screen.findByTestId("envelope-recipient")).toHaveTextContent(/Guest/i);
expect(screen.getByText(/Guest/i)).toBeInTheDocument();
});
}); });
it("should display an error message if the server request fails", async () => { it("should display an error message if the server request fails", async () => {
@@ -100,8 +98,8 @@ describe("Reader Page", () => {
); );
expect( expect(
await screen.findByText(/Failed to load letter/i), await screen.findByTestId("log-modal-message"),
).toBeInTheDocument(); ).toHaveTextContent(/Failed to load letter/i);
}); });
it("should navigate to the login page with redirect url when the letter has no sharing key and the user is not logged in", async () => { it("should navigate to the login page with redirect url when the letter has no sharing key and the user is not logged in", async () => {