1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix <Switch/> checked callback attribute value (#5360)

This commit is contained in:
Alex Andreev 2022-05-11 17:33:01 +03:00 committed by GitHub
parent d99cc5a015
commit cc9ee67dc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -48,4 +48,24 @@ describe("<Switch/>", () => {
expect(onClick).not.toHaveBeenCalled(); expect(onClick).not.toHaveBeenCalled();
}); });
it("returns true checked attribute in a onChange callback", () => {
const onClick = jest.fn();
const { getByTestId } = render(<Switch onChange={onClick} checked={true}/>);
const switcher = getByTestId("switch");
fireEvent.click(switcher);
expect(onClick).toHaveBeenCalledWith(false, expect.any(Object));
});
it("returns false checked attribute in a onChange callback", () => {
const onClick = jest.fn();
const { getByTestId } = render(<Switch onChange={onClick}/>);
const switcher = getByTestId("switch");
fireEvent.click(switcher);
expect(onClick).toHaveBeenCalledWith(true, expect.any(Object));
});
}); });

View File

@ -17,7 +17,13 @@ export function Switch({ children, disabled, onChange, ...props }: SwitchProps)
return ( return (
<label className={cssNames(styles.Switch, { [styles.disabled]: disabled })} data-testid="switch"> <label className={cssNames(styles.Switch, { [styles.disabled]: disabled })} data-testid="switch">
{children} {children}
<input type="checkbox" role="switch" disabled={disabled} onChange={(event) => onChange?.(props.checked, event)} {...props}/> <input
type="checkbox"
role="switch"
disabled={disabled}
onChange={(event) => onChange?.(event.target.checked, event)}
{...props}
/>
</label> </label>
); );
} }