Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI Tests for username change on /settings page. #1203

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
6 changes: 4 additions & 2 deletions app/(app)/settings/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ const Settings = ({ profile }: { profile: User }) => {

useEffect(() => {
if (isSuccess) {
toast.success("Saved");
toast.success("Saved", { className: "toast-success" });
}
if (isError) {
toast.error("Something went wrong saving settings.");
toast.error("Something went wrong saving settings.", {
className: "toast-error",
});
}
}, [isError, isSuccess]);

Expand Down
56 changes: 53 additions & 3 deletions e2e/settings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from "@playwright/test";
import { test, expect } from "@playwright/test";
import { loggedInAsUserOne } from "./utils";

test.describe("Unauthenticated setttings Page", () => {
Expand All @@ -10,6 +10,56 @@ test.describe("Authenticated settings Page", () => {
test.beforeEach(async ({ page }) => {
await loggedInAsUserOne(page);
});
//
// Replace with tests for authenticated users

// Test for changing username
test("Username input field", async ({ page }) => {
await page.goto("http://localhost:3000/settings", { timeout: 30000 });

const inputField = page.locator('input[id="username"]');
const submitButton = page.locator('button[type="submit"]');

// Test that the input field is visible and has the correct attributes
await expect(inputField).toBeVisible();
await expect(inputField).toHaveAttribute("type", "text");
await expect(inputField).toHaveAttribute("autocomplete", "username");

// Test that the error message appears when the input field is invalid
await inputField.fill("45&p^x#@!96%*()");
await submitButton.click();
const errorMessage = page.locator(
'p:text-is("Username can only contain alphanumerics and dashes.")',
);
await expect(errorMessage).toBeVisible();
await expect(errorMessage).toHaveText(
"Username can only contain alphanumerics and dashes.",
);

// Test minimum length
await inputField.fill("ab");
await submitButton.click();
await expect(
page.locator('p:text-is("String must contain at least 3 character(s)")'),
).toBeVisible();

// Test maximum length
await inputField.fill("a".repeat(51));
await submitButton.click();
await expect(
page.locator('p:text-is("Max username length is 40 characters.")'),
).toBeVisible();

// Reset the form
await page.locator('button:has-text("Reset")').click();

// Test that the input field can be filled with a valid value and saves it
await inputField.fill("codu-rules");
await page.locator('button[type="submit"]').click();
const toastError = page.locator(".toast-success");
await expect(toastError).toBeVisible();
await expect(toastError).toBeHidden();

// Reload the page and check that the input field has the correct value
await page.reload();
await expect(inputField).toHaveValue("codu-rules");
});
});
2 changes: 1 addition & 1 deletion e2e/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const setup = async () => {
image: `https://robohash.org/${encodeURIComponent(name)}?bgset=bg1`,
location: "Ireland",
bio: "Hi I am an robot",
websiteUrl: "codu.co",
websiteUrl: "https://codu.co",
};
const [createdUser] = await db.insert(user).values(userData).returning();
return createdUser;
Expand Down
Loading