Skip to content

Commit 8c65df6

Browse files
committed
test(form): added tests for controlled Switches
Closes #1175
1 parent bffae6f commit 8c65df6

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/form/src/toggle/SwitchTrack.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>(
5353
{
5454
id,
5555
disabled = false,
56-
checked = false,
5756
className,
5857
ballStyle,
5958
ballClassName,
@@ -63,6 +62,7 @@ export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>(
6362
},
6463
ref
6564
) {
65+
const { checked = false } = props;
6666
return (
6767
<span
6868
{...containerProps}
@@ -76,7 +76,6 @@ export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>(
7676
ref={ref}
7777
type="checkbox"
7878
className={cn(styles("input"))}
79-
checked={checked}
8079
disabled={disabled}
8180
/>
8281
<label

packages/form/src/toggle/__tests__/Switch.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from "react";
2-
import { render } from "@testing-library/react";
1+
import React, { useState } from "react";
2+
import { fireEvent, render } from "@testing-library/react";
33

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

@@ -60,4 +60,27 @@ describe("Switch", () => {
6060

6161
expect(container).toMatchSnapshot();
6262
});
63+
64+
it("should work as a controlled component", () => {
65+
function Test() {
66+
const [checked, setChecked] = useState(false);
67+
68+
return (
69+
<Switch
70+
id="switch"
71+
label="Label"
72+
checked={checked}
73+
onChange={(event) => setChecked(event.currentTarget.checked)}
74+
/>
75+
);
76+
}
77+
78+
const { getByRole } = render(<Test />);
79+
80+
const checkbox = getByRole("checkbox", { name: "Label" });
81+
expect(checkbox).not.toBeChecked();
82+
83+
fireEvent.click(checkbox);
84+
expect(checkbox).toBeChecked();
85+
});
6386
});

0 commit comments

Comments
 (0)