From 3abe5c1f3fb3b1cd4e081d95326e1931a4828c6d Mon Sep 17 00:00:00 2001 From: Muhammad Zourdy Date: Mon, 25 Jul 2022 07:14:14 +0200 Subject: [PATCH] Create scoreboard components - container --- .../scoreboard/components/counter/counter.tsx | 2 +- src/components/scoreboard/components/index.ts | 5 ++- .../components/reset/reset.module.scss | 9 ++++ .../components/reset/reset.test.tsx | 42 +++++++++++++++++++ .../scoreboard/components/reset/reset.tsx | 29 +++++++++++++ src/components/scoreboard/index.ts | 1 + .../scoreboard/scoreboard.module.scss | 6 +++ src/components/scoreboard/scoreboard.tsx | 34 +++++++++++++++ 8 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/components/scoreboard/components/reset/reset.module.scss create mode 100644 src/components/scoreboard/components/reset/reset.test.tsx create mode 100644 src/components/scoreboard/components/reset/reset.tsx create mode 100644 src/components/scoreboard/index.ts create mode 100644 src/components/scoreboard/scoreboard.module.scss create mode 100644 src/components/scoreboard/scoreboard.tsx diff --git a/src/components/scoreboard/components/counter/counter.tsx b/src/components/scoreboard/components/counter/counter.tsx index 188b8330..a8dc2eda 100644 --- a/src/components/scoreboard/components/counter/counter.tsx +++ b/src/components/scoreboard/components/counter/counter.tsx @@ -2,6 +2,6 @@ import React, { FunctionComponent } from "react"; import styles from "./counter.module.scss"; -export const Counter: FunctionComponent<{ itemNumber: number }> = ({ itemNumber }) => ( +export const Counter: FunctionComponent<{ itemNumber: string }> = ({ itemNumber }) => (
{itemNumber}
); diff --git a/src/components/scoreboard/components/index.ts b/src/components/scoreboard/components/index.ts index 1cd14940..ee4b2cef 100644 --- a/src/components/scoreboard/components/index.ts +++ b/src/components/scoreboard/components/index.ts @@ -1,2 +1,3 @@ -export * from "./components/counter/counter"; -export * from "./components/level"; +export * from "./counter/counter"; +export * from "./level/level"; +export * from "./reset/reset"; diff --git a/src/components/scoreboard/components/reset/reset.module.scss b/src/components/scoreboard/components/reset/reset.module.scss new file mode 100644 index 00000000..f2317bb4 --- /dev/null +++ b/src/components/scoreboard/components/reset/reset.module.scss @@ -0,0 +1,9 @@ +.resetButton { + font-size: 1.5vw; + cursor: pointer; + font-weight: 700; + border-width: 0.15vw; + border-style: solid; + background-color: #d1d1d1; + border-color: white #9e9e9e #9e9e9e white; +} diff --git a/src/components/scoreboard/components/reset/reset.test.tsx b/src/components/scoreboard/components/reset/reset.test.tsx new file mode 100644 index 00000000..24134300 --- /dev/null +++ b/src/components/scoreboard/components/reset/reset.test.tsx @@ -0,0 +1,42 @@ +import { ResetButton } from "@components/scoreboard/components/reset/reset"; +import { fireEvent, render, screen } from "@testing-library/react"; +import React, { FunctionComponent } from "react"; + +describe("Reset button test", function () { + const ResetWithDummyHandlerOnReset: FunctionComponent = () => null} />; + + it("should render elements with default state", function () { + render(); + + expect(screen.getByText("🙂")).toBeInTheDocument(); + }); + + it("onReset handler should be called", function () { + const onReset = jest.fn(); + + render(); + fireEvent.click(screen.getByText("🙂")); + + expect(onReset).toBeCalled(); + }); + + it("should change state when onMouseDown and onMouseUp events happened", function () { + render(); + + fireEvent.mouseDown(screen.getByText("🙂")); + expect(screen.getByText("😯")).toBeInTheDocument(); + + fireEvent.mouseUp(screen.getByText("😯")); + expect(screen.getByText("🙂")).toBeInTheDocument(); + }); + + it("should change state when onMouseDown and onMouseLeave events happened", function () { + render(); + + fireEvent.mouseDown(screen.getByText("🙂")); + expect(screen.getByText("😯")).toBeInTheDocument(); + + fireEvent.mouseLeave(screen.getByText("😯")); + expect(screen.getByText("🙂")).toBeInTheDocument(); + }); +}); diff --git a/src/components/scoreboard/components/reset/reset.tsx b/src/components/scoreboard/components/reset/reset.tsx new file mode 100644 index 00000000..f78f3de9 --- /dev/null +++ b/src/components/scoreboard/components/reset/reset.tsx @@ -0,0 +1,29 @@ +import React, { FunctionComponent, useState } from "react"; + +import styles from "./reset.module.scss"; + +export interface ResetProps { + /** + * Reset action handler + */ + onReset: () => void; +} + +export const ResetButton: FunctionComponent = ({ onReset }) => { + const [mouseDown, setMouseDown] = useState(false); + + const onMouseDown = () => setMouseDown(true); + const onMouseUp = () => setMouseDown(false); + + return ( + + ); +}; diff --git a/src/components/scoreboard/index.ts b/src/components/scoreboard/index.ts new file mode 100644 index 00000000..e48483f0 --- /dev/null +++ b/src/components/scoreboard/index.ts @@ -0,0 +1 @@ +export { ScoreBoard, ScoreboardProps } from "./scoreboard"; diff --git a/src/components/scoreboard/scoreboard.module.scss b/src/components/scoreboard/scoreboard.module.scss new file mode 100644 index 00000000..484e6a70 --- /dev/null +++ b/src/components/scoreboard/scoreboard.module.scss @@ -0,0 +1,6 @@ +.wrapperScoreboard { + display: flex; + width: max-content; + padding-bottom: 2vw; + justify-content: space-between; +} diff --git a/src/components/scoreboard/scoreboard.tsx b/src/components/scoreboard/scoreboard.tsx new file mode 100644 index 00000000..06f90c7a --- /dev/null +++ b/src/components/scoreboard/scoreboard.tsx @@ -0,0 +1,34 @@ +import React, { FunctionComponent } from "react"; + +import { Counter, Level, ResetButton } from "./components"; +import styles from "./scoreboard.module.scss"; + +export interface ScoreboardProps { + /** + * Timer + */ + time: string; + /** + * Possible game scenarios + */ + levels: string[]; + /** + * Action handler when the GameReset button is clicked + */ + onReset: () => void; + /** + * Bombs in the field + */ + mines: string; +} + +export const ScoreBoard: FunctionComponent = ({ time, mines, onReset, levels }) => { + return ( +
+ + + + +
+ ); +};