@@ -7,127 +7,127 @@ import { render, screen, fireEvent } from '@testing-library/react';
77import { Switch } from './Switch' ;
88
99describe ( 'Switch' , ( ) => {
10- it ( 'renders with unchecked state' , ( ) => {
11- const mockHandler = vi . fn ( ) ;
12- render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
13-
14- const button = screen . getByRole ( 'switch' ) ;
15- expect ( button ) . toBeInTheDocument ( ) ;
16- expect ( button ) . toHaveAttribute ( 'aria-checked' , 'false' ) ;
17- } ) ;
18-
19- it ( 'renders with checked state' , ( ) => {
20- const mockHandler = vi . fn ( ) ;
21- render ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
22-
23- const button = screen . getByRole ( 'switch' ) ;
24- expect ( button ) . toHaveAttribute ( 'aria-checked' , 'true' ) ;
25- } ) ;
26-
27- it ( 'calls onCheckedChange when clicked' , ( ) => {
28- const mockHandler = vi . fn ( ) ;
29- render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
30-
31- const button = screen . getByRole ( 'switch' ) ;
32- fireEvent . click ( button ) ;
33-
34- expect ( mockHandler ) . toHaveBeenCalledWith ( true ) ;
35- expect ( mockHandler ) . toHaveBeenCalledTimes ( 1 ) ;
36- } ) ;
37-
38- it ( 'toggles from checked to unchecked' , ( ) => {
39- const mockHandler = vi . fn ( ) ;
40- render ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
41-
42- const button = screen . getByRole ( 'switch' ) ;
43- fireEvent . click ( button ) ;
44-
45- expect ( mockHandler ) . toHaveBeenCalledWith ( false ) ;
46- } ) ;
47-
48- it ( 'does not call handler when disabled' , ( ) => {
49- const mockHandler = vi . fn ( ) ;
50- render ( < Switch checked = { false } onCheckedChange = { mockHandler } disabled = { true } /> ) ;
51-
52- const button = screen . getByRole ( 'switch' ) ;
53- fireEvent . click ( button ) ;
54-
55- expect ( mockHandler ) . not . toHaveBeenCalled ( ) ;
56- } ) ;
57-
58- it ( 'applies disabled attribute when disabled' , ( ) => {
59- const mockHandler = vi . fn ( ) ;
60- render ( < Switch checked = { false } onCheckedChange = { mockHandler } disabled = { true } /> ) ;
61-
62- const button = screen . getByRole ( 'switch' ) ;
63- expect ( button ) . toBeDisabled ( ) ;
64- } ) ;
65-
66- it ( 'applies custom className' , ( ) => {
67- const mockHandler = vi . fn ( ) ;
68- render ( < Switch checked = { false } onCheckedChange = { mockHandler } className = "custom-class" /> ) ;
69-
70- const button = screen . getByRole ( 'switch' ) ;
71- expect ( button ) . toHaveClass ( 'custom-class' ) ;
72- } ) ;
73-
74- it ( 'applies data-testid when provided' , ( ) => {
75- const mockHandler = vi . fn ( ) ;
76- render ( < Switch checked = { false } onCheckedChange = { mockHandler } data-testid = "test-switch" /> ) ;
77-
78- const button = screen . getByTestId ( 'test-switch' ) ;
79- expect ( button ) . toBeInTheDocument ( ) ;
80- } ) ;
81-
82- it ( 'has correct styles for checked state' , ( ) => {
83- const mockHandler = vi . fn ( ) ;
84- render ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
85-
86- const button = screen . getByRole ( 'switch' ) ;
87- expect ( button . className ) . toMatch ( / b g - \[ r g b \( v a r \( - - p r i m a r y \) \) \] / ) ;
88- } ) ;
89-
90- it ( 'has correct styles for unchecked state' , ( ) => {
91- const mockHandler = vi . fn ( ) ;
92- render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
93-
94- const button = screen . getByRole ( 'switch' ) ;
95- expect ( button . className ) . toMatch ( / b g - g r a y - 3 0 0 / ) ;
96- } ) ;
97-
98- it ( 'has disabled styling when disabled' , ( ) => {
99- const mockHandler = vi . fn ( ) ;
100- render ( < Switch checked = { false } onCheckedChange = { mockHandler } disabled = { true } /> ) ;
101-
102- const button = screen . getByRole ( 'switch' ) ;
103- expect ( button . className ) . toMatch ( / o p a c i t y - 5 0 / ) ;
104- } ) ;
105-
106- it ( 'can be toggled multiple times' , ( ) => {
107- const mockHandler = vi . fn ( ) ;
108- const { rerender } = render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
109-
110- const button = screen . getByRole ( 'switch' ) ;
111-
112- // First click - should call with true
113- fireEvent . click ( button ) ;
114- expect ( mockHandler ) . toHaveBeenCalledWith ( true ) ;
115- expect ( mockHandler ) . toHaveBeenCalledTimes ( 1 ) ;
116-
117- // Simulate parent updating the prop
118- rerender ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
119-
120- // Second click - should call with false
121- fireEvent . click ( button ) ;
122- expect ( mockHandler ) . toHaveBeenCalledWith ( false ) ;
123- expect ( mockHandler ) . toHaveBeenCalledTimes ( 2 ) ;
124-
125- // Simulate parent updating the prop again
126- rerender ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
127-
128- // Third click - should call with true again
129- fireEvent . click ( button ) ;
130- expect ( mockHandler ) . toHaveBeenCalledWith ( true ) ;
131- expect ( mockHandler ) . toHaveBeenCalledTimes ( 3 ) ;
132- } ) ;
10+ it ( 'renders with unchecked state' , ( ) => {
11+ const mockHandler = vi . fn ( ) ;
12+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
13+
14+ const button = screen . getByRole ( 'switch' ) ;
15+ expect ( button ) . toBeInTheDocument ( ) ;
16+ expect ( button ) . toHaveAttribute ( 'aria-checked' , 'false' ) ;
17+ } ) ;
18+
19+ it ( 'renders with checked state' , ( ) => {
20+ const mockHandler = vi . fn ( ) ;
21+ render ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
22+
23+ const button = screen . getByRole ( 'switch' ) ;
24+ expect ( button ) . toHaveAttribute ( 'aria-checked' , 'true' ) ;
25+ } ) ;
26+
27+ it ( 'calls onCheckedChange when clicked' , ( ) => {
28+ const mockHandler = vi . fn ( ) ;
29+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
30+
31+ const button = screen . getByRole ( 'switch' ) ;
32+ fireEvent . click ( button ) ;
33+
34+ expect ( mockHandler ) . toHaveBeenCalledWith ( true ) ;
35+ expect ( mockHandler ) . toHaveBeenCalledTimes ( 1 ) ;
36+ } ) ;
37+
38+ it ( 'toggles from checked to unchecked' , ( ) => {
39+ const mockHandler = vi . fn ( ) ;
40+ render ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
41+
42+ const button = screen . getByRole ( 'switch' ) ;
43+ fireEvent . click ( button ) ;
44+
45+ expect ( mockHandler ) . toHaveBeenCalledWith ( false ) ;
46+ } ) ;
47+
48+ it ( 'does not call handler when disabled' , ( ) => {
49+ const mockHandler = vi . fn ( ) ;
50+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } disabled = { true } /> ) ;
51+
52+ const button = screen . getByRole ( 'switch' ) ;
53+ fireEvent . click ( button ) ;
54+
55+ expect ( mockHandler ) . not . toHaveBeenCalled ( ) ;
56+ } ) ;
57+
58+ it ( 'applies disabled attribute when disabled' , ( ) => {
59+ const mockHandler = vi . fn ( ) ;
60+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } disabled = { true } /> ) ;
61+
62+ const button = screen . getByRole ( 'switch' ) ;
63+ expect ( button ) . toBeDisabled ( ) ;
64+ } ) ;
65+
66+ it ( 'applies custom className' , ( ) => {
67+ const mockHandler = vi . fn ( ) ;
68+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } className = "custom-class" /> ) ;
69+
70+ const button = screen . getByRole ( 'switch' ) ;
71+ expect ( button ) . toHaveClass ( 'custom-class' ) ;
72+ } ) ;
73+
74+ it ( 'applies data-testid when provided' , ( ) => {
75+ const mockHandler = vi . fn ( ) ;
76+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } data-testid = "test-switch" /> ) ;
77+
78+ const button = screen . getByTestId ( 'test-switch' ) ;
79+ expect ( button ) . toBeInTheDocument ( ) ;
80+ } ) ;
81+
82+ it ( 'has correct styles for checked state' , ( ) => {
83+ const mockHandler = vi . fn ( ) ;
84+ render ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
85+
86+ const button = screen . getByRole ( 'switch' ) ;
87+ expect ( button . className ) . toMatch ( / b g - \[ r g b \( v a r \( - - p r i m a r y \) \) \] / ) ;
88+ } ) ;
89+
90+ it ( 'has correct styles for unchecked state' , ( ) => {
91+ const mockHandler = vi . fn ( ) ;
92+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
93+
94+ const button = screen . getByRole ( 'switch' ) ;
95+ expect ( button . className ) . toMatch ( / b g - g r a y - 3 0 0 / ) ;
96+ } ) ;
97+
98+ it ( 'has disabled styling when disabled' , ( ) => {
99+ const mockHandler = vi . fn ( ) ;
100+ render ( < Switch checked = { false } onCheckedChange = { mockHandler } disabled = { true } /> ) ;
101+
102+ const button = screen . getByRole ( 'switch' ) ;
103+ expect ( button . className ) . toMatch ( / o p a c i t y - 5 0 / ) ;
104+ } ) ;
105+
106+ it ( 'can be toggled multiple times' , ( ) => {
107+ const mockHandler = vi . fn ( ) ;
108+ const { rerender } = render ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
109+
110+ const button = screen . getByRole ( 'switch' ) ;
111+
112+ // First click - should call with true
113+ fireEvent . click ( button ) ;
114+ expect ( mockHandler ) . toHaveBeenCalledWith ( true ) ;
115+ expect ( mockHandler ) . toHaveBeenCalledTimes ( 1 ) ;
116+
117+ // Simulate parent updating the prop
118+ rerender ( < Switch checked = { true } onCheckedChange = { mockHandler } /> ) ;
119+
120+ // Second click - should call with false
121+ fireEvent . click ( button ) ;
122+ expect ( mockHandler ) . toHaveBeenCalledWith ( false ) ;
123+ expect ( mockHandler ) . toHaveBeenCalledTimes ( 2 ) ;
124+
125+ // Simulate parent updating the prop again
126+ rerender ( < Switch checked = { false } onCheckedChange = { mockHandler } /> ) ;
127+
128+ // Third click - should call with true again
129+ fireEvent . click ( button ) ;
130+ expect ( mockHandler ) . toHaveBeenCalledWith ( true ) ;
131+ expect ( mockHandler ) . toHaveBeenCalledTimes ( 3 ) ;
132+ } ) ;
133133} ) ;
0 commit comments