Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for clicks #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import App from "./App";
import { act } from "react-dom/test-utils";

test("Renders", () => {
render(<App />);
const linkElement = screen.getAllByText(/None/i);
expect(linkElement[0]).toBeInTheDocument();
});

test("Clicking on an option changes the displayed piece", () => {
// const { container } = render(<App />);
render(<App />);

const radio = screen.getAllByRole("radio");

act(() => {
radio[0].click();
});
let pieceDisplay = screen.getByRole("none");
let style = getComputedStyle(pieceDisplay).backgroundColor;

expect(style).toEqual("yellow");

act(() => {
radio[1].click();
});
pieceDisplay = screen.getByRole("none");
style = getComputedStyle(pieceDisplay).backgroundColor;
expect(style).toEqual("violet");

act(() => {
radio[2].click();
});
pieceDisplay = screen.getByRole("none");
style = getComputedStyle(pieceDisplay).backgroundColor;
expect(style).toEqual("grey");
});

test("Clicking on a node should change it", () => {
render(<App />);

const table = screen.getAllByRole("button");

for (let i = 0; i < table.length; i++) {
const item = table[i];

let style = getComputedStyle(item).backgroundColor;

expect(style).toEqual("red");

act(() => {
item.click();
});

style = getComputedStyle(item).backgroundColor;

expect(style).toEqual("green");
}
});
53 changes: 28 additions & 25 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function ConeOrCube(props: { piece: cubeOrCone }): JSX.Element {
return (
<div
className="coneOrCube"
role="none"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't none the default role for a <div>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESLint complained about it - but maybe i had something there before

Copy link
Contributor Author

@Pokesi Pokesi Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tests dont passif the role isnt defined as none

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we shouldn't be setting the ARIA role to none anyway. It sounds like your tests are abusing the ARIA role to select this element. Maybe find a different selector? You have a class name on this after all...

style={{
backgroundColor:
piece === "None" ? "grey" : piece === "Cube" ? "violet" : "yellow",
Expand All @@ -42,31 +43,33 @@ function Table(props: {

return (
<table className="table">
{[0, 1, 2].map(function (key, row) {
return (
<tr key={key}>
{table[row].map(function (item, key) {
return (
<td key={key}>
<p style={{ fontSize: "12px" }}>Node ID #{row * 9 + key}</p>
<Button
onClick={() => {
handleButtonClick(key, row);
}}
variant="contained"
className="gridButton"
style={{
backgroundColor: item ? "green" : "red",
}}
>
{item ? "o" : "x"}
</Button>
</td>
);
})}
</tr>
);
})}
<tbody>
{[0, 1, 2].map(function (key, row) {
return (
<tr key={key}>
{table[row].map(function (item, key) {
return (
<td key={key}>
<p style={{ fontSize: "12px" }}>Node ID #{row * 9 + key}</p>
<Button
onClick={() => {
handleButtonClick(key, row);
}}
variant="contained"
className="gridButton"
style={{
backgroundColor: item ? "green" : "red",
}}
>
{item ? "o" : "x"}
</Button>
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
);
}
Expand Down
Binary file added tests.txt
Binary file not shown.