Skip to content

Commit

Permalink
Create grid component and test the render results
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Jul 31, 2022
1 parent 9ab3eaa commit ef594fb
Show file tree
Hide file tree
Showing 8 changed files with 1,298 additions and 6 deletions.
1,171 changes: 1,171 additions & 0 deletions src/components/grid/__snapshots__/grid.test.tsx.snap

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/components/grid/grid.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.containerWrapper {
display: grid;
width: max-content;
padding: 1vw;
}
14 changes: 14 additions & 0 deletions src/components/grid/grid.module.scss.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// autogenerated by typings-for-css-modules-loader.
// Please do not change this file!
declare namespace GridModuleScssNamespace {
export interface IGridModuleScss {
containerWrapper: string;
}
}

declare const GridModuleScssModule: GridModuleScssNamespace.IGridModuleScss & {
/** WARNING: Only available when `css-loader` is used without `style-loader` or `mini-css-extract-plugin` */
locals: GridModuleScssNamespace.IGridModuleScss;
};

export = GridModuleScssModule;
32 changes: 32 additions & 0 deletions src/components/grid/grid.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GridComponent } from "@components/grid/grid";
import { Field } from "@helpers/field";
import { render } from "@testing-library/react";
import React from "react";

const MockFieldData: Field = [
[9, 2, 9, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 1, 0, 1, 9, 1, 1, 9, 1],
[0, 0, 1, 9, 10, 0, 2, 2, 2, 1, 1, 1],
[0, 0, 10, 10, 1, 0, 1, 9, 1, 2, 2, 2],
[0, 1, 1, 2, 2, 9, 1, 1, 1, 0, 0, 0],
[0, 1, 9, 3, 9, 2, 10, 0, 0, 2, 1, 1],
[0, 2, 2, 4, 9, 2, 10, 1, 1, 1, 9, 1],
[0, 1, 9, 2, 1, 1, 1, 9, 1, 2, 2, 2],
[0, 1, 10, 10, 0, 0, 1, 1, 1, 1, 9, 1],
[0, 1, 10, 10, 0, 0, 1, 1, 1, 1, 9, 1],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 9, 1],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 9, 1],
];

describe("Grid Component rendering test", function () {
it("should render the grid correctly", function () {
const internalProps = {
onClick: jest.fn(),
onContextMenu: jest.fn(),
};

const { asFragment } = render(<GridComponent children={MockFieldData} {...internalProps} />);

expect(asFragment()).toMatchSnapshot();
});
});
48 changes: 48 additions & 0 deletions src/components/grid/grid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CellComponent } from "@components/grid/cell/cell";
import { Coordinates, Field } from "@helpers/field";
import React, { FunctionComponent, PropsWithChildren } from "react";

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

export interface GridProps {
/**
* Field data
*/
children: Field;
/**
* onClick handler
*/
onClick: (coords: Coordinates) => void;
/**
* onContextMenu handler
*/
onContextMenu: (coords: Coordinates) => void;
}

export const GridComponent: FunctionComponent<GridProps> = ({ children, ...restProps }) => {
return (
<WrapperContainer size={children.length} role="grid">
{children.map((row, y) =>
row.map((cell, x) => {
return <CellComponent key={`${y}_${x}_${cell}`} children={cell} coords={[y, x]} {...restProps} />;
})
)}
</WrapperContainer>
);
};

const WrapperContainer: FunctionComponent<PropsWithChildren<{ size: number; role: string }>> = ({
children,
size,
role,
}) => (
<div
className={styles.containerWrapper}
style={{
gridTemplateColumns: `repeat(${size}, auto)`,
}}
role={role}
>
{children}
</div>
);
2 changes: 2 additions & 0 deletions src/components/grid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./cell/cell";
export * from "./grid";
2 changes: 2 additions & 0 deletions src/hooks/useMouseDown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type SetMouseUpStatus = () => void;
export const useMouseDown = (): [boolean, SetMouseDownStatus, SetMouseUpStatus] => {
const [mouseDown, setMouseDown] = useState(false);

// useDebugValue(`mouseDown: ${mouseDown}`, (str) => `${str} ${new Date().toDateString()}`);

const onMouseDown = useCallback(
() => setMouseDown(true),
// Stryker disable next-line ArrayDeclaration
Expand Down
30 changes: 24 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import "@styles/styles.scss";
import "normalize.css/normalize.css";

import { CellComponent } from "@components/grid/cell/cell";
import { ScoreBoard } from "@components/scoreboard";
// import { App } from "@components/app";
import { Top } from "@components/top-section";
import { GridComponent } from "@components/grid";
import { Field } from "@helpers/field";
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);

// @ts-ignore
const MockFieldData: Field = [
[9, 2, 9, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 1, 0, 1, 9, 1, 1, 9, 1],
[0, 0, 1, 9, 10, 0, 2, 2, 2, 1, 1, 1],
[0, 0, 10, 10, 1, 0, 1, 9, 1, 2, 2, 2],
[0, 1, 1, 2, 2, 9, 1, 1, 1, 0, 0, 0],
[0, 1, 9, 3, 9, 2, 10, 0, 0, 2, 1, 1],
[0, 2, 2, 4, 9, 2, 10, 1, 1, 1, 9, 1],
[0, 1, 9, 2, 1, 1, 1, 9, 1, 2, 2, 2],
[0, 1, 10, 10, 0, 0, 1, 1, 1, 1, 9, 1],
[0, 1, 10, 10, 0, 0, 1, 1, 1, 1, 9, 1],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 9, 1],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 9, 1],
];

root.render(
<React.StrictMode>
{/*<Segment writeKey={process.env.SEGMENT_WRITE_KEY as string}>*/}
Expand All @@ -23,7 +40,8 @@ root.render(
{/* Minesweeper*/}
{/*</Top>*/}
{/*<ScoreBoard time="000" onReset={() => null} levels={["beginner", "intermediate", "expert"]} mines="010" />*/}
<CellComponent coords={[1, 1]} children={10} onContextMenu={() => null} onClick={() => null} />
{/*<CellComponent coords={[1, 1]} children={10} onContextMenu={() => null} onClick={() => null} />*/}
<GridComponent children={MockFieldData} onContextMenu={() => null} onClick={() => null} />
</>
{/*</Segment>*/}
</React.StrictMode>
Expand Down

0 comments on commit ef594fb

Please sign in to comment.