Skip to content

Commit e707b5f

Browse files
authored
chore: Refactor RadioGroup tests (#7456)
1 parent 72e40be commit e707b5f

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

packages/core/test/controls/radioGroupTests.tsx

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,63 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { assert } from "chai";
18-
import { type EnzymePropSelector, mount, type ReactWrapper } from "enzyme";
17+
import { render, screen } from "@testing-library/react";
18+
import userEvent from "@testing-library/user-event";
19+
import { expect } from "chai";
1920
import * as React from "react";
2021
import { spy, stub } from "sinon";
2122

22-
import { type OptionProps, Radio, RadioGroup } from "../../src";
23+
import { Classes, type OptionProps, Radio, RadioGroup } from "../../src";
2324
import { RADIOGROUP_WARN_CHILDREN_OPTIONS_MUTEX } from "../../src/common/errors";
2425

25-
describe("<RadioGroup>", () => {
26-
const emptyHandler = () => {
27-
return;
28-
};
26+
const emptyHandler = () => {
27+
return;
28+
};
2929

30-
it("nothing is selected by default", () => {
31-
const group = mount(
30+
describe("<RadioGroup>", () => {
31+
it("should select nothing by default", () => {
32+
render(
3233
<RadioGroup onChange={emptyHandler}>
3334
<Radio value="one" label="One" />
3435
<Radio value="two" label="Two" />
3536
</RadioGroup>,
3637
);
37-
assert.lengthOf(group.find({ checked: true }), 0);
38+
const radio1 = screen.getByRole<HTMLInputElement>("radio", { name: "One" });
39+
const radio2 = screen.getByRole<HTMLInputElement>("radio", { name: "Two" });
40+
41+
expect(radio1.checked).to.be.false;
42+
expect(radio2.checked).to.be.false;
3843
});
3944

40-
it("selectedValue checks that value", () => {
41-
const group = mount(
45+
it("should select the value when selectedValue is set", () => {
46+
render(
4247
<RadioGroup onChange={emptyHandler} selectedValue="two">
4348
<Radio value="one" label="One" />
4449
<Radio value="two" label="Two" />
4550
</RadioGroup>,
4651
);
47-
assert.isTrue(findInput(group, { checked: true }).is({ value: "two" }));
52+
53+
const radio1 = screen.getByRole<HTMLInputElement>("radio", { name: "One" });
54+
const radio2 = screen.getByRole<HTMLInputElement>("radio", { name: "Two" });
55+
56+
expect(radio1.checked).to.be.false;
57+
expect(radio2.checked).to.be.true;
4858
});
4959

5060
it("invokes onChange handler when a radio is clicked", () => {
51-
const changeSpy = spy();
52-
const group = mount(
53-
<RadioGroup onChange={changeSpy}>
61+
const onChange = spy();
62+
render(
63+
<RadioGroup onChange={onChange}>
5464
<Radio value="one" label="One" />
5565
<Radio value="two" label="Two" />
5666
</RadioGroup>,
5767
);
58-
findInput(group, { value: "one" }).simulate("change");
59-
findInput(group, { value: "two" }).simulate("change");
60-
assert.equal(changeSpy.callCount, 2);
68+
const radio1 = screen.getByRole<HTMLInputElement>("radio", { name: "One" });
69+
70+
userEvent.click(radio1);
71+
72+
expect(onChange.calledOnce).to.be.true;
73+
expect(onChange.getCall(0).args[0].target.value).to.equal("one");
6174
});
6275

6376
it("renders options as radio buttons", () => {
@@ -66,46 +79,52 @@ describe("<RadioGroup>", () => {
6679
{ label: "B", value: "b" },
6780
{ disabled: true, label: "C", value: "c" },
6881
];
69-
const group = mount(<RadioGroup onChange={emptyHandler} options={OPTIONS} selectedValue="b" />);
70-
const radios = group.find(Radio);
71-
assert.isTrue(radios.at(0).hasClass("foo"), "className");
72-
assert.isTrue(radios.at(1).is({ checked: true }), "selectedValue");
73-
assert.isTrue(radios.at(2).prop("disabled"), "disabled");
82+
render(<RadioGroup onChange={emptyHandler} options={OPTIONS} selectedValue="b" />);
83+
84+
const radio1 = screen.getByRole<HTMLInputElement>("radio", { name: "A" });
85+
const radio1Control = radio1.closest(`.${Classes.CONTROL}`);
86+
const radio2 = screen.getByRole<HTMLInputElement>("radio", { name: "B" });
87+
const radio3 = screen.getByRole<HTMLInputElement>("radio", { name: "C" });
88+
89+
expect(radio1Control).to.exist;
90+
expect([...radio1Control!.classList]).to.include("foo");
91+
expect(radio2.checked).to.be.true;
92+
expect(radio3.disabled).to.be.true;
7493
});
7594

7695
it("options label defaults to value", () => {
77-
const OPTIONS = [{ value: "text" }, { value: 23 }];
78-
const group = mount(<RadioGroup onChange={emptyHandler} options={OPTIONS} selectedValue="b" />);
79-
OPTIONS.forEach(props => {
80-
assert.strictEqual(findInput(group, props).parents().first().text(), props.value.toString());
81-
});
96+
const OPTIONS = [{ value: "text" }, { value: 123 }];
97+
render(<RadioGroup onChange={emptyHandler} options={OPTIONS} />);
98+
const text = screen.getByRole<HTMLInputElement>("radio", { name: "text" });
99+
const number = screen.getByRole<HTMLInputElement>("radio", { name: "123" });
100+
101+
expect(text.value).to.equal("text");
102+
expect(number.value).to.equal("123");
82103
});
83104

84105
it("uses options if given both options and children (with conosle warning)", () => {
85106
const warnSpy = stub(console, "warn");
86-
const group = mount(
107+
render(
87108
<RadioGroup onChange={emptyHandler} options={[]}>
88109
<Radio value="one" />
89110
</RadioGroup>,
90111
);
91-
assert.lengthOf(group.find(Radio), 0);
92-
assert.isTrue(warnSpy.alwaysCalledWith(RADIOGROUP_WARN_CHILDREN_OPTIONS_MUTEX));
112+
113+
expect(screen.queryByRole("radio")).to.not.exist;
114+
expect(warnSpy.calledWith(RADIOGROUP_WARN_CHILDREN_OPTIONS_MUTEX)).to.be.true;
93115
warnSpy.restore();
94116
});
95117

96118
it("renders non-Radio children too", () => {
97-
const group = mount(
119+
render(
98120
<RadioGroup onChange={emptyHandler}>
99121
<Radio />
100-
<address />
122+
<div data-testid="test" />
101123
<Radio />
102124
</RadioGroup>,
103125
);
104-
assert.lengthOf(group.find("address"), 1);
105-
assert.lengthOf(group.find(Radio), 2);
106-
});
107126

108-
function findInput(wrapper: ReactWrapper<any, any>, props: EnzymePropSelector) {
109-
return wrapper.find("input").filter(props);
110-
}
127+
expect(screen.getByTestId("test")).to.exist;
128+
expect(screen.getAllByRole("radio")).to.have.length(2);
129+
});
111130
});

0 commit comments

Comments
 (0)