Skip to content

Commit ff1fb47

Browse files
authored
chore: Update Card/CardList component tests (#7449)
1 parent ed2bb66 commit ff1fb47

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

packages/core/test/card-list/cardListTests.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,39 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { assert } from "chai";
18-
import { mount } from "enzyme";
17+
import { render, screen } from "@testing-library/react";
18+
import { expect } from "chai";
1919
import * as React from "react";
2020

2121
import { Card, CardList, Classes } from "../../src";
22+
import { hasClass } from "../utils";
2223

2324
describe("<CardList>", () => {
24-
it("supports className prop", () => {
25+
it("should support className prop", () => {
2526
const TEST_CLASS = "test-class";
26-
const wrapper = mount(
27+
render(
2728
<CardList className={TEST_CLASS}>
2829
<Card>first</Card>
2930
<Card>second</Card>
3031
</CardList>,
3132
);
33+
const cardList = screen.getByRole("list");
3234

33-
assert.isTrue(wrapper.find(`.${Classes.CARD_LIST}`).hostNodes().hasClass(TEST_CLASS), TEST_CLASS);
35+
expect(hasClass(cardList, Classes.CARD_LIST)).to.be.true;
36+
expect(hasClass(cardList, TEST_CLASS)).to.be.true;
3437
});
3538

36-
it("supports HTML props", () => {
37-
const cardList = mount(<CardList title="foo" />).find("div");
38-
assert.strictEqual(cardList.prop("title"), "foo");
39+
it("should support HTML props", () => {
40+
render(<CardList title="foo" />);
41+
const cardList = screen.getByRole("list");
42+
43+
expect(cardList.getAttribute("title")).to.equal("foo");
3944
});
4045

41-
it("supports ref prop", () => {
46+
it("should support ref prop", () => {
4247
const elementRef = React.createRef<HTMLDivElement>();
43-
mount(<CardList ref={elementRef} />);
44-
assert.isDefined(elementRef.current);
48+
render(<CardList ref={elementRef}>Test</CardList>);
49+
50+
expect(elementRef.current).to.exist;
4551
});
4652
});

packages/core/test/card/cardTests.tsx

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

17-
import { assert } from "chai";
18-
import { mount, shallow } 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 sinon from "sinon";
2122

2223
import { Card, Classes, H4 } from "../../src";
24+
import { hasClass } from "../utils";
2325

2426
describe("<Card>", () => {
25-
it("supports elevation, interactive, and className props", () => {
26-
const wrapper = shallow(<Card elevation={3} interactive={true} className={Classes.TEXT_MUTED} />);
27+
it("should support elevation, interactive, and className props", () => {
28+
render(
29+
<Card elevation={3} interactive={true} className={Classes.TEXT_MUTED}>
30+
Test
31+
</Card>,
32+
);
33+
const card = screen.getByText("Test");
2734

28-
assert.isTrue(wrapper.hasClass(Classes.CARD), Classes.CARD);
29-
assert.isTrue(wrapper.hasClass(Classes.ELEVATION_3), Classes.ELEVATION_3);
30-
assert.isTrue(wrapper.hasClass(Classes.INTERACTIVE), Classes.INTERACTIVE);
31-
assert.isTrue(wrapper.hasClass(Classes.TEXT_MUTED), Classes.TEXT_MUTED);
35+
expect(hasClass(card, Classes.CARD)).to.be.true;
36+
expect(hasClass(card, Classes.ELEVATION_3)).to.be.true;
37+
expect(hasClass(card, Classes.INTERACTIVE)).to.be.true;
38+
expect(hasClass(card, Classes.TEXT_MUTED)).to.be.true;
3239
});
3340

34-
it("renders children", () => {
35-
const wrapper = shallow(
41+
it("should render children", () => {
42+
render(
3643
<Card>
3744
<H4>Card content</H4>
3845
</Card>,
3946
);
40-
assert.isTrue(wrapper.find(H4).exists());
47+
48+
expect(screen.getByText("Card content")).to.exist;
4149
});
4250

43-
it("calls onClick when card is clicked", () => {
51+
it("should call onClick when card is clicked", async () => {
4452
const onClick = sinon.spy();
45-
shallow(<Card onClick={onClick} />).simulate("click");
46-
assert.isTrue(onClick.calledOnce);
53+
render(<Card onClick={onClick}>Test</Card>);
54+
const card = screen.getByText("Test");
55+
56+
await userEvent.click(card);
57+
58+
expect(onClick.calledOnce).to.be.true;
4759
});
4860

49-
it("supports HTML props", () => {
61+
it("should support HTML props", () => {
5062
const onChange = sinon.spy();
51-
const card = shallow(<Card onChange={onChange} title="foo" tabIndex={4000} />).find("div");
52-
assert.strictEqual(card.prop("onChange"), onChange);
53-
assert.strictEqual(card.prop("title"), "foo");
63+
render(
64+
<Card onChange={onChange} title="foo" tabIndex={4000}>
65+
Test
66+
</Card>,
67+
);
68+
const card = screen.getByText("Test");
69+
70+
expect(card.getAttribute("title")).to.equal("foo");
71+
expect(card.tabIndex).to.equal(4000);
5472
});
5573

56-
it("supports ref prop", () => {
74+
it("should support ref prop", () => {
5775
const elementRef = React.createRef<HTMLDivElement>();
58-
mount(<Card ref={elementRef} />);
59-
assert.isDefined(elementRef.current);
76+
render(<Card ref={elementRef}>Test</Card>);
77+
78+
expect(elementRef.current).to.exist;
6079
});
6180
});

0 commit comments

Comments
 (0)