diff --git a/api/app.js b/api/app.js index bd17718..1633f90 100644 --- a/api/app.js +++ b/api/app.js @@ -5,6 +5,7 @@ const http = require('http') const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); +const crypto = require("crypto") const app = express(); @@ -51,6 +52,12 @@ io.on('connection', (socket) => { console.log("User disconnected!") console.log(`Users Connected: ${io.engine.clientsCount}`) }) + + socket.on("create_room", () => { + const roomId = crypto.randomBytes(3).toString('hex') + const link = `http://localhost:5173/join/${roomId}` + socket.emit('receive_link', link) + }) }) server.listen(3001, () => { diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index f51ce5f..ebc1424 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -10,46 +10,53 @@ import { Lobby } from "./pages/Lobby"; import { RoundEnd } from "./pages/RoundEnd"; // router -const router = createBrowserRouter([ - { - path: "/", - element: , - }, - { - path: "/in-game", - element: , - }, - { - path: "/lobby", - element: , - }, - { - path: "/round-end", - element: , - } -]); function App() { - + const [isConnected, setIsConnected] = useState(socket.connected) - + const [gameLink, setGameLink] = useState("") + useEffect(() => { const onConnect = () => { setIsConnected(true) } - const onDisconnect = () => { setIsConnected(false) } - + const onReceiveLink = (data) => { + setGameLink(data) + } + socket.on('connect', onConnect) socket.on('disconnect', onDisconnect) - + socket.on('receive_link', (data) => onReceiveLink(data)) + return () => { socket.off('connect', onConnect) socket.off('disconnect', onDisconnect) + socket.off('receive_link', () => onReceiveLink("")) } }) + + const router = createBrowserRouter([ + { + path: "/", + element: , + }, + { + path: "/in-game", + element: , + }, + { + path: "/lobby", + element: , + }, + { + path: "/round-end", + element: , + } + ]); + return ( <> diff --git a/frontend/src/pages/HomePage.jsx b/frontend/src/pages/HomePage.jsx index 4db776c..78f3d3e 100644 --- a/frontend/src/pages/HomePage.jsx +++ b/frontend/src/pages/HomePage.jsx @@ -1,26 +1,29 @@ // the landing page with the create a game option //imports needed -import { Link } from "react-router-dom"; -{/*import { Button } from "../components/Button"; */} +import { socket } from "../socket"; +import { Button } from "../components/Button"; // page function -export function HomePage() { +export function HomePage({gameLink}) { + + const handleClick = () => { + socket.emit("create_room"); + }; return ( <> -
+

Guess the number!

- Rules of the game: -

    -
  • Guess the number between 1 and 100
  • -
  • 2 to 6 players
  • -
+ Rules of the game: +
    +
  • Guess the number between 1 and 100
  • +
  • 2 to 6 players
  • +

- - {/* */} - -
+ + {gameLink &&
Game Link: {gameLink}
} +
); } \ No newline at end of file diff --git a/frontend/src/socket.js b/frontend/src/socket.js index ce2528d..f8f9086 100644 --- a/frontend/src/socket.js +++ b/frontend/src/socket.js @@ -1,5 +1,6 @@ import { io } from "socket.io-client"; + const SOCKET_URL = import.meta.env.VITE_SOCKET_BACKEND_URL; console.log("SOCKET_URL",SOCKET_URL) -export const socket = io(SOCKET_URL) +export const socket = io("http://localhost:3001") diff --git a/frontend/tests/pages/HomePage.test.jsx b/frontend/tests/pages/HomePage.test.jsx index c84e9aa..cb4c5e4 100644 --- a/frontend/tests/pages/HomePage.test.jsx +++ b/frontend/tests/pages/HomePage.test.jsx @@ -2,19 +2,47 @@ //required imports import { render, screen } from "@testing-library/react"; -import { BrowserRouter } from "react-router-dom"; import { HomePage } from "../../src/pages/HomePage"; -import { describe, expect, test } from "vitest"; +import { describe, expect, test, vi } from "vitest"; +import userEvent from "@testing-library/user-event"; +import { socket } from "../../src/socket"; -describe("Homepage tests", () => { - test("name of the game", () => { - render( - - - - ); - - const heading = screen.getByTestId("game-name"); - expect(heading.textContent).toEqual("Guess the number!"); - }); -}); \ No newline at end of file +vi.mock("../../src/socket.js", () => { + return { socket: { emit: vi.fn() } }; +}); + +describe("Homepage tests", () => { + test("name of the game", () => { + render(); + const heading = screen.getByTestId("game-name"); + expect(heading.textContent).toEqual("Guess the number!"); + }); + test("rules of the game", () => { + render(); + 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(); + 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(); + 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(); + const link = screen.queryByTestId("game-link"); + expect(link).toBeNull(); + }); + test("given a link passed into the components", () => { + render(); + const link = screen.getByTestId("game-link"); + expect(link.textContent).toEqual("Game Link: http://example.com"); + }); +});