Skip to content

Commit

Permalink
updated page file names and added links between pages
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-winter committed Oct 16, 2024
1 parent 0fa5a33 commit e6fd4a2
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 77 deletions.
23 changes: 18 additions & 5 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { socket } from "./socket";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import "./App.css";

import { HomePage } from "./pages/HomePage";

import { InGame } from "./pages/InGame";
import { Lobby } from "./pages/Lobby";
import { LobbyHost } from "./pages/LobbyHost";
import { RoundEnd } from "./pages/RoundEnd";
import { LandingPlayer } from "./pages/LandingPlayer";
import { LobbyPlayer } from "./pages/LobbyPlayer";
import { LandingHost } from "./pages/LandingHost";

// router

Expand Down Expand Up @@ -41,15 +44,25 @@ function App() {
const router = createBrowserRouter([
{
path: "/",
element: <HomePage gameRoom={gameRoom}/>,
element: <LandingHost w/>,
},
{
path: "/lobby/player",
element: <LobbyPlayer gameRoom={gameRoom}/>
},

{
path: "/join/:roomId",
element: <LandingPlayer />
},

{
path: "/in-game",
element: <InGame />,
},
{
path: "/lobby",
element: <Lobby />,
path: "/lobby/host",
element: <LobbyHost gameRoom={gameRoom}/>,
},
{
path: "/round-end",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
//imports needed
import { socket } from "../socket";
import { Button } from "../components/Button";
import { useNavigate } from "react-router-dom";

// page function
export function HomePage({ gameRoom }) {
export function LandingHost() {
const navigate = useNavigate()
const handleClick = () => {
socket.emit("create_room");
navigate('/lobby/host')

};
return (
<>
Expand All @@ -21,7 +25,6 @@ export function HomePage({ gameRoom }) {
</ul>
</p>
<Button handleClick={handleClick} buttonText="Create Game"></Button>
{gameRoom && <div data-testid="game-link">Game Link: {`${window.location.href}join/${gameRoom}`}</div>}
</div>
</>
);
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/pages/LandingPlayer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Button } from "../components/Button"
import { useNavigate } from "react-router-dom"

export function LandingPlayer() {
const navigate = useNavigate()
const handleClick = () => {
navigate('/lobby/player')
}
return (
<>
<Button handleClick={handleClick} buttonText="Join Room"/>
</>
)
}
35 changes: 0 additions & 35 deletions frontend/src/pages/Lobby.jsx

This file was deleted.

42 changes: 42 additions & 0 deletions frontend/src/pages/LobbyHost.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// The page where the players are waiting while everybody joins the game
// For the host, this is the page with the 'start the game' button

import { Link } from "react-router-dom";
import { Button } from "../components/Button";
{/* import { Button } from "../components/Button"; */}

export function LobbyHost({ gameRoom }) {
return (
<>
<Button buttonText="Start Game"/>
<div>{`Game Room: http://localhost:5173/join/${gameRoom}`}</div>
</>
)
// return (
// <div className="lobby">
// <div className="players_list">
// <h1>Players joining component placeholder</h1>
// <p>
// Players
// <ul>
// <li>Player1</li>
// <li>Player2</li>
// <li>Player3</li>
// </ul>
// </p>
// </div>

// <Link to="/in-game" className="in-game-link"></Link>
// {/* <Button onClick={handleClick}>Start game</Button> */}

// <div className="share_link">
// <h1>Share link component placeholder</h1>
// <p data-testid="add-players-prompt">
// Add up to 6 players using this link
// </p>
// <a> Link</a>
// </div>

// </div>
// );
}
8 changes: 8 additions & 0 deletions frontend/src/pages/LobbyPlayer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function LobbyPlayer({ gameRoom }) {
return (
<>
<div >Waiting for host to start game...</div>
<div>{`Game Room: http://localhost:5173/join/${gameRoom}`}</div>
</>
)
}
2 changes: 1 addition & 1 deletion frontend/src/pages/RoundEnd.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function RoundEnd() {
</div>


<Link to="/" className="homepage-link"></Link>
<Link to="/" className="landing-host-link"></Link>
<Link to="/in-game" className="in-game-link"></Link>
{/* <Button onClick={handleClick}>Next round :D</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@

//required imports
import { render, screen } from "@testing-library/react";
import { HomePage } from "../../src/pages/HomePage";
import { LandingHost } from "../../src/pages/LandingHost";
import { describe, expect, test, vi } from "vitest";
import userEvent from "@testing-library/user-event";
import { socket } from "../../src/socket";
import { useNavigate } from "react-router-dom";

vi.mock("../../src/socket.js", () => {
return { socket: { emit: vi.fn() } };
});

describe("Homepage tests", () => {
vi.mock("react-router-dom", () => {
const navigateMock = vi.fn()
const useNavigateMock = () => navigateMock
return { useNavigate: useNavigateMock}
})



describe("LandingHost tests", () => {
test("name of the game", () => {
render(<HomePage gameRoom={""} />);
render(<LandingHost />);
const heading = screen.getByTestId("game-name");
expect(heading.textContent).toEqual("Guess the number!");
});
test("rules of the game", () => {
render(<HomePage gameRoom={""} />);
render(<LandingHost />);
expect(screen.getByText("Rules of the game:")).toBeTruthy();
expect(screen.getByText("Guess the number between 1 and 100")).toBeTruthy();
expect(screen.getByText("2 to 6 players")).toBeTruthy();
});
test("create game button appears on page", () => {
render(<HomePage gameRoom={""} />);
render(<LandingHost />);
const buttonEl = screen.getByRole("button");
expect(buttonEl.textContent).toEqual("Create Game");
});
test('when a user clicks on the button the socket emits to "create_room"', async () => {
render(<HomePage gameRoom={""} />);
render(<LandingHost />);
const buttonEl = screen.getByRole("button");
const user = userEvent.setup();
await user.click(buttonEl);
expect(socket.emit).toHaveBeenCalledWith("create_room");
});
test("when no link is passed into the component it is not visible", () => {
render(<HomePage gameRoom={""} />);
render(<LandingHost />);
const link = screen.queryByTestId("game-link");
expect(link).toBeNull();
});
test("given a link passed into the components", () => {
render(<HomePage gameRoom={"abc123"} />);
const link = screen.getByTestId("game-link");
expect(link.textContent).toEqual("Game Link: http://localhost:3000/join/abc123");
});
});
21 changes: 21 additions & 0 deletions frontend/tests/pages/LandingPlayer.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Tests for the landing page where the players can share a link to join
// the game, and the players can join the game

//required imports
import { render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { describe, expect, test } from "vitest";
import { LandingPlayer } from "../../src/pages/LandingPlayer";


describe("LandingPlayer tests", () => {
test("join room button is visible", () => {
render(
<BrowserRouter>
<LandingPlayer />
</BrowserRouter>
);
const buttonEl = screen.getByRole("button")
expect(buttonEl.textContent).toEqual("Join Room")
});
});
22 changes: 0 additions & 22 deletions frontend/tests/pages/Lobby.test.jsx

This file was deleted.

22 changes: 22 additions & 0 deletions frontend/tests/pages/LobbyHost.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Tests for the lobby page where the host can share a link to join
// the game, the host can start the game, and the host can see who has joined

//required imports
import { render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { LobbyHost } from "../../src/pages/LobbyHost";
import { describe, expect, test } from "vitest";


describe("LobbyHost tests", () => {
test("there is a button", () => {
render(
<BrowserRouter>
<LobbyHost gameRoom={""}/>
</BrowserRouter>
);

const buttonEl = screen.getByRole("button");
expect(buttonEl.textContent).toEqual("Start Game");
});
});
21 changes: 21 additions & 0 deletions frontend/tests/pages/LobbyPlayer.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Tests for the lobby page where the players can share a link to join
// the game, and the players can see who has joined

//required imports
import { render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { LobbyPlayer } from "../../src/pages/LobbyPlayer";
import { describe, expect, test } from "vitest";


describe("LobbyPlayer tests", () => {
test("name of the game", () => {
render(
<BrowserRouter>
<LobbyPlayer gameRoom={""}/>
</BrowserRouter>
);

expect(screen.getByText("Waiting for host to start game...")).toBeTruthy();
});
});

0 comments on commit e6fd4a2

Please sign in to comment.