11/* eslint-disable max-lines-per-function */
22import React from 'react' ;
33
4- import { cleanup , fireEvent , render , screen , waitFor } from '@/core/test-utils' ;
4+ import { cleanup , fireEvent , render , screen } from '@/core/test-utils' ;
55
66import { Button } from './button' ;
77
88afterEach ( cleanup ) ;
99
1010describe ( 'Button component ' , ( ) => {
1111 it ( 'should render correctly ' , ( ) => {
12- render ( < Button testID = "test- button" /> ) ;
13- expect ( screen . queryByTestId ( 'test- button') ) . not . toBeNull ( ) ;
12+ render ( < Button testID = "button" /> ) ;
13+ expect ( screen . getByTestId ( ' button') ) . toBeOnTheScreen ( ) ;
1414 } ) ;
1515 it ( 'should render the label correctly' , ( ) => {
16- render ( < Button testID = "test-button" label = "Submit" /> ) ;
17- expect ( screen . queryByTestId ( 'test-button' ) ) . not . toBeNull ( ) ;
18- expect ( screen . queryByTestId ( 'test-button-label' ) ) . not . toBeNull ( ) ;
19- expect ( screen . queryByText ( 'Submit' ) ) . toBeTruthy ( ) ;
16+ render ( < Button testID = "button" label = "Submit" /> ) ;
17+ expect ( screen . getByTestId ( 'button' ) ) . toBeOnTheScreen ( ) ;
18+ expect ( screen . getByText ( 'Submit' ) ) . toBeOnTheScreen ( ) ;
2019 } ) ;
2120 it ( 'should render the loading indicator correctly' , ( ) => {
22- render ( < Button testID = "test- button" loading = { true } /> ) ;
23- expect ( screen . queryByTestId ( 'test- button') ) . not . toBeNull ( ) ;
24- expect ( screen . getByTestId ( 'test- button-activity-indicator' ) ) . not . toBeNull ( ) ;
21+ render ( < Button testID = "button" loading = { true } /> ) ;
22+ expect ( screen . getByTestId ( ' button') ) . toBeOnTheScreen ( ) ;
23+ expect ( screen . getByTestId ( 'button-activity-indicator' ) ) . toBeOnTheScreen ( ) ;
2524 } ) ;
2625 it ( 'should call onClick handler when clicked' , async ( ) => {
2726 const onClick = jest . fn ( ) ;
2827 render (
29- < Button testID = "test- button" label = "Click the button" onPress = { onClick } />
28+ < Button testID = "button" label = "Click the button" onPress = { onClick } />
3029 ) ;
31- expect ( screen . queryByTestId ( 'test- button') ) . not . toBeNull ( ) ;
32- fireEvent . press ( screen . getByTestId ( 'test- button' ) ) ;
33- await waitFor ( ( ) => expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ) ;
30+ expect ( screen . getByTestId ( ' button') ) . toBeOnTheScreen ( ) ;
31+ fireEvent . press ( screen . getByTestId ( 'button' ) ) ;
32+ expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
3433 } ) ;
3534 it ( 'should be disabled when loading' , ( ) => {
3635 const onClick = jest . fn ( ) ;
3736 render (
3837 < Button
39- testID = "test- button"
38+ testID = "button"
4039 loading = { true }
4140 label = "Click the button"
4241 onPress = { onClick }
4342 />
4443 ) ;
45- expect ( screen . queryByTestId ( 'test- button') ) . not . toBeNull ( ) ;
46- expect ( screen . getByTestId ( 'test- button-activity-indicator' ) ) . not . toBeNull ( ) ;
47- expect (
48- screen . getByTestId ( 'test-button' ) . props . accessibilityState . disabled
49- ) . toBe ( true ) ;
50- fireEvent . press ( screen . getByTestId ( 'test- button' ) ) ;
44+ expect ( screen . getByTestId ( ' button') ) . toBeOnTheScreen ( ) ;
45+ expect ( screen . getByTestId ( 'button-activity-indicator' ) ) . toBeOnTheScreen ( ) ;
46+ expect ( screen . getByTestId ( 'button' ) . props . accessibilityState . disabled ) . toBe (
47+ true
48+ ) ;
49+ fireEvent . press ( screen . getByTestId ( 'button' ) ) ;
5150 expect ( onClick ) . toHaveBeenCalledTimes ( 0 ) ;
5251 } ) ;
5352 it ( 'should be disabled when disabled prop is true' , ( ) => {
54- render ( < Button testID = "test- button" disabled = { true } /> ) ;
55- expect (
56- screen . getByTestId ( 'test-button' ) . props . accessibilityState . disabled
57- ) . toBe ( true ) ;
53+ render ( < Button testID = "button" disabled = { true } /> ) ;
54+ expect ( screen . getByTestId ( 'button' ) . props . accessibilityState . disabled ) . toBe (
55+ true
56+ ) ;
5857 } ) ;
5958 it ( "shouldn't call onClick when disabled" , ( ) => {
6059 const onClick = jest . fn ( ) ;
6160 render (
6261 < Button
63- testID = "test- button"
62+ testID = "button"
6463 label = "Click the button"
6564 disabled = { true }
6665 onPress = { onClick }
6766 variant = "secondary"
6867 />
6968 ) ;
70- expect ( screen . queryByTestId ( 'test- button') ) . not . toBeNull ( ) ;
71- fireEvent . press ( screen . getByTestId ( 'test- button' ) ) ;
72- expect (
73- screen . getByTestId ( 'test-button' ) . props . accessibilityState . disabled
74- ) . toBe ( true ) ;
69+ expect ( screen . getByTestId ( ' button') ) . not . toBeNull ( ) ;
70+ fireEvent . press ( screen . getByTestId ( 'button' ) ) ;
71+ expect ( screen . getByTestId ( 'button' ) . props . accessibilityState . disabled ) . toBe (
72+ true
73+ ) ;
7574 expect ( onClick ) . toHaveBeenCalledTimes ( 0 ) ;
7675 } ) ;
7776 it ( 'should apply correct styles based on size prop' , ( ) => {
78- render ( < Button testID = "test- button" size = "lg" /> ) ;
79- const button = screen . getByTestId ( 'test- button' ) ;
80-
77+ render ( < Button testID = "button" size = "lg" /> ) ;
78+ const button = screen . getByTestId ( 'button' ) ;
79+ // TODO: should be fixed to use haveStyle instead of comparing the class name
8180 const expectedStyle = 'font-[600] font-jakarta text-white text-xl' ;
8281 const receivedStyle =
8382 button . props . children [ 0 ] . props . children . props . className ;
8483 expect ( receivedStyle ) . toContain ( expectedStyle ) ;
8584 } ) ;
8685 it ( 'should apply correct styles for label when variant is secondary' , ( ) => {
87- render ( < Button testID = "test- button" variant = "secondary" label = "Submit" /> ) ;
88- const button = screen . getByTestId ( 'test- button' ) ;
86+ render ( < Button testID = "button" variant = "secondary" label = "Submit" /> ) ;
87+ const button = screen . getByTestId ( 'button' ) ;
8988
9089 const expectedStyle =
9190 'font-[600] font-jakarta text-secondary-600 text-base' ;
@@ -94,8 +93,8 @@ describe('Button component ', () => {
9493 expect ( receivedStyle ) . toContain ( expectedStyle ) ;
9594 } ) ;
9695 it ( 'should apply correct styles for label when is disabled' , ( ) => {
97- render ( < Button testID = "test- button" label = "Submit" disabled /> ) ;
98- const button = screen . getByTestId ( 'test- button' ) ;
96+ render ( < Button testID = "button" label = "Submit" disabled /> ) ;
97+ const button = screen . getByTestId ( 'button' ) ;
9998
10099 const expectedStyle = 'font-[600] font-jakarta text-base text-neutral-600' ;
101100 const receivedStyle =
0 commit comments