Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: checkbox tests #560

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
disabled = false,
required = false,
name,
value,
value = "on",
el = $bindable(),
asChild,
child,
Expand Down
20 changes: 14 additions & 6 deletions packages/bits-ui/src/tests/checkbox/Checkbox.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from "@testing-library/svelte";
import { render } from "@testing-library/svelte/svelte5";
import { userEvent } from "@testing-library/user-event";
import { axe } from "jest-axe";
import { describe, it } from "vitest";
Expand All @@ -9,11 +9,11 @@ import type { Checkbox } from "$lib/index.js";

const kbd = getTestKbd();

function setup(props?: Checkbox.Props) {
function setup(props?: Checkbox.RootProps) {
const user = userEvent.setup();
const returned = render(CheckboxTest, props);
const root = returned.getByTestId("root");
const input = returned.getByTestId("input") as HTMLInputElement;
const input = document.querySelector("input") as HTMLInputElement;
return {
...returned,
root,
Expand All @@ -29,10 +29,18 @@ describe("checkbox", () => {
});

it("has bits data attrs", async () => {
const { getByTestId, root } = setup();
const indicator = getByTestId("indicator");
const { root } = setup();
expect(root).toHaveAttribute("data-checkbox-root");
expect(indicator).toHaveAttribute("data-checkbox-indicator");
});

it("doesn't render the checkbox input if a name prop isnt passed", async () => {
const { input } = setup();
expect(input).not.toBeInTheDocument();
});

it("renders the checkbox input if a name prop is passed", async () => {
const { input } = setup({ name: "checkbox" });
expect(input).toBeInTheDocument();
});

it('defaults the value to "on", when no value prop is passed', async () => {
Expand Down
21 changes: 7 additions & 14 deletions packages/bits-ui/src/tests/checkbox/CheckboxTest.svelte
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<script lang="ts">
import { Checkbox } from "$lib/index.js";

type $$Props = Checkbox.Props;

export let checked: Checkbox.Props["checked"] = false;
let { checked = false, ...restProps }: Checkbox.RootProps = $props();
</script>

<main>
<p data-testid="binding">{checked}</p>
<Checkbox.Root data-testid="root" bind:checked {...$$restProps}>
<Checkbox.Indicator data-testid="indicator" let:isChecked let:isIndeterminate>
{#if isIndeterminate}
indeterminate
{:else if isChecked}
true
{:else}
false
{/if}
</Checkbox.Indicator>
<Checkbox.Input data-testid="input" />
<Checkbox.Root name="terms" data-testid="root" bind:checked {...restProps}>
{#snippet children({ checked })}
<span data-testid="indicator">
{checked}
</span>
{/snippet}
</Checkbox.Root>
</main>
Loading