Skip to content

Commit 490de06

Browse files
Merge pull request steam-bell-92#1087 from nishtha-agarwal-211/feat-tic-tac-toe-e2e-tests
test: add playwright e2e tests for tic-tac-toe game
2 parents 6f298a1 + 58a0227 commit 490de06

2 files changed

Lines changed: 144 additions & 2 deletions

File tree

web-app/js/projects/tic-tac-toe.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,11 @@ function initTicTacToe() {
751751
if (win.sym === "X") scores.p1++; else scores.p2++;
752752
syncScoreboard();
753753
const winner = win.sym === "X" ? p1 : p2;
754-
showResult(`${winner} wins this round!`);
754+
showResult("🏆", `${winner} wins this round!`);
755755
} else {
756756
scores.draws++;
757757
syncScoreboard();
758-
showResult( "It's a draw!");
758+
showResult("🤝", "It's a draw!");
759759
}
760760
}
761761

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
const { test, expect } = require('@playwright/test');
2+
3+
test.describe('Tic Tac Toe E2E Game', () => {
4+
test.beforeEach(async ({ page }) => {
5+
// 1. Navigate to home
6+
await page.goto('/');
7+
8+
// 2. Open the projects section (which displays card play buttons)
9+
const exploreBtn = page.locator('#exploreBtn');
10+
await exploreBtn.click();
11+
12+
// 3. Find and click "Try It" for Tic Tac Toe
13+
const playBtn = page.locator('.btn-play[data-project="tic-tac-toe"]');
14+
await expect(playBtn).toBeVisible();
15+
await playBtn.click();
16+
17+
// 4. Verify modal is active
18+
const modal = page.locator('#projectModal');
19+
await expect(modal).toHaveClass(/active/);
20+
});
21+
22+
test('should setup players, play a game, detect a win, and handle rematch/reset', async ({ page }) => {
23+
const modalBody = page.locator('#modalBody');
24+
25+
// Verify Setup Screen is active initially
26+
const setupScreen = modalBody.locator('#screen-setup');
27+
await expect(setupScreen).toBeVisible();
28+
29+
// Select game mode (Two Players should be default, but let's click to be sure)
30+
const mode2p = setupScreen.locator('#mode-group button[data-val="2p"]');
31+
await mode2p.click();
32+
33+
// Select Round: 1 Round (to quickly end match for testing)
34+
const round1Pill = setupScreen.locator('#rounds-group button[data-val="1"]');
35+
await round1Pill.click();
36+
37+
// Fill player names
38+
const p1Input = setupScreen.locator('#p1-input');
39+
const p2Input = setupScreen.locator('#p2-input');
40+
await p1Input.fill('Alice');
41+
await p2Input.fill('Bob');
42+
43+
// Start the game
44+
const startBtn = setupScreen.locator('#start-btn');
45+
await startBtn.click();
46+
47+
// Verify game screen is visible
48+
const gameScreen = modalBody.locator('#screen-game');
49+
await expect(gameScreen).toBeVisible();
50+
51+
// Verify player names on the scoreboard
52+
await expect(gameScreen.locator('#sn1')).toHaveText('Alice');
53+
await expect(gameScreen.locator('#sn2')).toHaveText('Bob');
54+
55+
// Verify first turn belongs to Alice (X)
56+
await expect(gameScreen.locator('#turn-name')).toHaveText('Alice');
57+
await expect(gameScreen.locator('#turn-sym')).toHaveText('X');
58+
59+
// Let's play the moves to win the round/match
60+
// Alice (X) plays: 0, 1, 2 (Top row win)
61+
// Bob (O) plays: 3, 4
62+
const cell0 = gameScreen.locator('.cell[data-i="0"]');
63+
const cell1 = gameScreen.locator('.cell[data-i="1"]');
64+
const cell2 = gameScreen.locator('.cell[data-i="2"]');
65+
const cell3 = gameScreen.locator('.cell[data-i="3"]');
66+
const cell4 = gameScreen.locator('.cell[data-i="4"]');
67+
68+
// Move 1: Alice clicks cell 0
69+
await cell0.click();
70+
await expect(cell0).toHaveText('X');
71+
72+
// Move 2: Bob clicks cell 3
73+
await cell3.click();
74+
await expect(cell3).toHaveText('O');
75+
76+
// Move 3: Alice clicks cell 1
77+
await cell1.click();
78+
await expect(cell1).toHaveText('X');
79+
80+
// Move 4: Bob clicks cell 4
81+
await cell4.click();
82+
await expect(cell4).toHaveText('O');
83+
84+
// Move 5: Alice clicks cell 2 -> Alice wins!
85+
await cell2.click();
86+
await expect(cell2).toHaveText('X');
87+
88+
// Verify Result Overlay is displayed
89+
const resultOverlay = modalBody.locator('#result-overlay');
90+
await expect(resultOverlay).toHaveClass(/show/);
91+
92+
// Verify winner text and emoji
93+
const resEmoji = modalBody.locator('#res-emoji');
94+
const resText = modalBody.locator('#res-text');
95+
await expect(resEmoji).toHaveText('🏆');
96+
await expect(resText).toHaveText('Alice wins this round!');
97+
98+
// Since it's a 1-round match, next button should say "See Results →"
99+
const nextBtn = modalBody.locator('#next-btn');
100+
await expect(nextBtn).toHaveText('See Results →');
101+
102+
// Click next button to proceed to final screen
103+
await nextBtn.click();
104+
105+
// Verify Final Screen is visible
106+
const finalScreen = modalBody.locator('#screen-final');
107+
await expect(finalScreen).toBeVisible();
108+
109+
// Verify final title
110+
const finalTitle = finalScreen.locator('#final-title');
111+
await expect(finalTitle).toContainText('Alice Wins the Match!');
112+
113+
// Click rematch button
114+
const rematchBtn = finalScreen.locator('#rematch-btn');
115+
await rematchBtn.click();
116+
117+
// Verify we are back to the game screen with reset scoreboard
118+
await expect(gameScreen).toBeVisible();
119+
await expect(gameScreen.locator('#sv1')).toHaveText('0');
120+
await expect(gameScreen.locator('#sv2')).toHaveText('0');
121+
await expect(gameScreen.locator('#svd')).toHaveText('0');
122+
});
123+
124+
test('should allow navigating back to menu', async ({ page }) => {
125+
const modalBody = page.locator('#modalBody');
126+
const gameScreen = modalBody.locator('#screen-game');
127+
const setupScreen = modalBody.locator('#screen-setup');
128+
129+
// Start a quick game
130+
await modalBody.locator('#start-btn').click();
131+
132+
// Verify game screen
133+
await expect(gameScreen).toBeVisible();
134+
135+
// Click back/menu button
136+
const backBtn = gameScreen.locator('#back-btn');
137+
await backBtn.click();
138+
139+
// Verify we are back to setup screen
140+
await expect(setupScreen).toBeVisible();
141+
});
142+
});

0 commit comments

Comments
 (0)