@@ -7,11 +7,12 @@ import { Checkbox, Radio, Switch } from './checkbox';
77
88afterEach ( cleanup ) ;
99
10- describe ( 'Checkbox, Radio & Switch components ' , ( ) => {
10+ const AGREE_TERMS = 'I agree to terms and conditions' ;
11+
12+ describe ( 'Checkbox component ' , ( ) => {
1113 const CHECKBOX_LABEL = 'checkbox-label' ;
12- const AGREE_TERMS = 'I agree to terms and conditions' ;
13- const RADIO_LABEL = 'radio-label' ;
14- const SWITCH_LABEL = 'switch-label' ;
14+
15+
1516 it ( '<Checkbox /> renders correctly and call on change on Press' , ( ) => {
1617 const mockOnChange = jest . fn ( ( checked ) => checked ) ;
1718 render (
@@ -84,6 +85,33 @@ describe('Checkbox, Radio & Switch components ', () => {
8485 expect ( mockOnChange ) . toHaveBeenCalledTimes ( 0 ) ;
8586 } ) ;
8687
88+ it ( '<Checkbox /> should not render label when empty or not provided' , ( ) => {
89+ const mockOnChange = jest . fn ( ( checked ) => checked ) ;
90+ render ( < Checkbox testID = "checkbox" label = "" onChange = { mockOnChange }
91+ accessibilityLabel = "agree" /> ) ;
92+ expect ( screen . queryByTestId ( CHECKBOX_LABEL ) ) . not . toBeOnTheScreen ( ) ;
93+ } ) ;
94+
95+ it ( '<Checkbox /> renders as checked when checked prop is true' , ( ) => {
96+ const mockOnChange = jest . fn ( ( checked ) => checked ) ;
97+ render (
98+ < Checkbox
99+ testID = "checkbox"
100+ onChange = { mockOnChange }
101+ checked = { true }
102+ accessibilityLabel = "agree"
103+ accessibilityHint = "toggle Agree"
104+ />
105+ ) ;
106+ expect ( screen . getByTestId ( 'checkbox' ) ) . toBeChecked ( ) ;
107+ fireEvent . press ( screen . getByTestId ( 'checkbox' ) ) ;
108+ expect ( mockOnChange ) . toHaveBeenCalledWith ( false ) ; // Checkbox should toggle to unchecked
109+ } ) ;
110+ } ) ;
111+
112+ describe ( 'Radio component ' , ( ) => {
113+ const RADIO_LABEL = 'radio-label' ;
114+
87115 it ( '<Radio /> renders correctly and call on change on Press' , ( ) => {
88116 const mockOnChange = jest . fn ( ( checked ) => checked ) ;
89117 render (
@@ -147,6 +175,33 @@ describe('Checkbox, Radio & Switch components ', () => {
147175 expect ( mockOnChange ) . toHaveBeenCalledTimes ( 0 ) ;
148176 } ) ;
149177
178+ it ( '<Radio /> should not render label when empty or not provided' , ( ) => {
179+ const mockOnChange = jest . fn ( ( checked ) => checked ) ;
180+ render ( < Radio testID = "radio" label = "" onChange = { mockOnChange }
181+ accessibilityLabel = "agree" /> ) ;
182+ expect ( screen . queryByTestId ( RADIO_LABEL ) ) . not . toBeOnTheScreen ( ) ;
183+ } ) ;
184+
185+ it ( '<Radio /> renders as checked when checked prop is true' , ( ) => {
186+ const mockOnChange = jest . fn ( ( checked ) => checked ) ;
187+ render (
188+ < Radio
189+ testID = "radio"
190+ onChange = { mockOnChange }
191+ checked = { true }
192+ accessibilityLabel = "agree"
193+ accessibilityHint = "toggle Agree"
194+ />
195+ ) ;
196+ expect ( screen . getByTestId ( 'radio' ) ) . toBeChecked ( ) ;
197+ fireEvent . press ( screen . getByTestId ( 'radio' ) ) ;
198+ expect ( mockOnChange ) . toHaveBeenCalledWith ( false ) ; // Radio should toggle to unchecked
199+ } ) ;
200+ } )
201+
202+ describe ( 'Switch component ' , ( ) => {
203+ const SWITCH_LABEL = 'switch-label' ;
204+
150205 it ( '<Switch /> renders correctly and call on change on Press' , ( ) => {
151206 const mockOnChange = jest . fn ( ( checked ) => checked ) ;
152207 render (
@@ -209,73 +264,27 @@ describe('Checkbox, Radio & Switch components ', () => {
209264 fireEvent . press ( screen . getByTestId ( 'switch' ) ) ;
210265 expect ( mockOnChange ) . toHaveBeenCalledTimes ( 0 ) ;
211266 } ) ;
212-
213- it ( '<Checkbox /> should not render label when empty or not provided' , ( ) => {
214- const mockOnChange = jest . fn ( ( checked ) => checked ) ;
215- render ( < Checkbox testID = "checkbox" label = "" onChange = { mockOnChange }
216- accessibilityLabel = "agree" /> ) ;
217- expect ( screen . queryByTestId ( CHECKBOX_LABEL ) ) . not . toBeOnTheScreen ( ) ;
218- } ) ;
219-
220- it ( '<Radio /> should not render label when empty or not provided' , ( ) => {
221- const mockOnChange = jest . fn ( ( checked ) => checked ) ;
222- render ( < Radio testID = "radio" label = "" onChange = { mockOnChange }
223- accessibilityLabel = "agree" /> ) ;
224- expect ( screen . queryByTestId ( RADIO_LABEL ) ) . not . toBeOnTheScreen ( ) ;
225- } ) ;
226267
227268 it ( '<Switch /> should not render label when empty or not provided' , ( ) => {
228269 const mockOnChange = jest . fn ( ( checked ) => checked ) ;
229270 render ( < Switch testID = "switch" label = "" onChange = { mockOnChange }
230271 accessibilityLabel = "agree" /> ) ;
231272 expect ( screen . queryByTestId ( SWITCH_LABEL ) ) . not . toBeOnTheScreen ( ) ;
232273 } ) ;
233- } ) ;
234-
235- it ( '<Checkbox /> renders as checked when checked prop is true' , ( ) => {
236- const mockOnChange = jest . fn ( ( checked ) => checked ) ;
237- render (
238- < Checkbox
239- testID = "checkbox"
240- onChange = { mockOnChange }
241- checked = { true }
242- accessibilityLabel = "agree"
243- accessibilityHint = "toggle Agree"
244- />
245- ) ;
246- expect ( screen . getByTestId ( 'checkbox' ) ) . toBeChecked ( ) ;
247- fireEvent . press ( screen . getByTestId ( 'checkbox' ) ) ;
248- expect ( mockOnChange ) . toHaveBeenCalledWith ( false ) ; // Checkbox should toggle to unchecked
249- } ) ;
250274
251- it ( '<Radio /> renders as checked when checked prop is true' , ( ) => {
252- const mockOnChange = jest . fn ( ( checked ) => checked ) ;
253- render (
254- < Radio
255- testID = "radio"
256- onChange = { mockOnChange }
257- checked = { true }
258- accessibilityLabel = "agree"
259- accessibilityHint = "toggle Agree"
260- />
261- ) ;
262- expect ( screen . getByTestId ( 'radio' ) ) . toBeChecked ( ) ;
263- fireEvent . press ( screen . getByTestId ( 'radio' ) ) ;
264- expect ( mockOnChange ) . toHaveBeenCalledWith ( false ) ; // Radio should toggle to unchecked
265- } ) ;
266-
267- it ( '<Switch /> renders as checked when checked prop is true' , ( ) => {
268- const mockOnChange = jest . fn ( ( checked ) => checked ) ;
269- render (
270- < Switch
271- testID = "switch"
272- onChange = { mockOnChange }
273- checked = { true }
274- accessibilityLabel = "agree"
275- accessibilityHint = "toggle Agree"
276- />
277- ) ;
278- expect ( screen . getByTestId ( 'switch' ) . props . accessibilityState . checked ) . toBe ( true ) ;
279- fireEvent . press ( screen . getByTestId ( 'switch' ) ) ;
280- expect ( mockOnChange ) . toHaveBeenCalledWith ( false ) ; // Switch should toggle to unchecked
281- } ) ;
275+ it ( '<Switch /> renders as checked when checked prop is true' , ( ) => {
276+ const mockOnChange = jest . fn ( ( checked ) => checked ) ;
277+ render (
278+ < Switch
279+ testID = "switch"
280+ onChange = { mockOnChange }
281+ checked = { true }
282+ accessibilityLabel = "agree"
283+ accessibilityHint = "toggle Agree"
284+ />
285+ ) ;
286+ expect ( screen . getByTestId ( 'switch' ) . props . accessibilityState . checked ) . toBe ( true ) ;
287+ fireEvent . press ( screen . getByTestId ( 'switch' ) ) ;
288+ expect ( mockOnChange ) . toHaveBeenCalledWith ( false ) ; // Switch should toggle to unchecked
289+ } ) ;
290+ } )
0 commit comments