1
1
import React , { useState } from 'react' ;
2
- import { act , render , waitFor } from '@testing-library/react-native' ;
2
+ import { act , render , waitFor , screen } from '@testing-library/react-native' ;
3
3
import Picker from '../index' ;
4
4
import { PickerDriver } from '../Picker.driver.new' ;
5
5
const countries = [
@@ -27,38 +27,26 @@ const getDriver = (props?: any) => {
27
27
return PickerDriver ( { renderTree : render ( < TestCase { ...props } /> ) , testID} ) ;
28
28
} ;
29
29
30
+ const onPress = jest . fn ( ) ;
30
31
const onDismiss = jest . fn ( ) ;
32
+ const onShow = jest . fn ( ) ;
31
33
32
34
describe ( 'Picker' , ( ) => {
33
35
beforeEach ( ( ) => {
34
36
jest . clearAllMocks ( ) ;
35
37
} ) ;
36
38
37
39
describe ( 'Modal' , ( ) => {
38
- describe ( 'Test value' , ( ) => {
39
- it ( 'Get correct value of a single item' , ( ) => {
40
- const driver = getDriver ( { value : countries [ 2 ] . value } ) ;
41
- expect ( driver . getValue ( ) ) . toEqual ( countries [ 2 ] . label ) ;
42
- } ) ;
43
-
44
- it ( 'Get correct value of multiple selected items' , ( ) => {
45
- const driver = getDriver ( { value : [ countries [ 2 ] . value , countries [ 4 ] . value ] } ) ;
46
- expect ( driver . getValue ( ) ) . toEqual ( `${ countries [ 2 ] . label } , ${ countries [ 4 ] . label } ` ) ;
47
- } ) ;
48
- } ) ;
49
-
50
40
describe ( 'Test open' , ( ) => {
51
41
it ( 'Should open when enabled' , ( ) => {
52
42
const driver = getDriver ( ) ;
53
-
54
43
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
55
44
driver . open ( ) ;
56
45
expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
57
46
} ) ;
58
47
59
48
it ( 'Should not open when disabled' , ( ) => {
60
49
const driver = getDriver ( { editable : false } ) ;
61
-
62
50
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
63
51
driver . open ( ) ;
64
52
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
@@ -67,19 +55,28 @@ describe('Picker', () => {
67
55
68
56
it ( 'Test close' , ( ) => {
69
57
const driver = getDriver ( ) ;
70
-
71
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
72
58
driver . open ( ) ;
73
59
expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
74
60
driver . cancel ( ) ;
75
61
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
76
62
} ) ;
77
63
64
+ describe ( 'Test value' , ( ) => {
65
+ it ( 'Get correct value of a single item' , ( ) => {
66
+ const driver = getDriver ( { value : countries [ 2 ] . value } ) ;
67
+ expect ( driver . getValue ( ) ) . toEqual ( countries [ 2 ] . label ) ;
68
+ } ) ;
69
+
70
+ it ( 'Get correct value of multiple selected items' , ( ) => {
71
+ const driver = getDriver ( { value : [ countries [ 2 ] . value , countries [ 4 ] . value ] } ) ;
72
+ expect ( driver . getValue ( ) ) . toEqual ( `${ countries [ 2 ] . label } , ${ countries [ 4 ] . label } ` ) ;
73
+ } ) ;
74
+ } ) ;
75
+
78
76
describe ( 'Test selection' , ( ) => {
79
77
it ( 'Should select a single item' , ( ) => {
80
78
const driver = getDriver ( ) ;
81
79
expect ( driver . getValue ( ) ) . toEqual ( '' ) ;
82
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
83
80
driver . open ( ) ;
84
81
expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
85
82
driver . selectItem ( countries [ 2 ] . label ) ;
@@ -90,7 +87,6 @@ describe('Picker', () => {
90
87
it ( 'Should select multiple items' , ( ) => {
91
88
const driver = getDriver ( { mode : 'MULTI' } ) ;
92
89
expect ( driver . getValue ( ) ) . toEqual ( '' ) ;
93
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
94
90
driver . open ( ) ;
95
91
expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
96
92
driver . selectItem ( countries [ 2 ] . label ) ;
@@ -100,122 +96,199 @@ describe('Picker', () => {
100
96
} ) ;
101
97
} ) ;
102
98
103
- it ( 'Test onDismiss' , ( ) => {
104
- const driver = getDriver ( {
105
- pickerModalProps : {
106
- onDismiss
107
- }
99
+ it ( 'Test onPress' , ( ) => {
100
+ const driver = getDriver ( { onPress} ) ;
101
+ driver . open ( ) ;
102
+ expect ( onPress ) . toHaveBeenCalled ( ) ;
103
+ } ) ;
104
+
105
+ describe ( 'Test pickerModalProps' , ( ) => {
106
+ it ( 'Test onDismiss' , ( ) => {
107
+ const driver = getDriver ( {
108
+ pickerModalProps : {
109
+ onDismiss
110
+ }
111
+ } ) ;
112
+ driver . open ( ) ;
113
+ expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
114
+ driver . cancel ( ) ;
115
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
116
+ expect ( onDismiss ) . toHaveBeenCalledTimes ( 2 ) ; // TODO: this should be 1
108
117
} ) ;
109
118
110
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
111
- driver . open ( ) ;
112
- expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
113
- driver . cancel ( ) ;
114
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
115
- expect ( onDismiss ) . toHaveBeenCalledTimes ( 2 ) ; // TODO: this should be 1
119
+ // TODO: this test is not passing yet
120
+ // The onShow function get's called when the Modal is fully opened, tried to use act and waitFor to wait for fully open but it didn't work
121
+ it . skip ( 'Test onShow passed via pickerModalProps' , async ( ) => {
122
+ const driver = getDriver ( {
123
+ pickerModalProps : {
124
+ onShow
125
+ }
126
+ } ) ;
127
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
128
+ jest . useFakeTimers ( ) ;
129
+ expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
130
+ expect ( onShow ) . toHaveBeenCalled ( ) ;
131
+ } ) ;
132
+
133
+ describe ( 'Test Modal TopBar' , ( ) => {
134
+ const modalProps = { mode : 'MULTI' , topBarProps : { cancelLabel : 'Cancel' } } ;
135
+ it ( 'should close and select items when pressing on done button' , ( ) => {
136
+ const driver = getDriver ( modalProps ) ;
137
+ driver . open ( ) ;
138
+ expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
139
+ driver . selectItem ( countries [ 2 ] . label ) ;
140
+ driver . selectItem ( countries [ 4 ] . label ) ;
141
+ driver . done ( ) ;
142
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
143
+ expect ( driver . getValue ( ) ) . toEqual ( `${ countries [ 2 ] . label } , ${ countries [ 4 ] . label } ` ) ;
144
+ } ) ;
145
+
146
+ it ( 'should close without selecting items when pressing on cancel button' , ( ) => {
147
+ const driver = getDriver ( modalProps ) ;
148
+ driver . open ( ) ;
149
+ expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
150
+ driver . selectItem ( countries [ 2 ] . label ) ;
151
+ driver . selectItem ( countries [ 4 ] . label ) ;
152
+ driver . cancel ( ) ;
153
+ expect ( driver . getValue ( ) ) . toEqual ( `` ) ;
154
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
155
+ } ) ;
156
+ } ) ;
116
157
} ) ;
117
158
} ) ;
118
159
119
- // TODO: this is a work in progress, the tests are not passing yet
120
- describe . skip ( 'Dialog' , ( ) => {
160
+ describe ( 'Dialog' , ( ) => {
161
+ const dialogProps = { useDialog : true , customPickerProps : { migrateDialog : true } } ;
121
162
describe ( 'Test value' , ( ) => {
122
163
it ( 'Get correct value of a single item' , ( ) => {
123
164
const driver = getDriver ( {
124
- useDialog : true ,
125
- customPickerProps : { migrateDialog : true } ,
165
+ ...dialogProps ,
126
166
value : countries [ 2 ] . value
127
167
} ) ;
128
168
expect ( driver . getValue ( ) ) . toEqual ( countries [ 2 ] . label ) ;
129
169
} ) ;
130
170
131
171
it ( 'Get correct value of multiple selected items' , ( ) => {
132
172
const driver = getDriver ( {
133
- useDialog : true ,
134
- customPickerProps : { migrateDialog : true } ,
173
+ ...dialogProps ,
135
174
value : [ countries [ 2 ] . value , countries [ 4 ] . value ]
136
175
} ) ;
137
176
expect ( driver . getValue ( ) ) . toEqual ( `${ countries [ 2 ] . label } , ${ countries [ 4 ] . label } ` ) ;
138
177
} ) ;
139
178
} ) ;
140
179
141
- describe ( 'Test open' , ( ) => {
180
+ describe ( 'Test open/close ' , ( ) => {
142
181
it ( 'Should open when enabled' , async ( ) => {
143
- const driver = getDriver ( { useDialog : true , customPickerProps : { migrateDialog : true } } ) ;
144
-
182
+ const driver = getDriver ( dialogProps ) ;
145
183
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
146
- // driver.open();
147
- // expect(driver.isOpen()).toBeTruthy();
148
184
jest . useFakeTimers ( ) ;
149
185
act ( ( ) => driver . open ( ) ) ;
150
186
jest . runAllTimers ( ) ;
151
- // advanceAnimationByTime(10000);
152
- // await new Promise(r => setTimeout(r, 1000));
153
187
await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ) ;
154
188
} ) ;
155
189
156
190
it ( 'Should not open when disabled' , ( ) => {
157
- const driver = getDriver ( { useDialog : true , customPickerProps : { migrateDialog : true } , editable : false } ) ;
158
-
191
+ const driver = getDriver ( { ...dialogProps , editable : false } ) ;
159
192
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
160
193
driver . open ( ) ;
161
194
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
162
195
} ) ;
163
- } ) ;
164
-
165
- it ( 'Test close' , ( ) => {
166
- const driver = getDriver ( { useDialog : true , customPickerProps : { migrateDialog : true } } ) ;
167
196
168
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
169
- driver . open ( ) ;
170
- expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
171
- driver . cancel ( ) ;
172
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
197
+ it ( 'Test close' , async ( ) => {
198
+ const driver = getDriver ( dialogProps ) ;
199
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
200
+ act ( ( ) => driver . open ( ) ) ;
201
+ await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ) ;
202
+ act ( ( ) => driver . dismissDialog ( ) ) ;
203
+ await waitFor ( ( ) => expect ( driver . dismissDialog ( ) ) . toBeFalsy ( ) ) ;
204
+ } ) ;
173
205
} ) ;
174
206
175
207
describe ( 'Test selection' , ( ) => {
176
- it ( 'Should select a single item' , ( ) => {
177
- const driver = getDriver ( { useDialog : true , customPickerProps : { migrateDialog : true } } ) ;
178
- expect ( driver . getValue ( ) ) . toEqual ( undefined ) ;
179
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
180
- driver . open ( ) ;
181
- expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
208
+ it ( 'Should select a single item' , async ( ) => {
209
+ const driver = getDriver ( dialogProps ) ;
210
+ expect ( driver . getValue ( ) ) . toEqual ( '' ) ;
211
+ act ( ( ) => driver . open ( ) ) ;
212
+ await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ) ;
182
213
driver . selectItem ( countries [ 2 ] . label ) ;
183
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
214
+ act ( ( ) => driver . dismissDialog ( ) ) ;
215
+ await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ) ;
184
216
expect ( driver . getValue ( ) ) . toEqual ( countries [ 2 ] . label ) ;
185
217
} ) ;
186
218
187
- it ( 'Should select multiple items' , ( ) => {
188
- const driver = getDriver ( { useDialog : true , customPickerProps : { migrateDialog : true } , mode : 'MULTI' } ) ;
189
- expect ( driver . getValue ( ) ) . toEqual ( undefined ) ;
219
+ it ( 'Should select multiple items' , async ( ) => {
220
+ const driver = getDriver ( { ... dialogProps , mode : 'MULTI' } ) ;
221
+ expect ( driver . getValue ( ) ) . toEqual ( '' ) ;
190
222
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
191
- driver . open ( ) ;
192
- expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
223
+ act ( ( ) => driver . open ( ) ) ;
224
+ await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ) ;
193
225
driver . selectItem ( countries [ 2 ] . label ) ;
194
226
driver . selectItem ( countries [ 4 ] . label ) ;
195
227
driver . done ( ) ;
228
+ await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ) ;
196
229
expect ( driver . getValue ( ) ) . toEqual ( `${ countries [ 2 ] . label } , ${ countries [ 4 ] . label } ` ) ;
197
230
} ) ;
198
231
} ) ;
199
232
200
- it ( 'Test onDismiss' , ( ) => {
233
+ it ( 'Test onDismiss' , async ( ) => {
201
234
const driver = getDriver ( {
202
- useDialog : true ,
203
- customPickerProps : { migrateDialog : true } ,
204
- pickerModalProps : {
205
- onDismiss
235
+ ...dialogProps ,
236
+ customPickerProps : {
237
+ dialogProps : {
238
+ onDismiss
239
+ }
206
240
}
207
241
} ) ;
208
-
209
242
expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
210
- driver . open ( ) ;
211
- expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ;
212
- driver . cancel ( ) ;
213
- expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
214
- expect ( onDismiss ) . toHaveBeenCalledTimes ( 2 ) ; // TODO: this should be 1
243
+ act ( ( ) => driver . open ( ) ) ;
244
+ await waitFor ( ( ) => expect ( driver . isOpen ( ) ) . toBeTruthy ( ) ) ;
245
+ act ( ( ) => driver . dismissDialog ( ) ) ;
246
+ await waitFor ( ( ) => expect ( driver . dismissDialog ( ) ) . toBeFalsy ( ) ) ;
247
+ expect ( onDismiss ) . toHaveBeenCalledTimes ( 1 ) ;
215
248
} ) ;
216
249
} ) ;
217
250
218
- // TODO: add tests for WheelPicker as well
251
+ // TODO: add tests for WheelPicker
252
+ // Note: the picker dialog should be opened and then the wheel picker should be rendered, this is the main issue with testing it for now
219
253
// describe.skip('WheelPicker', () => {
220
254
// });
255
+
256
+ //TODO: add more tests for different props
257
+ describe ( 'Picker field types' , ( ) => {
258
+ describe ( 'Test filter field type' , ( ) => {
259
+ const placeholderText = 'Select a Filter' ;
260
+ it ( 'should render a filter picker' , ( ) => {
261
+ const driver = getDriver ( { fieldType : 'filter' , placeholder : placeholderText } ) ;
262
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
263
+ const label = screen . getByTestId ( `${ testID } .filter.type.label` ) ;
264
+ expect ( label ) . toBeTruthy ( ) ;
265
+ expect ( label . props . children ) . toEqual ( placeholderText ) ;
266
+ } ) ;
267
+ } ) ;
268
+ describe ( 'Test settings field type' , ( ) => {
269
+ const labelText = 'Settings' ;
270
+ const placeholderText = 'Select a setting' ;
271
+ it ( 'should render a settings picker with label' , async ( ) => {
272
+ const driver = getDriver ( { fieldType : 'settings' , label : labelText , placeholder : placeholderText } ) ;
273
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
274
+ const label = screen . getByTestId ( `${ testID } .settings.type.label` ) ;
275
+ const placeholder = screen . getByTestId ( `${ testID } .settings.type.placeholder` ) ;
276
+ expect ( label ) . toBeTruthy ( ) ;
277
+ expect ( placeholder ) . toBeTruthy ( ) ;
278
+ expect ( label . props . children ) . toEqual ( labelText ) ;
279
+ expect ( placeholder . props . children ) . toEqual ( labelText ) ;
280
+ } ) ;
281
+
282
+ it ( 'should render a settings picker with placeholder' , async ( ) => {
283
+ const driver = getDriver ( { fieldType : 'settings' , placeholder : placeholderText } ) ;
284
+ expect ( driver . isOpen ( ) ) . toBeFalsy ( ) ;
285
+ const label = screen . getByTestId ( `${ testID } .settings.type.label` ) ;
286
+ const placeholder = screen . getByTestId ( `${ testID } .settings.type.placeholder` ) ;
287
+ expect ( label ) . toBeTruthy ( ) ;
288
+ expect ( placeholder ) . toBeTruthy ( ) ;
289
+ expect ( label . props . children ) . toEqual ( undefined ) ;
290
+ expect ( placeholder . props . children ) . toEqual ( placeholderText ) ;
291
+ } ) ;
292
+ } ) ;
293
+ } ) ;
221
294
} ) ;
0 commit comments