Skip to content

Commit

Permalink
fix: login tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Dec 7, 2024
1 parent 9210905 commit 111119e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
5 changes: 3 additions & 2 deletions packages/frontend/src/features/menu/MenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const MenuActions = ({ menu }: { menu: NestedMenu | null }) => {
{ text: "Clauses départementales", onClick: () => setMenu("clauses-departementales") },
{ text: "Clauses nationales", onClick: () => setMenu("clauses-nationales") },
{ text: "Assistance technique", onClick: () => setMenu("help") },
{ text: "Se déconnecter", onClick: logout },
{ text: "Se déconnecter", onClick: logout, dataTestId: "logout" },
];

return (
Expand All @@ -43,10 +43,11 @@ export const MenuActions = ({ menu }: { menu: NestedMenu | null }) => {
},
})}
>
{actions.map(({ text, onClick }, index) => (
{actions.map(({ text, onClick, dataTestId }, index) => (
<Fragment key={text}>
<Button
className={css({ w: "100%" })}
data-test-id={dataTestId}
type="button"
priority="tertiary no outline"
onClick={(e) => {
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/features/menu/MenuButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const MenuButton = ({ headerRef }: { headerRef: any }) => {
className={css({ ml: "16px", mb: "0" })}
priority="tertiary"
iconId="fr-icon-account-circle-fill"
data-test-id="account-menu"
>
Mon compte
</Button>
Expand Down
3 changes: 3 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { defineConfig, devices } from "@playwright/test";
import dotenv from "dotenv";
import { expand } from "dotenv-expand";

expand(dotenv.config({ path: "./.env" }));
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
Expand Down
42 changes: 23 additions & 19 deletions tests/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { test, expect, type Route } from "@playwright/test";
import type { RouterOutputs } from "../packages/frontend/src/api";
import { db } from "../packages/backend/src/db/db";

test.beforeEach(async ({ page }) => {
await page.goto("./");
});

test.afterAll(async () => {
await cleanupDb();
});

test.describe("Create user", () => {
test("should be redirected to the login page", async ({ page }) => {
await page.waitForURL((url) => url.pathname === "/login");
Expand All @@ -21,48 +26,47 @@ test.describe("Create user", () => {
expect(page.url()).toContain("signup");
});

test("should create a new user", async ({ page }) => {
await page.route("*/**/api/create-user", mockRouteResponse);
test.only("should create a new user", async ({ page }) => {
await page.goto("./signup");

await page.fill("input[name=name]", "Test runner");
await page.fill("input[name=email]", "[email protected]");
await page.fill("input[name=password]", "Password123!");
await page.fill("input[name=name]", mockUser.name);
await page.fill("input[name=email]", mockUser.email);
await page.fill("input[name=password]", mockUser.password);

await page.selectOption("select[name=udap_id]", "udap-landes");

await page.click("button[type=submit]");

await page.waitForURL((url) => url.pathname === "/");
expect(page.url()).toContain("/");

const button = await page.waitForSelector("#fr-header-quick-access-item--Sed_connecter-0");
await page.click("button[data-test-id=account-menu]");

const button = await page.waitForSelector("[data-test-id=logout]");
await button.click();
await page.waitForURL((url) => url.pathname === "/login");
expect(page.url()).toContain("login");
});

test("should login", async ({ page }) => {
await page.route("*/**/api/login", mockRouteResponse);

await page.goto("./login");

await page.fill("input[name=email]", "[email protected]");
await page.fill("input[name=password]", "Password123!");
await page.fill("input[name=email]", mockUser.email);
await page.fill("input[name=password]", mockUser.password);

await page.click("button[type=submit]");
await page.waitForURL((url) => url.pathname === "/");
expect(page.url()).toContain("/");
});
});

const mockRouteResponse = async (route: Route) => {
await route.fulfill({ json: mockLoginResponse });
const cleanupDb = async () => {
const result = await db.deleteFrom("internal_user").where("email", "=", mockUser.email).returning("id").execute();
await db.deleteFrom("user").where("id", "=", result[0].id).execute();
};

const mockLoginResponse: RouterOutputs<"/api/create-user"> = {
token: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
user: {
id: "3787db2f-5bd0-4a43-af83-a9768c2b7e71",
name: "Test runner",
email: "[email protected]",
},
const mockUser = {
name: "Test runner",
email: "[email protected]",
password: "Password123!",
};

0 comments on commit 111119e

Please sign in to comment.