Skip to content

Commit

Permalink
feat: add playwright + user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Apr 12, 2024
1 parent 84fd842 commit a03f83f
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 7 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ yarn-debug.log*
yarn-error.log*

.env

# vercel
.vercel

Expand Down Expand Up @@ -57,4 +56,8 @@ robots.txt

local_db/*
.electric_migrations_*
trace-dir/*
trace-dir/*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
1 change: 0 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ services:
environment:
DATABASE_URL: postgresql://${PG_USER}:${PG_PASSWORD}@pg:${PG_PORT}/${PG_ELECTRIC_DB}
AUTH_JWT_KEY: ${JWT_SECRET}
# LOGICAL_PUBLISHER_PORT: 5433
PG_PROXY_PASSWORD: ${PG_PROXY_PASSWORD}
PG_PROXY_PORT: ${PG_PROXY_PORT}
AUTH_MODE: secure
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
"electric:client": "pnpm frontend client:generate",
"client:ts": "typed-openapi ./packages/backend/openapi.json --output ./packages/frontend/src/api.gen.ts",
"client:json": "pnpm backend dev --create-only",
"client:generate": "pnpm client:json && pnpm client:ts"
"client:generate": "pnpm client:json && pnpm client:ts",
"salut": "docker ps",
"e2e": "playwright test"
},
"keywords": [],
"author": "",
"devDependencies": {
"@biomejs/biome": "1.6.4",
"@databases/pg-migrations": "^5.0.3",
"@pandabox/prettier-plugin": "^0.1.0",
"@playwright/test": "^1.43.0",
"@types/node": "^20.11.28",
"electric-sql": "^0.9.5",
"prettier": "^3.2.5",
"typed-openapi": "^0.4.1"
Expand Down
1 change: 0 additions & 1 deletion packages/frontend/src/components/FullWidthButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Button, { type ButtonProps } from "@codegouvfr/react-dsfr/Button";
import { css, cx } from "#styled-system/css";

export const FullWidthButton = ({ className, type, linkProps, ...props }: ButtonProps) => {
linkProps!;
return (
<Button
{...(props as any)}
Expand Down
7 changes: 6 additions & 1 deletion packages/frontend/src/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export const LoginForm = () => {
<Link to="/reset-password">Mot de passe oublié</Link>
</styled.div>

<FullWidthButton className={css({ mt: "1.5rem" })} type="submit" onClick={form.handleSubmit(login)}>
<FullWidthButton
className={css({ mt: "1.5rem" })}
type="submit"
nativeButtonProps={{ type: "submit" }}
onClick={form.handleSubmit(login)}
>
Se connecter
</FullWidthButton>
</form>
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const SignupForm = () => {
<SignupPasswordInput form={form} />
</InputGroup>

<FullWidthButton className={css({ mt: "1.5rem" })} type="submit">
<FullWidthButton className={css({ mt: "1.5rem" })} type="submit" nativeButtonProps={{ type: "submit" }}>
Valider
</FullWidthButton>
</form>
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://localhost:5177",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
headless: false,
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

// {
// name: "firefox",
// use: { ...devices["Desktop Firefox"] },
// },

// {
// name: "webkit",
// use: { ...devices["Desktop Safari"] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: "pnpm frontend dev --port 5177",
url: "http://localhost:5177",
reuseExistingServer: !process.env.CI,
},
});
56 changes: 56 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions tests/user.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { test, expect, type Route } from "@playwright/test";
import type { RouterOutputs } from "../packages/frontend/src/api";

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

test.describe("Create user", () => {
test("should be redirected to the login page", async ({ page }) => {
await page.waitForURL((url) => url.pathname === "/login");
expect(page.url()).toContain("login");
});

test("should go to signup page", async ({ page }) => {
const button = await page.waitForSelector("[href='/signup']");
expect(button).toBeDefined();

await button.click();
await page.waitForURL((url) => url.pathname === "/signup");

expect(page.url()).toContain("signup");
});

test("should create a new user", async ({ page }) => {
await page.route("*/**/api/create-user", mockRouteResponse);
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.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 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.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 mockLoginResponse: RouterOutputs<"/api/create-user"> = {
token: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
user: {
id: "3787db2f-5bd0-4a43-af83-a9768c2b7e71",
name: "Test runner",
email: "[email protected]",
},
};

0 comments on commit a03f83f

Please sign in to comment.