mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
test(checkbox): test multiple checkbox reactivity
This commit is contained in:
parent
558404118d
commit
863c30f3c8
2 changed files with 131 additions and 0 deletions
|
@ -4,6 +4,7 @@ import CheckboxGroup from "./Checkbox.group.test.svelte";
|
|||
import CheckboxSkeleton from "./Checkbox.skeleton.test.svelte";
|
||||
import CheckboxSlot from "./Checkbox.slot.test.svelte";
|
||||
import Checkbox from "./Checkbox.test.svelte";
|
||||
import MultipleCheckboxes from "./MultipleCheckboxes.test.svelte";
|
||||
|
||||
describe("Checkbox", () => {
|
||||
beforeEach(() => {
|
||||
|
@ -146,4 +147,106 @@ describe("Checkbox", () => {
|
|||
expect(customLabel).toBeInTheDocument();
|
||||
expect(customLabel).toHaveTextContent("Custom label content");
|
||||
});
|
||||
|
||||
it("renders multiple checkboxes with default values", () => {
|
||||
render(MultipleCheckboxes);
|
||||
|
||||
expect(screen.getByTestId("checkbox-0")).toBeInTheDocument();
|
||||
expect(screen.getByTestId("checkbox-1")).toBeInTheDocument();
|
||||
expect(screen.getByTestId("checkbox-2")).toBeInTheDocument();
|
||||
|
||||
const appleCheckbox = screen
|
||||
.getByTestId("checkbox-0")
|
||||
.querySelector("input[type='checkbox']");
|
||||
const bananaCheckbox = screen
|
||||
.getByTestId("checkbox-1")
|
||||
.querySelector("input[type='checkbox']");
|
||||
const coconutCheckbox = screen
|
||||
.getByTestId("checkbox-2")
|
||||
.querySelector("input[type='checkbox']");
|
||||
|
||||
assert(appleCheckbox);
|
||||
assert(bananaCheckbox);
|
||||
assert(coconutCheckbox);
|
||||
|
||||
expect(appleCheckbox).toBeChecked();
|
||||
expect(bananaCheckbox).toBeChecked();
|
||||
expect(coconutCheckbox).not.toBeChecked();
|
||||
|
||||
expect(screen.getByTestId("selected-values")).toHaveTextContent(
|
||||
'["Apple","Banana"]',
|
||||
);
|
||||
});
|
||||
|
||||
it("handles checkbox selection changes", async () => {
|
||||
const consoleLog = vi.spyOn(console, "log");
|
||||
render(MultipleCheckboxes);
|
||||
|
||||
const coconutCheckbox = screen
|
||||
.getByTestId("checkbox-2")
|
||||
.querySelector("input[type='checkbox']");
|
||||
assert(coconutCheckbox);
|
||||
|
||||
await user.click(coconutCheckbox);
|
||||
|
||||
expect(coconutCheckbox).toBeChecked();
|
||||
expect(consoleLog).toHaveBeenCalledWith("group changed:", [
|
||||
"Apple",
|
||||
"Banana",
|
||||
"Coconut",
|
||||
]);
|
||||
expect(screen.getByTestId("selected-values")).toHaveTextContent(
|
||||
'["Apple","Banana","Coconut"]',
|
||||
);
|
||||
});
|
||||
|
||||
it("handles button click to set specific value", async () => {
|
||||
const consoleLog = vi.spyOn(console, "log");
|
||||
render(MultipleCheckboxes);
|
||||
|
||||
await user.click(screen.getByText(/Set to/));
|
||||
|
||||
expect(consoleLog).toHaveBeenCalledWith("set to banana");
|
||||
expect(consoleLog).toHaveBeenCalledWith("group changed:", ["Banana"]);
|
||||
|
||||
const appleCheckbox = screen
|
||||
.getByTestId("checkbox-0")
|
||||
.querySelector("input[type='checkbox']");
|
||||
const bananaCheckbox = screen
|
||||
.getByTestId("checkbox-1")
|
||||
.querySelector("input[type='checkbox']");
|
||||
const coconutCheckbox = screen
|
||||
.getByTestId("checkbox-2")
|
||||
.querySelector("input[type='checkbox']");
|
||||
|
||||
assert(appleCheckbox);
|
||||
assert(bananaCheckbox);
|
||||
assert(coconutCheckbox);
|
||||
|
||||
expect(appleCheckbox).not.toBeChecked();
|
||||
expect(bananaCheckbox).toBeChecked();
|
||||
expect(coconutCheckbox).not.toBeChecked();
|
||||
|
||||
expect(screen.getByTestId("selected-values")).toHaveTextContent(
|
||||
'["Banana"]',
|
||||
);
|
||||
});
|
||||
|
||||
it("handles deselection of checkboxes", async () => {
|
||||
const consoleLog = vi.spyOn(console, "log");
|
||||
render(MultipleCheckboxes);
|
||||
|
||||
const bananaCheckbox = screen
|
||||
.getByTestId("checkbox-1")
|
||||
.querySelector("input[type='checkbox']");
|
||||
assert(bananaCheckbox);
|
||||
|
||||
await user.click(bananaCheckbox);
|
||||
|
||||
expect(bananaCheckbox).not.toBeChecked();
|
||||
expect(consoleLog).toHaveBeenCalledWith("group changed:", ["Apple"]);
|
||||
expect(screen.getByTestId("selected-values")).toHaveTextContent(
|
||||
'["Apple"]',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
28
tests/Checkbox/MultipleCheckboxes.test.svelte
Normal file
28
tests/Checkbox/MultipleCheckboxes.test.svelte
Normal file
|
@ -0,0 +1,28 @@
|
|||
<script lang="ts">
|
||||
import { Checkbox, Button } from "carbon-components-svelte";
|
||||
|
||||
export let values = ["Apple", "Banana", "Coconut"];
|
||||
export let group = values.slice(0, 2);
|
||||
|
||||
$: console.log("group changed:", group);
|
||||
</script>
|
||||
|
||||
{#each values as value, index}
|
||||
<Checkbox
|
||||
bind:group
|
||||
labelText={value}
|
||||
{value}
|
||||
data-testid={`checkbox-${index}`}
|
||||
/>
|
||||
{/each}
|
||||
|
||||
<Button
|
||||
on:click={() => {
|
||||
group = ["Banana"];
|
||||
console.log("set to banana");
|
||||
}}
|
||||
>
|
||||
Set to ["Banana"]
|
||||
</Button>
|
||||
|
||||
<span data-testid="selected-values">{JSON.stringify(group)}</span>
|
Loading…
Add table
Add a link
Reference in a new issue