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

Fix checked callback attribute value

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-05-11 10:09:40 +03:00
parent 9f25212f45
commit b8e56dfba5
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?.(!props.checked, event)}
{...props}
/>
</label> </label>
); );
} }