-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix Switch not toggling. (#985)
- Loading branch information
Showing
3 changed files
with
61 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useState } from "react"; | ||
import { Switch as SwitchComponent, SwitchProps } from "src/inputs"; | ||
import { click, render } from "src/utils/rtl"; | ||
|
||
describe("Switch", () => { | ||
it("can change", async () => { | ||
const onChange = jest.fn(); | ||
// Given a switch | ||
const r = await render(<SwitchTest label="Age" onChange={onChange} />); | ||
// Then it defaults no checked | ||
expect(r.age).not.toBeChecked(); | ||
// And when we click it, it flips to checked | ||
click(r.age); | ||
expect(r.age).toBeChecked(); | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
// And if we click it again, it flips back to unchecked | ||
click(r.age); | ||
expect(r.age).not.toBeChecked(); | ||
expect(onChange).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
type SwitchTestProps = Omit<SwitchProps, "onChange" | "selected"> & { | ||
onChange?: (value: boolean) => void; | ||
selected?: boolean; | ||
}; | ||
|
||
function SwitchTest({ selected: initSelected, onChange: _onChange, ...props }: SwitchTestProps) { | ||
const [selected, setSelected] = useState(initSelected || false); | ||
return ( | ||
<SwitchComponent | ||
labelStyle="inline" | ||
selected={selected} | ||
onChange={(value) => { | ||
_onChange?.(value); | ||
setSelected(value); | ||
}} | ||
{...props} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters