|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +test.describe('Game Listing and Navigation', () => { |
| 4 | + test('should display games with titles on index page', async ({ page }) => { |
| 5 | + await page.goto('/'); |
| 6 | + |
| 7 | + // Wait for the games to load |
| 8 | + await page.waitForSelector('[data-testid="games-grid"]', { timeout: 10000 }); |
| 9 | + |
| 10 | + // Check that games are displayed |
| 11 | + const gameCards = page.locator('[data-testid="game-card"]'); |
| 12 | + |
| 13 | + // Wait for at least one game card to be visible |
| 14 | + await expect(gameCards.first()).toBeVisible(); |
| 15 | + |
| 16 | + // Check that we have at least one game |
| 17 | + const gameCount = await gameCards.count(); |
| 18 | + expect(gameCount).toBeGreaterThan(0); |
| 19 | + |
| 20 | + // Check that each game card has a title |
| 21 | + const firstGameCard = gameCards.first(); |
| 22 | + await expect(firstGameCard.locator('[data-testid="game-title"]')).toBeVisible(); |
| 23 | + |
| 24 | + // Verify that game titles are not empty |
| 25 | + const gameTitle = await firstGameCard.locator('[data-testid="game-title"]').textContent(); |
| 26 | + expect(gameTitle?.trim()).toBeTruthy(); |
| 27 | + }); |
| 28 | + |
| 29 | + test('should navigate to correct game details page when clicking on a game', async ({ page }) => { |
| 30 | + await page.goto('/'); |
| 31 | + |
| 32 | + // Wait for games to load |
| 33 | + await page.waitForSelector('[data-testid="games-grid"]', { timeout: 10000 }); |
| 34 | + |
| 35 | + // Get the first game card and its data attributes |
| 36 | + const firstGameCard = page.locator('[data-testid="game-card"]').first(); |
| 37 | + const gameId = await firstGameCard.getAttribute('data-game-id'); |
| 38 | + const gameTitle = await firstGameCard.getAttribute('data-game-title'); |
| 39 | + |
| 40 | + // Click on the first game |
| 41 | + await firstGameCard.click(); |
| 42 | + |
| 43 | + // Verify we're on the correct game details page |
| 44 | + await expect(page).toHaveURL(`/game/${gameId}`); |
| 45 | + |
| 46 | + // Verify the game details page loads |
| 47 | + await page.waitForSelector('[data-testid="game-details"]', { timeout: 10000 }); |
| 48 | + |
| 49 | + // Verify the title matches what we clicked on |
| 50 | + const detailsTitle = page.locator('[data-testid="game-details-title"]'); |
| 51 | + await expect(detailsTitle).toHaveText(gameTitle || ''); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should display game details with all required information', async ({ page }) => { |
| 55 | + // Navigate to a specific game (we'll use game ID 1 as an example) |
| 56 | + await page.goto('/game/1'); |
| 57 | + |
| 58 | + // Wait for game details to load |
| 59 | + await page.waitForSelector('[data-testid="game-details"]', { timeout: 10000 }); |
| 60 | + |
| 61 | + // Check that the game title is present and not empty |
| 62 | + const gameTitle = page.locator('[data-testid="game-details-title"]'); |
| 63 | + await expect(gameTitle).toBeVisible(); |
| 64 | + const titleText = await gameTitle.textContent(); |
| 65 | + expect(titleText?.trim()).toBeTruthy(); |
| 66 | + |
| 67 | + // Check that the game description is present and not empty |
| 68 | + const gameDescription = page.locator('[data-testid="game-details-description"]'); |
| 69 | + await expect(gameDescription).toBeVisible(); |
| 70 | + const descriptionText = await gameDescription.textContent(); |
| 71 | + expect(descriptionText?.trim()).toBeTruthy(); |
| 72 | + |
| 73 | + // Check that either publisher or category (or both) are present |
| 74 | + const publisherExists = await page.locator('[data-testid="game-details-publisher"]').isVisible(); |
| 75 | + const categoryExists = await page.locator('[data-testid="game-details-category"]').isVisible(); |
| 76 | + expect(publisherExists && categoryExists).toBeTruthy(); |
| 77 | + |
| 78 | + // If publisher exists, check it has content |
| 79 | + if (publisherExists) { |
| 80 | + const publisherText = await page.locator('[data-testid="game-details-publisher"]').textContent(); |
| 81 | + expect(publisherText?.trim()).toBeTruthy(); |
| 82 | + } |
| 83 | + |
| 84 | + // If category exists, check it has content |
| 85 | + if (categoryExists) { |
| 86 | + const categoryText = await page.locator('[data-testid="game-details-category"]').textContent(); |
| 87 | + expect(categoryText?.trim()).toBeTruthy(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + test('should display a button to back the game', async ({ page }) => { |
| 92 | + await page.goto('/game/1'); |
| 93 | + |
| 94 | + // Wait for game details to load |
| 95 | + await page.waitForSelector('[data-testid="game-details"]', { timeout: 10000 }); |
| 96 | + |
| 97 | + // Check that the back game button is present |
| 98 | + const backButton = page.locator('[data-testid="back-game-button"]'); |
| 99 | + await expect(backButton).toBeVisible(); |
| 100 | + await expect(backButton).toContainText('Support This Game'); |
| 101 | + |
| 102 | + // Verify the button is clickable |
| 103 | + await expect(backButton).toBeEnabled(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('should be able to navigate back to home from game details', async ({ page }) => { |
| 107 | + await page.goto('/game/1'); |
| 108 | + |
| 109 | + // Wait for the page to load |
| 110 | + await page.waitForSelector('[data-testid="game-details"]', { timeout: 10000 }); |
| 111 | + |
| 112 | + // Find and click the back to all games link |
| 113 | + const backLink = page.locator('a:has-text("Back to all games")'); |
| 114 | + await expect(backLink).toBeVisible(); |
| 115 | + await backLink.click(); |
| 116 | + |
| 117 | + // Verify we're back on the home page |
| 118 | + await expect(page).toHaveURL('/'); |
| 119 | + await page.waitForSelector('[data-testid="games-grid"]', { timeout: 10000 }); |
| 120 | + }); |
| 121 | + |
| 122 | + test('should handle navigation to non-existent game gracefully', async ({ page }) => { |
| 123 | + // Navigate to a game that doesn't exist |
| 124 | + await page.goto('/game/99999'); |
| 125 | + |
| 126 | + // The page should load without crashing |
| 127 | + // Check if there's an error message or if it handles gracefully |
| 128 | + await page.waitForTimeout(3000); |
| 129 | + |
| 130 | + // The page should either show an error or handle it gracefully |
| 131 | + // We expect the page to not crash and still have a valid title |
| 132 | + await expect(page).toHaveTitle(/Game Details - Tailspin Toys/); |
| 133 | + }); |
| 134 | +}); |
0 commit comments