Skip to content

Commit

Permalink
test code home page added
Browse files Browse the repository at this point in the history
  • Loading branch information
hasan-py committed Oct 12, 2024
1 parent cf36cd6 commit d9511f2
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 455 deletions.
18 changes: 0 additions & 18 deletions client/e2e/example.spec.ts

This file was deleted.

4 changes: 4 additions & 0 deletions client/e2e/fixtures/createModerator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"success": true,
"message": "Moderator created successfully"
}
3 changes: 3 additions & 0 deletions client/e2e/fixtures/moderatorExists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"userExists": true
}
3 changes: 3 additions & 0 deletions client/e2e/fixtures/moderatorNotExists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"userExists": false
}
9 changes: 9 additions & 0 deletions client/e2e/support/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const serverURL = (path: string) => {
return "http://localhost:8000" + path;
};

export const API_ROUTES = {
LOGIN: serverURL("/api/auth/login"),
CHECK_MODERATOR: serverURL("/api/auth/check-moderator"),
CREATE_MODERATOR: serverURL("/api/auth/create-moderator"),
};
45 changes: 45 additions & 0 deletions client/e2e/tests/home.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { test, expect } from "@playwright/test";

test.describe("Home Page", () => {
test("page rendered successfully", async ({ page }) => {
await page.goto("/");
await expect(
page.locator("text=Find your next captivating gaming moment")
).toBeVisible();
});

test("Game List: should display game cards with images, titles, and review counts", async ({
page,
}) => {
await page.goto("/");

// Check that the grid exists
await expect(page.locator(".grid")).toBeVisible();

const gameCards = page.locator(".grid .col-span-1");
const cardCount = await gameCards.count(); // Get the number of game cards

// Check if the count is greater than 1
expect(cardCount).toBeGreaterThan(1);

// Check the first game card
const firstGameCard = gameCards.first();
await expect(firstGameCard.locator("img")).toHaveAttribute(
"src",
/thumbnail\.jpg/
);
await expect(firstGameCard.locator("img")).toHaveAttribute(
"alt",
/Game Image/
);

// Check game card details
await expect(firstGameCard.locator("p.text-lg")).toBeVisible();
await expect(firstGameCard.locator("p.text-lg")).not.toBeEmpty();

await expect(firstGameCard.locator("p.text-sm")).toBeVisible();
await expect(firstGameCard.locator("p.text-sm")).not.toBeEmpty();

await expect(firstGameCard.locator("p.text-xs")).toContainText("Reviews");
});
});
140 changes: 140 additions & 0 deletions client/e2e/tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// import { test, expect, Page } from "@playwright/test";
// import { API_ROUTES } from "../support/constants";

// test.describe("Moderator Authentication", () => {
// test.beforeEach(async ({ page }) => {
// await page.goto("/login");
// });

// const interceptCheckModerator = async (page: Page, fixtureName: string) => {
// await page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
// const jsonResponse = await route.fetch();
// await jsonResponse.json();
// route.fulfill({
// status: 200,
// });
// });
// };

// test("should display login or create account based on user existence", async ({
// page,
// }) => {
// await interceptCheckModerator(page, "moderatorExists.json");

// // Visit the login page or trigger the action that sends the request
// await page.goto("/login"); // Ensure the page that triggers the API call is visited

// // Now wait for the response
// const response = await page.waitForResponse(API_ROUTES.CHECK_MODERATOR);
// const data = response.json();
// const userExists = data;

// // Expectation based on the response
// if (!!userExists) {
// await expect(page.locator("text=Login")).toBeVisible();
// } else {
// await expect(page.locator("text=Create")).toBeVisible();
// }
// });

// test("should show error on login validations", async ({ page }) => {
// await interceptCheckModerator(page, "moderatorExists.json");

// await page.click("button");
// await expect(page.locator("text=Invalid email address")).toBeVisible();
// await expect(
// page.locator("text=Password must be at least 6 characters")
// ).toBeVisible();
// });

// test("should show error on create moderator validations", async ({
// page,
// }) => {
// await interceptCheckModerator(page, "moderatorNotExists.json");

// await page.click("button");
// await expect(page.locator("text=Invalid email address")).toBeVisible();
// await expect(
// page.locator("text=Password must be at least 6 characters")
// ).toBeVisible();
// await expect(page.locator("text=Name is required")).toBeVisible();
// });

// test("should toggle the password visibility", async ({ page }) => {
// await interceptCheckModerator(page, "moderatorExists.json");

// await page.fill('input[placeholder="Password"]', "password123");
// await page.click("#toggle-password");
// await expect(page.locator('input[placeholder="Password"]')).toHaveAttribute(
// "type",
// "text"
// );
// await expect(page.locator('input[placeholder="Password"]')).toHaveValue(
// "password123"
// );
// await page.click("#toggle-password");
// await expect(page.locator('input[placeholder="Password"]')).toHaveAttribute(
// "type",
// "password"
// );
// });

// test("should create an account for new users", async ({ page }) => {
// await interceptCheckModerator(page, "moderatorNotExists.json");

// await page.route(API_ROUTES.CREATE_MODERATOR, (route) =>
// route.fulfill({
// status: 200,
// body: JSON.stringify({
// success: true,
// message: "Moderator created! Please login!",
// }),
// })
// );

// const response = await page.waitForResponse(API_ROUTES.CHECK_MODERATOR);
// const data = await response.json();
// const userExists = data.userExists;

// if (!userExists) {
// await expect(page.locator("text=Create Account")).toBeVisible();
// await page.fill('input[placeholder="Name"]', "John Doe");
// await page.fill('input[placeholder="Email"]', "[email protected]");
// await page.fill('input[placeholder="Password"]', "password123");
// await page.click("button");

// const createModeratorResponse = await page.waitForResponse(
// API_ROUTES.CREATE_MODERATOR
// );
// const createModeratorData = await createModeratorResponse.json();

// expect(createModeratorData.success).toBeTruthy();
// await expect(
// page.locator("text=Moderator created! Please login!")
// ).toBeVisible();
// }
// });

// test("should login an existing user", async ({ page }) => {
// await interceptCheckModerator(page, "moderatorExists.json");

// await page.route(API_ROUTES.LOGIN, (route) =>
// route.fulfill({
// status: 200,
// body: JSON.stringify({ token: "mockToken" }),
// })
// );

// await page.fill('input[placeholder="Email"]', "[email protected]");
// await page.fill('input[placeholder="Password"]', "password123");
// await page.click("button");

// const loginResponse = await page.waitForResponse(API_ROUTES.LOGIN);
// const loginData = await loginResponse.json();

// expect(loginData.token).toBe("mockToken");
// await expect(page.locator("text=Login successfully")).toBeVisible();

// await expect(page).toHaveURL("/admin");
// });
// });
Loading

0 comments on commit d9511f2

Please sign in to comment.