Skip to content

Commit e2d1ef2

Browse files
Switch test (#786)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 30a1704 commit e2d1ef2

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

docs/app/docs/components/switch/docs/codeUsage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const code = {
22
javascript: {
33
code: `
4-
import Switch from "@radui/ui/Switch"
4+
import Switch from "@radui/ui/Switch";
55
66
const SwitchExample = () => (
77
<div>

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/ui/Switch/Switch.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const COMPONENT_NAME = 'Switch';
66

77
export type SwitchProps = {
88
defaultChecked? : boolean;
9-
checked: boolean;
9+
checked?: boolean;
1010
color?: string;
1111
children?: React.ReactNode;
1212
className?: string;
@@ -23,7 +23,7 @@ const Switch = ({ children, customRootClass = '', className = '', color = '', de
2323
const handleChecked = () => {
2424
const updatedState = !isChecked;
2525
setIsChecked(updatedState);
26-
onChange(updatedState);
26+
onChange(updatedState)
2727
};
2828

2929
const data_attributes: Record<string, string> = {};
@@ -33,8 +33,9 @@ const Switch = ({ children, customRootClass = '', className = '', color = '', de
3333
}
3434
return (
3535
<>
36-
<input type='checkbox' className={clsx(rootClass)} {...props} checked= {isChecked}/>
36+
<input type='checkbox' className={clsx(rootClass)} {...props} checked= {isChecked} onChange={(e) => setIsChecked(e.target.checked)}/>
3737
<button type="button" onClick={handleChecked} role="switch" {...data_attributes}></button>
38+
3839
</>
3940
);
4041
};

src/components/ui/Switch/stories/Switch.stories.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useState } from 'react';
12
import Switch from '../Switch';
23
import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor';
34

@@ -9,10 +10,12 @@ export default {
910

1011
const CheckBox = (args) => {
1112
const variants = ['classic', 'surface', 'solid'];
13+
const [isChecked, setIsChecked] = useState(true)
14+
1215
const handleChange = (state) => {
1316
setIsChecked(state);
1417
};
15-
return <SandboxEditor className="space-x-1">
18+
return <SandboxEditor className="flex flex-col gap-2">
1619
{variants.map((variant, index) => (
1720
<Switch defaultChecked={args} key={index} variant={variant} onChange={handleChange} {...args}/>
1821
))}
@@ -22,6 +25,24 @@ const CheckBox = (args) => {
2225

2326
export const All = {};
2427

28+
export const controlled = () => {
29+
const [checked, setChecked] = useState(true);
30+
31+
const handleToggle = () => {
32+
setChecked((prev) => !prev)
33+
}
34+
return <SandboxEditor>
35+
<Switch checked={checked} onChange={handleToggle}/>
36+
</SandboxEditor>
37+
}
38+
39+
export const Uncontrolled = () => {
40+
41+
return <SandboxEditor>
42+
<Switch defaultChecked ={true} onChange={() => {}}/>
43+
44+
</SandboxEditor>
45+
}
2546
export const Color = {
2647
args: {
2748
color:"blue"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from "react";
2+
import { fireEvent, render, screen } from '@testing-library/react';
3+
import Switch from "../Switch";
4+
5+
describe('Switch Component', () => {
6+
7+
test('renders correctly', () => {
8+
render(<Switch checked={true} onChange={() => {}} />)
9+
const inputElement = screen.getByRole('checkbox')
10+
expect(inputElement).toBeInTheDocument();
11+
})
12+
13+
test('renders in controlled mode correctly', () => {
14+
const handleChange = jest.fn();
15+
render(<Switch checked={true} onChange={handleChange} />)
16+
const switchElement = screen.getByRole('switch')
17+
18+
fireEvent.click(switchElement)
19+
expect(handleChange).toHaveBeenCalledWith(false)
20+
21+
fireEvent.click(switchElement)
22+
expect(handleChange).toHaveBeenCalledWith(true)
23+
})
24+
25+
test('renders in uncontrolled mode correctly with defaultChecked', () => {
26+
27+
render(<Switch defaultChecked={true} onChange={() => {}}/>)
28+
const checkbox = screen.getByRole('checkbox')
29+
expect(checkbox).toBeChecked()
30+
})
31+
32+
test('toggles state independently',() => {
33+
render(<Switch defaultChecked={false} onChange={() => {}} />)
34+
const checkbox = screen.getByRole('checkbox')
35+
expect(checkbox).not.toBeChecked()
36+
37+
fireEvent.click(checkbox)
38+
expect(checkbox).toBeChecked()
39+
40+
fireEvent.click(checkbox)
41+
expect(checkbox).not.toBeChecked()
42+
})
43+
44+
})

0 commit comments

Comments
 (0)