Skip to content

Commit

Permalink
Preparing the test cases for cell components
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Jul 29, 2022
1 parent e504892 commit 7db31f5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = {
"\\.(css|less|scss|sass)$": "identity-obj-proxy",
"test/(.*)$": "<rootDir>/test/$1",
"~/(.*)$": "<rootDir>/src/$1",
"@helpers/(.*)$": "<rootDir>/src/helpers/$1",
"~hooks/(.*)$": "<rootDir>/src/hooks/$1",
},
setupFilesAfterEnv: ["<rootDir>/test/setup.ts"],
globalTeardown: "<rootDir>/test/teardown.ts",
Expand Down
27 changes: 27 additions & 0 deletions src/components/grid/cell/cell.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CellState, Coordinates } from "@helpers/field";
import { fireEvent, render, screen } from "@testing-library/react";
import React from "react";

import { CellComponent } from "./cell";

describe("Testing the CELL component", function () {
const coords: Coordinates = [1, 1];

for (let cell = CellState.empty; cell <= CellState.weakMark; cell++) {
it("should check preventDefault contextMenu for every type of cell", function () {
const internalProps = {
coords,
onClick: jest.fn(),
onContextMenu: jest.fn(),
};

render(<CellComponent children={cell} {...internalProps} />);

expect(true).toBe(true);
});

it("onClick and onContextMenu handler should be called for active cells", function () {
expect(true).toBe(true);
});
}
});
31 changes: 20 additions & 11 deletions src/components/grid/cell/cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const CellComponent: FunctionComponent<CellProps> = ({ coords, children,
onMouseUp,
onMouseLeave: onMouseUp,
mouseDown,
"data-testid": `${coords}`,
"data-testid": `cells_${coords}`,
role: "cell",
};

Expand All @@ -96,20 +96,20 @@ export const ComponentsMap: FunctionComponent<ComponentsMapProps> = ({ children,
case CellState.bomb:
return (
<BombFrame {...rest}>
<span className={styles.bombEntity} />
<span className={styles.bombEntity} data-testid={`bomb_${rest["data-testid"]}`} />
</BombFrame>
);
case CellState.mark:
return (
<ClosedFrame {...rest}>
<FlagComponent />
<FlagComponent data-testid={`flag_${rest["data-testid"]}`} />
</ClosedFrame>
);

case CellState.weakMark:
return (
<ClosedFrame {...rest}>
<WeakFlagComponent />
<WeakFlagComponent data-testid={`weakFlag_${rest["data-testid"]}`} />
</ClosedFrame>
);
case CellState.hidden:
Expand All @@ -119,9 +119,14 @@ export const ComponentsMap: FunctionComponent<ComponentsMapProps> = ({ children,
}
};

const ClosedFrame: FunctionComponent<PropsWithChildren<{ mouseDown?: boolean }>> = ({ mouseDown, children }) => {
const ClosedFrame: FunctionComponent<PropsWithChildren<{ mouseDown?: boolean }>> = ({
mouseDown,
children,
...restProps
}) => {
return (
<div
{...restProps}
className={classNames(styles.closedFrame, {
[styles.changedBorderColor]: !mouseDown,
[styles.transparentClosedFrame]: mouseDown,
Expand All @@ -146,16 +151,20 @@ const RevealedFrame: FunctionComponent<PropsWithChildren<RevealedProps>> = ({ ch
);
};

const BombFrame: FunctionComponent<{ children: ReactNode }> = ({ children }) => {
return <div className={classNames(styles.closedFrame, styles.revealedFrame, styles.bombFrame)}>{children}</div>;
const BombFrame: FunctionComponent<{ children: ReactNode }> = ({ children, ...restProps }) => {
return (
<div {...restProps} className={classNames(styles.closedFrame, styles.revealedFrame, styles.bombFrame)}>
{children}
</div>
);
};

const FlagComponent: FunctionComponent = () => {
return <div className={styles.flagComponent} />;
const FlagComponent: FunctionComponent = ({ ...restProps }) => {
return <div {...restProps} className={styles.flagComponent} />;
};

const WeakFlagComponent: FunctionComponent = () => {
return <div className={classNames(styles.flagComponent, styles.flagTransparent)} />;
const WeakFlagComponent: FunctionComponent = ({ ...restProps }) => {
return <div {...restProps} className={classNames(styles.flagComponent, styles.flagTransparent)} />;
};

const transparent = "rgba(0,0,0,0)";
Expand Down

0 comments on commit 7db31f5

Please sign in to comment.