Skip to content

Commit 6500043

Browse files
author
Chance Strickland
committed
Add interaction tests to Minutes
1 parent 5170745 commit 6500043

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/Minutes.test.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,74 @@ import {
66
MinutesInput,
77
} from "./Minutes";
88
import * as ReactTestingLibrary from "@testing-library/react";
9+
import ReactTestUtils from "react-dom/test-utils";
10+
import userEvent from "@testing-library/user-event";
911

1012
describe("<Minutes />", () => {
1113
describe("rendering", () => {
12-
test("that input contains accurate value based on value prop", () => {
14+
it("that input contains accurate value based on value prop", () => {
1315
let { getByRole } = ReactTestingLibrary.render(<Minutes value={5} />);
1416
let input = getByRole("textbox");
1517
expect(input.value).toBe(String(5));
1618
});
19+
20+
it("should increment when the add button is clicked", () => {
21+
let { getByRole, getByLabelText } = ReactTestingLibrary.render(
22+
<MinutesWithState />
23+
);
24+
let input = getByRole("textbox");
25+
let addButton = getByLabelText("Add");
26+
userEvent.click(addButton);
27+
expect(input.value).toBe(String(6));
28+
});
29+
30+
it("should decrement when the subtract button is clicked", () => {
31+
let { getByRole, getByLabelText } = ReactTestingLibrary.render(
32+
<MinutesWithState />
33+
);
34+
let input = getByRole("textbox");
35+
let subtractButton = getByLabelText("Subtract");
36+
userEvent.click(subtractButton);
37+
expect(input.value).toBe(String(4));
38+
});
39+
40+
describe("if the value is at 0", () => {
41+
it("should not decrement when the subtract button is clicked", () => {
42+
let { getByRole, getByLabelText } = ReactTestingLibrary.render(
43+
<MinutesWithState defaultValue={0} />
44+
);
45+
let input = getByRole("textbox");
46+
let subtractButton = getByLabelText("Subtract");
47+
userEvent.click(subtractButton);
48+
expect(input.value).toBe(String(0));
49+
});
50+
});
51+
52+
it("should update when the user types a valid value", () => {
53+
let { getByRole } = ReactTestingLibrary.render(
54+
<MinutesWithState defaultValue={5} />
55+
);
56+
let input = getByRole("textbox");
57+
58+
userEvent.type(input, "{backspace}");
59+
userEvent.type(input, "10");
60+
61+
expect(input.value).toBe(String(10));
62+
});
1763
});
1864
});
1965

2066
function Minutes({ "aria-labelledby": ariaLabelledBy, ...props }) {
2167
return (
2268
<MinutesRoot {...props}>
2369
<MinutesSubtract aria-label="Subtract" />
24-
<MinutesInput />
2570
<MinutesInput aria-labelledby={ariaLabelledBy} />
2671
<MinutesAdd aria-label="Add" />
2772
</MinutesRoot>
2873
);
2974
}
75+
76+
function MinutesWithState({ defaultValue = 5 }) {
77+
let [minutes, setMinutes] = React.useState(defaultValue);
78+
return <Minutes value={minutes} onValueChange={setMinutes} />;
79+
}

0 commit comments

Comments
 (0)