Skip to content

Commit

Permalink
updated some test code
Browse files Browse the repository at this point in the history
  • Loading branch information
hasan-py committed Oct 19, 2024
1 parent a52bf0a commit 63db182
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 23 deletions.
150 changes: 150 additions & 0 deletions client/e2e/tests/gameListAndReview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { test, expect } from "@playwright/test";
import { API_ROUTES } from "../support/constants";
import { gameListResponse, moderatorExistsResponse } from "../fixtures";

test.describe("Game List", () => {
test.beforeEach(async ({ page }) => {
await page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
await route.fulfill({ json: moderatorExistsResponse });
});

await page.goto("/login");

await page.route(API_ROUTES.LOGIN, async (route) => {
await route.fulfill({
json: {
token: "mockToken",
},
});
});

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

await page.click("button");

await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: gameListResponse });
});

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

test("Should display game cards with images, titles, and review counts", async ({
page,
}) => {
await expect(page.locator(".grid")).toBeVisible();

const gameCards = page.locator(".grid .col-span-1");

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

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");
});

test("Should check hover effect on game cards", async ({ page }) => {
const firstGameCard = page.locator(".grid .col-span-1 div").first();
await firstGameCard.hover();
await expect(firstGameCard).toHaveClass(/hover:-translate-y-1/);
});
});

// test.describe("Game Review", () => {
// test.beforeEach(async ({ page }) => {
// await page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
// await route.fulfill({ json: moderatorExistsResponse });
// });

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

// await page.route(API_ROUTES.LOGIN, async (route) => {
// await route.fulfill({
// json: {
// token: "mockToken",
// },
// });
// });

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

// await page.click("button");

// await page.route(API_ROUTES.GAME_LIST, async (route) => {
// await route.fulfill({ json: gameListResponse });
// });

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

// test("Game Review: should render game review page", async ({ page }) => {
// await page.goto("/");
// const firstGameCard = page.locator(".grid .col-span-1 div").first();
// await firstGameCard.click();

// await expect(page).toHaveURL(/\/review\/[a-zA-Z0-9]+/);
// });

// test("Game Review: should submit a review", async ({ page, context }) => {
// // Mocking geolocation
// await context.grantPermissions(["geolocation"]);
// await page.goto("/");

// await page.route("**/games", (route) =>
// route.fulfill({
// status: 200,
// body: JSON.stringify([{ id: "1", name: "Mock Game", reviews: [] }]),
// })
// );

// await page.goto("/");
// const firstGameCard = page.locator(".grid .col-span-1 div").first();
// await firstGameCard.click();

// await page.fill('input[placeholder="Email"]', "[email protected]");
// await page.fill('input[placeholder="Name"]', "John Doe");
// await page.fill(
// 'textarea[placeholder="Here is a sample placeholder"]',
// "Nice game. Loved it."
// );

// await page.locator('fieldset.rating label[for="star4"]').click();
// await page.click("#submit-review-button");

// await expect(page.locator("text=Thank you for your review!")).toBeVisible();
// await expect(page.locator("text=John Doe")).toBeVisible();
// await expect(page.locator("text=Nice game. Loved it.")).toBeVisible();
// });

// test("Game Review: should delete review from admin panel", async ({
// page,
// }) => {
// await page.goto("/games");

// const reviewLocator = page.locator('[review-email="[email protected]"]');
// const reviewCount = await reviewLocator.count();

// for (let i = 0; i < reviewCount; i++) {
// const review = reviewLocator.nth(i);
// await review.click();
// await page.waitForTimeout(500);
// await expect(
// page.locator("text=Review deleted successfully")
// ).toBeVisible();
// }
// });
// });
24 changes: 1 addition & 23 deletions client/e2e/tests/home.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,15 @@ test("Game List: No games found", async ({ page }) => {
await expect(page.locator("text=Only Moderator can list game")).toBeVisible();
});

test("Game List: should display game cards with images, titles, and review counts", async ({
page,
}) => {
test("Game List: Check game count greater than one", async ({ page }) => {
await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: gameListResponse });
});

await page.goto("/");

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

expect(cardCount).toBeGreaterThan(1);

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

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");
});
80 changes: 80 additions & 0 deletions client/e2e/tests/moderator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { test, expect } from "@playwright/test";
import { API_ROUTES } from "../support/constants";
import { moderatorExistsResponse } from "../fixtures";

test.describe("Moderator Admin Panel", () => {
test.beforeEach(async ({ page }) => {
await page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
await route.fulfill({ json: moderatorExistsResponse });
});

await page.goto("/login");

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

await page.route(API_ROUTES.LOGIN, async (route) => {
await route.fulfill({
json: {
token: "mockToken",
},
});
});

await page.click("button");
});

test("Check admin login successfully", async ({ page }) => {
await expect(page.locator("text=Login successfully")).toBeVisible();
});

test("Check all the menu accessible", async ({ page }) => {
// Check admin URL
await expect(page).toHaveURL("/admin");

// Navigate to different menus
await page.click("#home-card");
await expect(page).toHaveURL("/");

await page.goBack();
await expect(page).toHaveURL("/admin");

await page.click("#game-list-card");
await expect(page).toHaveURL("/games");

await page.goBack();
await expect(page).toHaveURL("/admin");

await page.click("#review-in-map");
await expect(page).toHaveURL("/map");

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

test("Check logout functionality", async ({ page }) => {
// Ensure URL is admin
await expect(page).toHaveURL("/admin");

// Perform logout
await page.click("#logout-button");
await expect(page.locator("text=Logout successful")).toBeVisible();
await expect(page).toHaveURL("/login");
});
});

test("Check all the protected menu not accessible when logged out", async ({
page,
}) => {
// Try to access admin page directly
await page.goto("/admin", { waitUntil: "domcontentloaded" });
await expect(page).toHaveURL("/login");

// Try to access map page directly
await page.goto("/map", { waitUntil: "domcontentloaded" });
await expect(page).toHaveURL("/login");

// Try to access games page directly
await page.goto("/games", { waitUntil: "domcontentloaded" });
await expect(page).toHaveURL("/login");
});

0 comments on commit 63db182

Please sign in to comment.