Skip to content

Commit

Permalink
Add test cases for scoreboard components
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Aug 5, 2022
1 parent d25ffd1 commit 80af0f2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Level component test case should render level correctly 1`] = `
<DocumentFragment>
<select
class="selectLevel"
role="select-component"
>
<option
class="optionLevel"
>
beginner
</option>
<option
class="optionLevel"
>
intermediate
</option>
<option
class="optionLevel"
>
expert
</option>
</select>
</DocumentFragment>
`;
23 changes: 23 additions & 0 deletions src/components/scoreboard/components/level/level.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Level } from "@components/scoreboard/components";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";

describe("Level component test case", function () {
it("should render level correctly", function () {
const onChange = jest.fn();
const levelData = ["beginner", "intermediate", "expert"];
const { asFragment } = render(<Level levelData={levelData} onChange={onChange} />);

expect(asFragment()).toMatchSnapshot();
});

it("should Select level behaviour", function () {
const onChange = jest.fn();
const levelData = ["beginner", "intermediate", "expert"];
render(<Level levelData={levelData} onChange={onChange} />);

userEvent.selectOptions(screen.getByRole("select-component"), "intermediate");
expect(screen.getByRole("option", { name: "intermediate" })).toBeEnabled();
});
});
9 changes: 6 additions & 3 deletions src/components/scoreboard/components/level/level.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React, { FunctionComponent } from "react";
import React, { ChangeEvent, FunctionComponent } from "react";

import styles from "./level.module.scss";

export const Level: FunctionComponent<{ levelData: string[] }> = ({ levelData }) => (
<select className={styles.selectLevel}>
export const Level: FunctionComponent<{
levelData: string[];
onChange: (event: ChangeEvent<HTMLSelectElement>) => void;
}> = ({ levelData, onChange }) => (
<select onChange={onChange} className={styles.selectLevel} role="select-component">
{levelData.map((item: string) => {
return (
<option className={styles.optionLevel} key={item}>
Expand Down
3 changes: 2 additions & 1 deletion src/components/scoreboard/components/reset/reset.module.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.resetButton {
font-size: 1.5vw;
font-size: 1.1vw;
height: 100%;
cursor: pointer;
font-weight: 700;
border-width: 0.15vw;
Expand Down
3 changes: 1 addition & 2 deletions src/components/scoreboard/scoreboard.module.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.wrapperScoreboard {
display: flex;
width: max-content;
padding-bottom: 2vw;
width: 100%;
justify-content: space-between;
}
14 changes: 10 additions & 4 deletions src/components/scoreboard/scoreboard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent } from "react";
import React, { ChangeEvent, FunctionComponent } from "react";

import { Counter, Level, ResetButton } from "./components";
import styles from "./scoreboard.module.scss";
Expand All @@ -20,14 +20,20 @@ export interface ScoreboardProps {
* Bombs in the field
*/
mines: string;
/**
* Action handler when select new lvl
*/
onChange: (event: ChangeEvent<HTMLSelectElement>) => void;
}

export const ScoreBoard: FunctionComponent<ScoreboardProps> = ({ time, mines, onReset, levels }) => {
export const ScoreBoard: FunctionComponent<ScoreboardProps> = ({ time, mines, onReset, levels, onChange }) => {
return (
<div className={styles.wrapperScoreboard}>
<Counter itemNumber={time} />
<Level levelData={levels} />
<ResetButton onReset={onReset} />
<div>
<Level onChange={onChange} levelData={levels} />
<ResetButton onReset={onReset} />
</div>
<Counter itemNumber={mines} />
</div>
);
Expand Down
6 changes: 0 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@ import { MockFieldData } from "@helpers/mockData";
import React from "react";
import { createRoot } from "react-dom/client";

// import { CellComponent } from "@components/grid/cell/cell";
// import { ScoreBoard } from "@components/scoreboard";
// import { App } from "@components/app";
// import { Top } from "@components/top-section";

// import Segment from "react-segment-analytics";

const container = document.getElementById("root") as HTMLElement;
const root = createRoot(container);

root.render(
<React.StrictMode>
{/*<Segment writeKey={process.env.SEGMENT_WRITE_KEY as string}>*/}
{/*<App />*/}
<>
{/*<Top feature="Flag" firstAction="ctrl" secondAction="click">*/}
{/* Minesweeper*/}
{/*</Top>*/}
{/*<ScoreBoard time="000" onReset={() => null} levels={["beginner", "intermediate", "expert"]} mines="010" />*/}
{/*<CellComponent coords={[1, 1]} children={10} onContextMenu={() => null} onClick={() => null} />*/}
<GridComponent children={MockFieldData} onContextMenu={() => null} onClick={() => null} />
</>
{/*</Segment>*/}
Expand Down

0 comments on commit 80af0f2

Please sign in to comment.