|
| 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