Skip to content

Commit

Permalink
Create scoreboard components - container
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Jul 25, 2022
1 parent 8b54b4b commit 3abe5c1
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/scoreboard/components/counter/counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => (
<div className={styles.parentCounter}>{itemNumber}</div>
);
5 changes: 3 additions & 2 deletions src/components/scoreboard/components/index.ts
Original file line number Diff line number Diff line change
@@ -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";
9 changes: 9 additions & 0 deletions src/components/scoreboard/components/reset/reset.module.scss
Original file line number Diff line number Diff line change
@@ -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;
}
42 changes: 42 additions & 0 deletions src/components/scoreboard/components/reset/reset.test.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => <ResetButton onReset={() => null} />;

it("should render elements with default state", function () {
render(<ResetWithDummyHandlerOnReset />);

expect(screen.getByText("🙂")).toBeInTheDocument();
});

it("onReset handler should be called", function () {
const onReset = jest.fn();

render(<ResetButton onReset={onReset} />);
fireEvent.click(screen.getByText("🙂"));

expect(onReset).toBeCalled();
});

it("should change state when onMouseDown and onMouseUp events happened", function () {
render(<ResetWithDummyHandlerOnReset />);

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(<ResetWithDummyHandlerOnReset />);

fireEvent.mouseDown(screen.getByText("🙂"));
expect(screen.getByText("😯")).toBeInTheDocument();

fireEvent.mouseLeave(screen.getByText("😯"));
expect(screen.getByText("🙂")).toBeInTheDocument();
});
});
29 changes: 29 additions & 0 deletions src/components/scoreboard/components/reset/reset.tsx
Original file line number Diff line number Diff line change
@@ -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<ResetProps> = ({ onReset }) => {
const [mouseDown, setMouseDown] = useState(false);

const onMouseDown = () => setMouseDown(true);
const onMouseUp = () => setMouseDown(false);

return (
<button
className={styles.resetButton}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
onMouseLeave={onMouseUp}
onClick={onReset}
>
{mouseDown ? "😯" : "🙂"}
</button>
);
};
1 change: 1 addition & 0 deletions src/components/scoreboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ScoreBoard, ScoreboardProps } from "./scoreboard";
6 changes: 6 additions & 0 deletions src/components/scoreboard/scoreboard.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.wrapperScoreboard {
display: flex;
width: max-content;
padding-bottom: 2vw;
justify-content: space-between;
}
34 changes: 34 additions & 0 deletions src/components/scoreboard/scoreboard.tsx
Original file line number Diff line number Diff line change
@@ -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<ScoreboardProps> = ({ time, mines, onReset, levels }) => {
return (
<div className={styles.wrapperScoreboard}>
<Counter itemNumber={time} />
<Level levelData={levels} />
<ResetButton onReset={onReset} />
<Counter itemNumber={mines} />
</div>
);
};

0 comments on commit 3abe5c1

Please sign in to comment.