From ebcc5f5c17ac1d2e6f1202157b7da804468d7c2e Mon Sep 17 00:00:00 2001 From: Joe Winter Date: Thu, 24 Oct 2024 14:21:48 +0100 Subject: [PATCH 1/2] e2e --- api/numberGame/Pokemon.js | 1 + frontend/src/components/GuessForm.jsx | 1 - tests/inGame.spec.js | 31 +++++++++++++++++++++++++++ tests/playerLobby.spec.js | 6 ++++++ tests/utils.js | 13 ++++++----- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 tests/inGame.spec.js diff --git a/api/numberGame/Pokemon.js b/api/numberGame/Pokemon.js index 168e2e7..2df2c97 100644 --- a/api/numberGame/Pokemon.js +++ b/api/numberGame/Pokemon.js @@ -10,6 +10,7 @@ class Pokemon { try { const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`) const pokemon = await response.json() + console.log("api call:", this._extractInfo(pokemon)) return this._extractInfo(pokemon) } catch(error) { console.log("Error fetching pokemon, re-fetching!", error) diff --git a/frontend/src/components/GuessForm.jsx b/frontend/src/components/GuessForm.jsx index 3062128..db23c4e 100644 --- a/frontend/src/components/GuessForm.jsx +++ b/frontend/src/components/GuessForm.jsx @@ -6,7 +6,6 @@ export function GuessForm(props) {

- props.setInput(e.target.value)} id="website-admin" className="rounded-lg bg-gray-50 border border-gray-300 text-gray-900 focus:ring-blue-500 focus:border-blue-500 block flex-1 min-w-0 w-full text-sm p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Your guess">
diff --git a/tests/inGame.spec.js b/tests/inGame.spec.js new file mode 100644 index 0000000..bded082 --- /dev/null +++ b/tests/inGame.spec.js @@ -0,0 +1,31 @@ +const { test, expect, chromium } = require("@playwright/test"); +const Utils = require("./utils") + +const utils = new Utils() + +test.describe("In game page", () => { + let playerPage; + let context; + let browser; + + test.beforeEach(async ({ page }) => { + await page.goto("/"); + + await page.getByPlaceholder("Username").fill("Joe"); + await page.locator("button").click(); + + browser = await chromium.launch(); + context = await browser.newContext(); + playerPage = await context.newPage(); + }); + + test.afterEach(async () => { + await context.close(); + await browser.close(); + }); + + test("mocks a pokemon and doesnt call api", async ({page}) => { + + expect(screen.getByText("Hello Please Work!!!!!!")) +}); +}) \ No newline at end of file diff --git a/tests/playerLobby.spec.js b/tests/playerLobby.spec.js index 007382b..40c8221 100644 --- a/tests/playerLobby.spec.js +++ b/tests/playerLobby.spec.js @@ -33,4 +33,10 @@ test.describe("Player Lobby Page", () => { await expect(page.getByText("Simon")).toBeVisible(); await expect(page.getByText("Joe (Host)")).toBeVisible(); }); + test("when a host starts a game, the player is directed to in-game", async ({page}) => { + await utils.newPlayerJoinGame(page, playerPage, "Simon"); + await utils.hostStartsGame(page) + await expect(playerPage).toHaveURL("/in-game"); + + }) }); diff --git a/tests/utils.js b/tests/utils.js index 8172512..3c52b30 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -1,17 +1,20 @@ -const { expect} = require("@playwright/test"); +const { expect } = require("@playwright/test"); class Utils { - async newPlayerJoinGame(page, newPage, name) { // host shares link const gameLink = await page.getByTestId("game-link").textContent(); // player goes to host's link - await newPage.goto(gameLink) - await expect(newPage).toHaveURL(gameLink) + await newPage.goto(gameLink); + await expect(newPage).toHaveURL(gameLink); // player enters name and clicks join room await newPage.getByPlaceholder("Username").fill(name); await newPage.locator("button").click(); } + + async hostStartsGame(page) { + await page.getByRole("button", { name: "Start Game" }).click(); + } } -module.exports = Utils \ No newline at end of file +module.exports = Utils; From 9c09422f09f85ed68d8ae668460b417257dba592 Mon Sep 17 00:00:00 2001 From: Joe Winter <130766060+aulus-plautius@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:22:18 +0100 Subject: [PATCH 2/2] Update Pokemon.js --- api/numberGame/Pokemon.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/numberGame/Pokemon.js b/api/numberGame/Pokemon.js index 2df2c97..a448046 100644 --- a/api/numberGame/Pokemon.js +++ b/api/numberGame/Pokemon.js @@ -10,7 +10,6 @@ class Pokemon { try { const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`) const pokemon = await response.json() - console.log("api call:", this._extractInfo(pokemon)) return this._extractInfo(pokemon) } catch(error) { console.log("Error fetching pokemon, re-fetching!", error) @@ -27,4 +26,4 @@ class Pokemon { } } -module.exports = Pokemon \ No newline at end of file +module.exports = Pokemon