1
1
import React , { useState } from 'react' ;
2
- import { waitFor } from '@testing-library/react-native' ;
2
+ import { waitFor , render } from '@testing-library/react-native' ;
3
3
import View from '../../view' ;
4
4
import Text from '../../text' ;
5
5
import Button from '../index' ;
6
6
import { ImageSourcePropType } from 'react-native' ;
7
- import { ButtonDriver } from '../Button.driver' ;
8
- import { TextDriver } from '../../text/Text.driver' ;
7
+ import { ButtonDriver } from '../Button.driver.new ' ;
8
+ import { TextDriver } from '../../text/Text.driver.new ' ;
9
9
10
10
const BUTTON_ID = 'button_test_id' ;
11
11
const CHILDREN_TEXT_ID = 'children_test_id' ;
12
12
const CHILDREN_TEXT = 'custom button text' ;
13
13
14
14
// TODO: This tests are flaky and only fail on CI - we should investigate why
15
15
describe ( 'Button' , ( ) => {
16
- afterEach ( ( ) => {
17
- ButtonDriver . clear ( ) ;
18
- } ) ;
19
-
20
16
it ( 'should render a button' , async ( ) => {
21
- const component = WrapperScreenWithButton ( ) ;
22
- const buttonDriver = new ButtonDriver ( { component, testID : BUTTON_ID } ) ;
23
-
17
+ const renderTree = render ( < WrapperScreenWithButton /> ) ;
18
+ const buttonDriver = ButtonDriver ( { renderTree, testID : 'button_test_id' } ) ;
24
19
expect ( await buttonDriver . exists ( ) ) . toBeTruthy ( ) ;
25
20
} ) ;
26
21
27
22
describe ( 'custom button' , ( ) => {
28
23
it ( 'should render a custom button' , async ( ) => {
29
- const component = WrapperScreenWithCustomButton ( ) ;
30
- const buttonDriver = new ButtonDriver ( { component, testID : BUTTON_ID } ) ;
31
-
24
+ const renderTree = render ( < WrapperScreenWithCustomButton /> ) ;
25
+ const buttonDriver = ButtonDriver ( { renderTree, testID : 'button_test_id' } ) ;
32
26
expect ( buttonDriver . exists ( ) ) . toBeTruthy ( ) ;
33
27
} ) ;
34
28
35
29
it ( 'should render the children with correct text' , async ( ) => {
36
- const component = WrapperScreenWithCustomButton ( ) ;
37
- const buttonDriver = new ButtonDriver ( { component, testID : BUTTON_ID } ) ;
38
-
30
+ const renderTree = render ( < WrapperScreenWithCustomButton /> ) ;
31
+ const buttonDriver = ButtonDriver ( { renderTree, testID : 'button_test_id' } ) ;
39
32
expect ( buttonDriver . exists ( ) ) . toBeTruthy ( ) ;
40
-
41
- const childrenTextDriver = new TextDriver ( { component, testID : CHILDREN_TEXT_ID } ) ;
42
-
43
- expect ( await childrenTextDriver . getTextContent ( ) ) . toEqual ( CHILDREN_TEXT ) ;
33
+ const childrenTextDriver = TextDriver ( { renderTree, testID : CHILDREN_TEXT_ID } ) ;
34
+ expect ( await childrenTextDriver . getText ( ) ) . toEqual ( CHILDREN_TEXT ) ;
44
35
} ) ;
45
36
} ) ;
46
37
47
38
describe ( 'OnPress' , ( ) => {
48
39
let onPressCallback : jest . Mock ;
49
- beforeEach ( ( ) => onPressCallback = jest . fn ( ) ) ;
40
+ beforeEach ( ( ) => ( onPressCallback = jest . fn ( ) ) ) ;
50
41
afterEach ( ( ) => onPressCallback . mockClear ( ) ) ;
51
42
52
- it . skip ( 'should trigger onPress callback' , async ( ) => {
53
- const component = WrapperScreenWithButton ( { onPress : onPressCallback } ) ;
54
- const buttonDriver = new ButtonDriver ( { component , testID : BUTTON_ID } ) ;
55
-
43
+ it ( 'should trigger onPress callback' , async ( ) => {
44
+ const props = { onPress : onPressCallback } ;
45
+ const renderTree = render ( < WrapperScreenWithButton { ... props } /> ) ;
46
+ const buttonDriver = ButtonDriver ( { renderTree , testID : 'button_test_id' } ) ;
56
47
buttonDriver . press ( ) ;
57
-
58
48
await waitFor ( ( ) => expect ( onPressCallback ) . toHaveBeenCalledTimes ( 1 ) ) ;
59
49
} ) ;
60
50
61
51
it ( 'should not trigger onPress callback if button disabled' , async ( ) => {
62
- const component = WrapperScreenWithButton ( { onPress : onPressCallback , disabled : true } ) ;
63
- const buttonDriver = new ButtonDriver ( { component , testID : BUTTON_ID } ) ;
64
-
52
+ const props = { disabled : true , onPress : onPressCallback } ;
53
+ const renderTree = render ( < WrapperScreenWithButton { ... props } /> ) ;
54
+ const buttonDriver = ButtonDriver ( { renderTree , testID : 'button_test_id' } ) ;
65
55
buttonDriver . press ( ) ;
66
-
67
56
await waitFor ( ( ) => expect ( onPressCallback ) . toHaveBeenCalledTimes ( 0 ) ) ;
68
57
} ) ;
69
58
} ) ;
70
59
71
60
describe ( 'label' , ( ) => {
72
61
const LABEL = 'mock label' ;
73
62
it ( 'should render a button with correct content' , async ( ) => {
74
- const component = WrapperScreenWithButton ( { label : LABEL } ) ;
75
- const buttonDriver = new ButtonDriver ( { component , testID : BUTTON_ID } ) ;
76
-
77
- expect ( await buttonDriver . getLabelContent ( ) ) . toEqual ( LABEL ) ;
63
+ const props = { label : LABEL } ;
64
+ const renderTree = render ( < WrapperScreenWithButton { ... props } /> ) ;
65
+ const buttonDriver = ButtonDriver ( { renderTree , testID : 'button_test_id' } ) ;
66
+ expect ( await buttonDriver . getLabel ( ) . getText ( ) ) . toEqual ( LABEL ) ;
78
67
} ) ;
79
68
80
69
it ( 'should render a button with correct label content. ' , async ( ) => {
81
- const component = WrapperScreenWithButton ( { label : LABEL } ) ;
82
- const buttonDriver = new ButtonDriver ( { component , testID : BUTTON_ID } ) ;
83
-
84
- expect ( await buttonDriver . getLabelContent ( ) ) . toEqual ( LABEL ) ;
70
+ const props = { label : LABEL } ;
71
+ const renderTree = render ( < WrapperScreenWithButton { ... props } /> ) ;
72
+ const buttonDriver = ButtonDriver ( { renderTree , testID : 'button_test_id' } ) ;
73
+ expect ( await buttonDriver . getLabel ( ) . getText ( ) ) . toEqual ( LABEL ) ;
85
74
} ) ;
86
75
87
76
it ( 'should render a button without label. ' , async ( ) => {
88
- const component = WrapperScreenWithButton ( ) ;
89
- const buttonDriver = new ButtonDriver ( { component, testID : BUTTON_ID } ) ;
90
-
91
- expect ( await buttonDriver . isLabelExists ( ) ) . toBeFalsy ( ) ;
77
+ const renderTree = render ( < WrapperScreenWithButton /> ) ;
78
+ const buttonDriver = ButtonDriver ( { renderTree, testID : 'button_test_id' } ) ;
79
+ expect ( await buttonDriver . getLabel ( ) . exists ( ) ) . toBeFalsy ( ) ;
92
80
} ) ;
93
81
} ) ;
94
82
95
83
describe ( 'icon' , ( ) => {
96
84
it ( 'should render a button without an icon. ' , async ( ) => {
97
- const component = WrapperScreenWithButton ( ) ;
98
- const buttonDriver = new ButtonDriver ( { component, testID : BUTTON_ID } ) ;
99
-
100
- expect ( await buttonDriver . isIconExists ( ) ) . toBeFalsy ( ) ;
85
+ const renderTree = render ( < WrapperScreenWithButton /> ) ;
86
+ const buttonDriver = ButtonDriver ( { renderTree, testID : 'button_test_id' } ) ;
87
+ expect ( await buttonDriver . getIcon ( ) . exists ( ) ) . toBeFalsy ( ) ;
101
88
} ) ;
102
89
103
90
it ( 'should render a button with icon. ' , async ( ) => {
104
91
const ICON = 12 ;
105
- const component = WrapperScreenWithButton ( { iconSource : ICON } ) ;
106
- const buttonDriver = new ButtonDriver ( { component, testID : BUTTON_ID } ) ;
92
+ const props = { iconSource : ICON } ;
93
+ const renderTree = render ( < WrapperScreenWithButton { ...props } /> ) ;
94
+ const buttonDriver = ButtonDriver ( { renderTree, testID : 'button_test_id' } ) ;
107
95
108
- expect ( await buttonDriver . isIconExists ( ) ) . toBeTruthy ( ) ;
96
+ expect ( await buttonDriver . getIcon ( ) . exists ( ) ) . toBeTruthy ( ) ;
109
97
} ) ;
110
98
} ) ;
111
99
112
100
describe ( 'more complicated screen' , ( ) => {
113
101
//todo take it out of this file. to the demo screens maybe
114
102
it ( 'should change text values according to state changes from buttons pressing' , async ( ) => {
115
- const component = StatefulScreenWithTextsAndButtonss ( ) ;
116
- const text1Driver = new TextDriver ( { testID : `text_1` , component } ) ;
117
- const text2Driver = new TextDriver ( { testID : `text_2` , component } ) ;
118
- const button1Driver = new ButtonDriver ( { testID : `${ BUTTON_ID } 1 ` , component } ) ;
119
- const button2Driver = new ButtonDriver ( { testID : `${ BUTTON_ID } 2 ` , component } ) ;
103
+ const renderTree = render ( StatefulScreenWithTextsAndButtonss ( ) ) ;
104
+ const text1Driver = TextDriver ( { testID : `text_1` , renderTree } ) ;
105
+ const text2Driver = TextDriver ( { testID : `text_2` , renderTree } ) ;
106
+ const button2Driver = ButtonDriver ( { testID : `${ BUTTON_ID } 2 ` , renderTree } ) ;
107
+ const button1Driver = ButtonDriver ( { testID : `${ BUTTON_ID } 1 ` , renderTree } ) ;
120
108
121
- expect ( await text1Driver . getTextContent ( ) ) . toBe ( 'button 1 pressed 0 times' ) ;
122
- expect ( await text2Driver . getTextContent ( ) ) . toBe ( 'button 2 pressed 0 times' ) ;
109
+ expect ( await text1Driver . getText ( ) ) . toBe ( 'button 1 pressed 0 times' ) ;
110
+ expect ( await text2Driver . getText ( ) ) . toBe ( 'button 2 pressed 0 times' ) ;
123
111
124
112
await button1Driver . press ( ) ;
125
113
await button1Driver . press ( ) ;
126
114
await button2Driver . press ( ) ;
127
115
128
- await waitFor ( async ( ) => expect ( await text1Driver . getTextContent ( ) ) . toBe ( 'button 1 pressed 2 times' ) ) ;
129
- await waitFor ( async ( ) => expect ( await text2Driver . getTextContent ( ) ) . toBe ( 'button 2 pressed 1 times' ) ) ;
116
+ await waitFor ( async ( ) => expect ( await text1Driver . getText ( ) ) . toBe ( 'button 1 pressed 2 times' ) ) ;
117
+ await waitFor ( async ( ) => expect ( await text2Driver . getText ( ) ) . toBe ( 'button 2 pressed 1 times' ) ) ;
130
118
} ) ;
131
119
} ) ;
132
120
} ) ;
133
121
134
122
function WrapperScreenWithButton ( buttonProps : {
135
- onPress ?: ( ) => void ;
136
- label ?: string ;
137
- iconSource ?: ImageSourcePropType ;
138
- disabled ?: boolean ;
139
- } = { } ) {
123
+ onPress ?: ( ) => void ;
124
+ label ?: string ;
125
+ iconSource ?: ImageSourcePropType ;
126
+ disabled ?: boolean ;
127
+ } = { } ) {
140
128
const { onPress, label, iconSource, disabled} = buttonProps ;
141
- return ( < View testID = { 'wrapper_screen_test_id' } >
142
- < Button testID = { BUTTON_ID } onPress = { onPress } label = { label } iconSource = { iconSource } disabled = { disabled } />
143
- </ View > ) ;
129
+ return (
130
+ < View testID = { 'wrapper_screen_test_id' } >
131
+ < Button testID = { BUTTON_ID } onPress = { onPress } label = { label } iconSource = { iconSource } disabled = { disabled } />
132
+ </ View >
133
+ ) ;
144
134
}
145
135
146
136
function WrapperScreenWithCustomButton ( buttonProps : { onPress ?: ( ) => void } = { } ) {
147
137
const { onPress} = buttonProps ;
148
- return ( < View testID = { 'wrapper_screen_test_id' } >
149
- < Button testID = { BUTTON_ID } onPress = { onPress } >
150
- < Text testID = { CHILDREN_TEXT_ID } > { CHILDREN_TEXT } </ Text >
151
- </ Button >
152
- </ View > ) ;
138
+ return (
139
+ < View testID = { 'wrapper_screen_test_id' } >
140
+ < Button testID = { BUTTON_ID } onPress = { onPress } >
141
+ < Text testID = { CHILDREN_TEXT_ID } > { CHILDREN_TEXT } </ Text >
142
+ </ Button >
143
+ </ View >
144
+ ) ;
153
145
}
154
146
155
147
const StatefulScreenWithTextsAndButtonss = ( ) => < StatefulScreenWithTextsAndButtons /> ;
@@ -159,8 +151,8 @@ const StatefulScreenWithTextsAndButtons = () => {
159
151
const [ count2 , setCount2 ] = useState ( 0 ) ;
160
152
return (
161
153
< View testID = { 'stateful_wrapper_screen_test_id' } >
162
- < Text testID = { 'text_1' } > { `button 1 pressed ${ count1 } times` } </ Text >
163
- < Text testID = { 'text_2' } > { `button 2 pressed ${ count2 } times` } </ Text >
154
+ < Text testID = { 'text_1' } > { `button 1 pressed ${ count1 } times` } </ Text >
155
+ < Text testID = { 'text_2' } > { `button 2 pressed ${ count2 } times` } </ Text >
164
156
< Button testID = { `${ BUTTON_ID } 1` } onPress = { ( ) => setCount1 ( count1 + 1 ) } />
165
157
< Button testID = { `${ BUTTON_ID } 2` } onPress = { ( ) => setCount2 ( count2 + 1 ) } />
166
158
</ View >
0 commit comments