-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create grid component and test the render results
- Loading branch information
Showing
8 changed files
with
1,298 additions
and
6 deletions.
There are no files selected for viewing
1,171 changes: 1,171 additions & 0 deletions
1,171
src/components/grid/__snapshots__/grid.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.containerWrapper { | ||
display: grid; | ||
width: max-content; | ||
padding: 1vw; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./cell/cell"; | ||
export * from "./grid"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters