Skip to content

Commit 07745b3

Browse files
committed
test: add sign up e3e tests
fastapi/full-stack-fastapi-template#1268
1 parent e33bcfa commit 07745b3

File tree

3 files changed

+182
-1
lines changed

3 files changed

+182
-1
lines changed

frontend/src/routes/signup.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function SignUp() {
8787
<Input
8888
id="full_name"
8989
minLength={3}
90-
{...register("full_name")}
90+
{...register("full_name", { required: "Full Name is required" })}
9191
placeholder="Full Name"
9292
type="text"
9393
/>
@@ -102,6 +102,7 @@ function SignUp() {
102102
<Input
103103
id="email"
104104
{...register("email", {
105+
required: "Email is required",
105106
pattern: emailPattern,
106107
})}
107108
placeholder="Email"

frontend/tests/sign-up.spec.ts

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import { type Page, expect, test } from "@playwright/test"
2+
3+
import { randomEmail } from "./utils/random"
4+
5+
test.use({ storageState: { cookies: [], origins: [] } })
6+
7+
type OptionsType = {
8+
exact?: boolean
9+
}
10+
11+
const fillForm = async (
12+
page: Page,
13+
full_name: string,
14+
email: string,
15+
password: string,
16+
confirm_password: string,
17+
) => {
18+
await page.getByPlaceholder("Full Name").fill(full_name)
19+
await page.getByPlaceholder("Email").fill(email)
20+
await page.getByPlaceholder("Password", { exact: true }).fill(password)
21+
await page.getByPlaceholder("Repeat Password").fill(confirm_password)
22+
}
23+
24+
const verifyInput = async (
25+
page: Page,
26+
placeholder: string,
27+
options?: OptionsType,
28+
) => {
29+
const input = page.getByPlaceholder(placeholder, options)
30+
await expect(input).toBeVisible()
31+
await expect(input).toHaveText("")
32+
await expect(input).toBeEditable()
33+
}
34+
35+
test("Inputs are visible, empty and editable", async ({ page }) => {
36+
await page.goto("/signup")
37+
38+
await verifyInput(page, "Full Name")
39+
await verifyInput(page, "Email")
40+
await verifyInput(page, "Password", { exact: true })
41+
await verifyInput(page, "Repeat Password")
42+
})
43+
44+
test("Sign Up button is visible", async ({ page }) => {
45+
await page.goto("/signup")
46+
47+
await expect(page.getByRole("button", { name: "Sign Up" })).toBeVisible()
48+
})
49+
50+
test("Log In link is visible", async ({ page }) => {
51+
await page.goto("/signup")
52+
53+
await expect(page.getByRole("link", { name: "Log In" })).toBeVisible()
54+
})
55+
56+
test("Sign up with valid name, email, and password", async ({ page }) => {
57+
const full_name = "Test User"
58+
const email = randomEmail()
59+
const password = "changethis"
60+
61+
await page.goto("/signup")
62+
await fillForm(page, full_name, email, password, password)
63+
await page.getByRole("button", { name: "Sign Up" }).click()
64+
})
65+
66+
test("Sign up with invalid email", async ({ page }) => {
67+
await page.goto("/signup")
68+
69+
await fillForm(
70+
page,
71+
"Playwright Test",
72+
"invalid-email",
73+
"changethis",
74+
"changethis",
75+
)
76+
await page.getByRole("button", { name: "Sign Up" }).click()
77+
78+
await expect(page.getByText("Invalid email address")).toBeVisible()
79+
})
80+
81+
test("Sign up with existing email", async ({ page }) => {
82+
const full_name = "Test User"
83+
const email = randomEmail()
84+
const password = "changethis"
85+
86+
// Sign up with an email
87+
await page.goto("/signup")
88+
89+
await fillForm(page, full_name, email, password, password)
90+
await page.getByRole("button", { name: "Sign Up" }).click()
91+
92+
// Sign up again with the same email
93+
await page.goto("/signup")
94+
95+
await fillForm(page, full_name, email, password, password)
96+
await page.getByRole("button", { name: "Sign Up" }).click()
97+
98+
await page
99+
.getByText("The user with this email already exists in the system")
100+
.click()
101+
})
102+
103+
test("Sign up with weak password", async ({ page }) => {
104+
const full_name = "Test User"
105+
const email = randomEmail()
106+
const password = "weak"
107+
108+
await page.goto("/signup")
109+
110+
await fillForm(page, full_name, email, password, password)
111+
await page.getByRole("button", { name: "Sign Up" }).click()
112+
113+
await expect(
114+
page.getByText("Password must be at least 8 characters"),
115+
).toBeVisible()
116+
})
117+
118+
test("Sign up with mismatched passwords", async ({ page }) => {
119+
const full_name = "Test User"
120+
const email = randomEmail()
121+
const password = "changethis"
122+
const password2 = "changethat"
123+
124+
await page.goto("/signup")
125+
126+
await fillForm(page, full_name, email, password, password2)
127+
await page.getByRole("button", { name: "Sign Up" }).click()
128+
129+
await expect(page.getByText("Passwords do not match")).toBeVisible()
130+
})
131+
132+
test("Sign up with missing full name", async ({ page }) => {
133+
const full_name = ""
134+
const email = randomEmail()
135+
const password = "changethis"
136+
137+
await page.goto("/signup")
138+
139+
await fillForm(page, full_name, email, password, password)
140+
await page.getByRole("button", { name: "Sign Up" }).click()
141+
142+
await expect(page.getByText("Full Name is required")).toBeVisible()
143+
})
144+
145+
test("Sign up with missing email", async ({ page }) => {
146+
const full_name = "Test User"
147+
const email = ""
148+
const password = "changethis"
149+
150+
await page.goto("/signup")
151+
152+
await fillForm(page, full_name, email, password, password)
153+
await page.getByRole("button", { name: "Sign Up" }).click()
154+
155+
await expect(page.getByText("Email is required")).toBeVisible()
156+
})
157+
158+
test("Sign up with missing password", async ({ page }) => {
159+
const full_name = ""
160+
const email = randomEmail()
161+
const password = ""
162+
163+
await page.goto("/signup")
164+
165+
await fillForm(page, full_name, email, password, password)
166+
await page.getByRole("button", { name: "Sign Up" }).click()
167+
168+
await expect(page.getByText("Password is required")).toBeVisible()
169+
})

frontend/tests/utils/random.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const randomEmail = () =>
2+
`test_${Math.random().toString(36).substring(7)}@example.com`
3+
4+
export const randomTeamName = () =>
5+
`Team ${Math.random().toString(36).substring(7)}`
6+
7+
export const slugify = (text: string) =>
8+
text
9+
.toLowerCase()
10+
.replace(/\s+/g, "-")
11+
.replace(/[^\w-]+/g, "")

0 commit comments

Comments
 (0)