Skip to content

Commit 3cd0f52

Browse files
committed
test: add icon-button tests
1 parent d00c0e0 commit 3cd0f52

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { TapsiIconButton } from "./index.ts";
2+
3+
import { render, screen, userEvent } from "@internals/test-helpers";
4+
import { describe, expect, it, jest } from "@jest/globals";
5+
import { createComponent } from "@lit/react";
6+
import React from "react";
7+
import {
8+
itShouldMount,
9+
itSupportsClassName,
10+
itSupportsDataSetProps,
11+
itSupportsStyle,
12+
} from "../../internals/tests.tsx";
13+
14+
const IconButton = createComponent({
15+
react: React,
16+
elementClass: TapsiIconButton,
17+
tagName: "tapsi-icon-button",
18+
});
19+
20+
const handleClick = jest.fn();
21+
const getTestIconButton = () => screen.getByTestId("test-icon-button");
22+
23+
const mockRequiredProps = {
24+
"data-testid": "test-icon-button",
25+
label: "test-icon-button-label",
26+
};
27+
28+
describe("🧪 button/icon-button: UI", () => {
29+
itShouldMount(IconButton, mockRequiredProps);
30+
itSupportsClassName(IconButton, mockRequiredProps);
31+
itSupportsStyle(IconButton, mockRequiredProps);
32+
itSupportsDataSetProps(IconButton, mockRequiredProps);
33+
34+
it("should trigger `handleClick` function after clicking on the icon button", async () => {
35+
render(
36+
<IconButton
37+
{...mockRequiredProps}
38+
onClick={handleClick}
39+
/>,
40+
);
41+
const testIconButton = getTestIconButton();
42+
43+
const { click } = userEvent.setup();
44+
45+
await click(testIconButton);
46+
47+
expect(handleClick).toHaveBeenCalledTimes(1);
48+
});
49+
50+
it("should trigger `handleClick` function after focusing on the button and pressing Enter", async () => {
51+
render(
52+
<IconButton
53+
{...mockRequiredProps}
54+
onClick={handleClick}
55+
/>,
56+
);
57+
const testIconButton = getTestIconButton();
58+
59+
const { tab, keyboard } = userEvent.setup();
60+
61+
await tab();
62+
expect(testIconButton).toHaveFocus();
63+
await keyboard("{Enter}");
64+
65+
expect(handleClick).toHaveBeenCalledTimes(1);
66+
});
67+
68+
it("should not be able to trigger `handleClick` function while `disabled`", async () => {
69+
render(
70+
<IconButton
71+
{...mockRequiredProps}
72+
onClick={handleClick}
73+
disabled
74+
/>,
75+
);
76+
const testIconButton = getTestIconButton();
77+
78+
const { tab, click } = userEvent.setup();
79+
80+
await tab();
81+
expect(testIconButton).not.toHaveFocus();
82+
83+
await click(testIconButton);
84+
85+
expect(handleClick).toHaveBeenCalledTimes(0);
86+
});
87+
88+
it("should have `aria-label` when the host has a `label` property", async () => {
89+
render(<IconButton {...mockRequiredProps} />);
90+
91+
expect(
92+
await screen.findByShadowTestId("tapsi-button-root"),
93+
).toHaveAttribute("aria-label", "test-icon-button-label");
94+
});
95+
96+
it("should show spinner with `loading` prop", async () => {
97+
render(
98+
<IconButton
99+
{...mockRequiredProps}
100+
loading
101+
/>,
102+
);
103+
104+
expect(
105+
await screen.findByShadowTestId("tapsi-button-spinner"),
106+
).toBeInTheDocument();
107+
});
108+
});

0 commit comments

Comments
 (0)