Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test(form): added tests for controlled Switches
Closes #1175
  • Loading branch information
mlaursen committed Jun 10, 2021
1 parent bffae6f commit 8c65df6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 1 addition & 2 deletions packages/form/src/toggle/SwitchTrack.tsx
Expand Up @@ -53,7 +53,6 @@ export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>(
{
id,
disabled = false,
checked = false,
className,
ballStyle,
ballClassName,
Expand All @@ -63,6 +62,7 @@ export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>(
},
ref
) {
const { checked = false } = props;
return (
<span
{...containerProps}
Expand All @@ -76,7 +76,6 @@ export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>(
ref={ref}
type="checkbox"
className={cn(styles("input"))}
checked={checked}
disabled={disabled}
/>
<label
Expand Down
27 changes: 25 additions & 2 deletions packages/form/src/toggle/__tests__/Switch.tsx
@@ -1,5 +1,5 @@
import React from "react";
import { render } from "@testing-library/react";
import React, { useState } from "react";
import { fireEvent, render } from "@testing-library/react";

import { Switch } from "../Switch";

Expand Down Expand Up @@ -60,4 +60,27 @@ describe("Switch", () => {

expect(container).toMatchSnapshot();
});

it("should work as a controlled component", () => {
function Test() {
const [checked, setChecked] = useState(false);

return (
<Switch
id="switch"
label="Label"
checked={checked}
onChange={(event) => setChecked(event.currentTarget.checked)}
/>
);
}

const { getByRole } = render(<Test />);

const checkbox = getByRole("checkbox", { name: "Label" });
expect(checkbox).not.toBeChecked();

fireEvent.click(checkbox);
expect(checkbox).toBeChecked();
});
});

0 comments on commit 8c65df6

Please sign in to comment.