Skip to content

Commit ca4a121

Browse files
committed
test(checkbox): add unit tests
1 parent 1ba777a commit ca4a121

File tree

6 files changed

+197
-15
lines changed

6 files changed

+197
-15
lines changed

tests/Checkbox.test.svelte

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts">
2+
import { Checkbox } from "carbon-components-svelte";
3+
4+
export let group = ["option-2"];
5+
6+
$: console.log(group);
7+
</script>
8+
9+
<Checkbox
10+
bind:group
11+
value="option-1"
12+
labelText="Option 1"
13+
data-testid="checkbox-1"
14+
/>
15+
16+
<Checkbox
17+
bind:group
18+
value="option-2"
19+
labelText="Option 2"
20+
data-testid="checkbox-2"
21+
/>
22+
23+
<Checkbox
24+
bind:group
25+
value="option-3"
26+
labelText="Option 3"
27+
data-testid="checkbox-3"
28+
/>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
import { Checkbox } from "carbon-components-svelte";
3+
</script>
4+
5+
<Checkbox skeleton data-testid="checkbox-skeleton" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
import { Checkbox } from "carbon-components-svelte";
3+
</script>
4+
5+
<Checkbox data-testid="checkbox-slot">
6+
<span slot="labelText" data-testid="custom-label">Custom label content</span>
7+
</Checkbox>

tests/Checkbox/Checkbox.test.svelte

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script lang="ts">
2+
import { Checkbox } from "carbon-components-svelte";
3+
4+
export let checked = false;
5+
export let indeterminate = false;
6+
export let disabled = false;
7+
export let hideLabel = false;
8+
export let labelText = "Checkbox label";
9+
</script>
10+
11+
<Checkbox
12+
bind:checked
13+
bind:indeterminate
14+
{disabled}
15+
{hideLabel}
16+
{labelText}
17+
data-testid="checkbox"
18+
on:check={() => console.log("check")}
19+
on:click={() => console.log("click")}
20+
/>

tests/Checkbox/Checkbox.test.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { render, screen } from "@testing-library/svelte";
2+
import { user } from "../setup-tests";
3+
import Checkbox from "./Checkbox.test.svelte";
4+
import CheckboxSkeleton from "./Checkbox.skeleton.test.svelte";
5+
import CheckboxGroup from "./Checkbox.group.test.svelte";
6+
import CheckboxSlot from "./Checkbox.slot.test.svelte";
7+
8+
describe("Checkbox", () => {
9+
beforeEach(() => {
10+
vi.clearAllMocks();
11+
});
12+
13+
it("renders with default props", () => {
14+
render(Checkbox);
15+
16+
const checkbox = screen.getByTestId("checkbox");
17+
expect(checkbox).toBeInTheDocument();
18+
19+
const input = checkbox.querySelector("input[type='checkbox']");
20+
expect(input).not.toBeChecked();
21+
expect(input).not.toBeDisabled();
22+
expect(input).not.toHaveAttribute("indeterminate");
23+
24+
const label = checkbox.querySelector("label");
25+
expect(label).toHaveTextContent("Checkbox label");
26+
const hiddenElement = label?.querySelector(".bx--visually-hidden");
27+
expect(hiddenElement).not.toBeInTheDocument();
28+
});
29+
30+
it("renders checked state", () => {
31+
render(Checkbox, { checked: true });
32+
33+
const input = screen
34+
.getByTestId("checkbox")
35+
.querySelector("input[type='checkbox']");
36+
expect(input).toBeChecked();
37+
});
38+
39+
it("emits events", async () => {
40+
const consoleLog = vi.spyOn(console, "log");
41+
render(Checkbox);
42+
43+
const input = screen
44+
.getByTestId("checkbox")
45+
.querySelector("input[type='checkbox']") as HTMLInputElement;
46+
await user.click(input);
47+
expect(consoleLog).toHaveBeenCalledWith("check");
48+
expect(consoleLog).toHaveBeenCalledWith("click");
49+
expect(consoleLog).toHaveBeenCalledTimes(2);
50+
});
51+
52+
it("renders indeterminate state", () => {
53+
render(Checkbox, { indeterminate: true });
54+
55+
const input = screen
56+
.getByTestId("checkbox")
57+
.querySelector("input[type='checkbox']") as HTMLInputElement;
58+
expect(input.indeterminate).toBe(true);
59+
});
60+
61+
it("renders disabled state", () => {
62+
render(Checkbox, { disabled: true });
63+
64+
const input = screen
65+
.getByTestId("checkbox")
66+
.querySelector("input[type='checkbox']");
67+
expect(input).toBeDisabled();
68+
});
69+
70+
it("renders with hidden label", () => {
71+
render(Checkbox, { hideLabel: true });
72+
73+
const label = screen.getByTestId("checkbox").querySelector("label");
74+
const hiddenElement = label?.querySelector(".bx--visually-hidden");
75+
expect(hiddenElement).toBeInTheDocument();
76+
});
77+
78+
it("renders with custom label text", () => {
79+
render(Checkbox, { labelText: "Custom label" });
80+
81+
const label = screen.getByTestId("checkbox").querySelector("label");
82+
expect(label).toHaveTextContent("Custom label");
83+
});
84+
85+
it("renders skeleton state", () => {
86+
render(CheckboxSkeleton);
87+
88+
const skeleton = screen.getByTestId("checkbox-skeleton");
89+
expect(skeleton).toBeInTheDocument();
90+
expect(
91+
skeleton.querySelector(".bx--checkbox-label-text.bx--skeleton"),
92+
).toBeInTheDocument();
93+
expect(
94+
skeleton.querySelector("input[type='checkbox']"),
95+
).not.toBeInTheDocument();
96+
});
97+
98+
it("works with checkbox groups", async () => {
99+
const consoleLog = vi.spyOn(console, "log");
100+
render(CheckboxGroup);
101+
102+
const checkbox1 = screen
103+
.getByTestId("checkbox-1")
104+
.querySelector("input[type='checkbox']") as HTMLInputElement;
105+
const checkbox2 = screen
106+
.getByTestId("checkbox-2")
107+
.querySelector("input[type='checkbox']") as HTMLInputElement;
108+
const checkbox3 = screen
109+
.getByTestId("checkbox-3")
110+
.querySelector("input[type='checkbox']") as HTMLInputElement;
111+
112+
expect(checkbox1).not.toBeChecked();
113+
expect(checkbox2).toBeChecked();
114+
expect(checkbox3).not.toBeChecked();
115+
expect(consoleLog).toHaveBeenCalledWith(["option-2"]);
116+
117+
await user.click(checkbox1);
118+
expect(checkbox1).toBeChecked();
119+
expect(consoleLog).toHaveBeenCalledWith(["option-2", "option-1"]);
120+
121+
await user.click(checkbox2);
122+
expect(checkbox2).not.toBeChecked();
123+
expect(consoleLog).toHaveBeenCalledWith(["option-2"]);
124+
125+
await user.click(checkbox3);
126+
expect(checkbox3).toBeChecked();
127+
expect(consoleLog).toHaveBeenCalledWith(["option-1", "option-3"]);
128+
});
129+
130+
it("supports custom label slot", () => {
131+
render(CheckboxSlot);
132+
133+
const customLabel = screen.getByTestId("custom-label");
134+
expect(customLabel).toBeInTheDocument();
135+
expect(customLabel).toHaveTextContent("Custom label content");
136+
});
137+
});

0 commit comments

Comments
 (0)