@@ -2,6 +2,8 @@ import React from 'react';
2
2
import { mount } from 'enzyme' ;
3
3
import { components } from 'react-select' ;
4
4
import ReactSelect from 'react-select' ;
5
+ import isEqual from 'lodash/isEqual' ;
6
+
5
7
import DataDrivenSelect , { Select } from '../../form-fields/select/select' ;
6
8
7
9
describe ( '<Select />' , ( ) => {
@@ -166,4 +168,100 @@ describe('<Select />', () => {
166
168
} ) ;
167
169
} ) ;
168
170
} ) ;
171
+
172
+ describe ( 'reloading props' , ( ) => {
173
+ const NEW_OPTIONS = [ { label : 'Different label' , value : 2 } ] ;
174
+ let asyncLoading ;
175
+ let asyncLoadingNew ;
176
+
177
+ beforeEach ( ( ) => {
178
+ asyncLoading = ( ) => Promise . resolve ( initialProps . options ) ;
179
+ asyncLoadingNew = ( ) => Promise . resolve ( NEW_OPTIONS ) ;
180
+ } ) ;
181
+
182
+ it ( 'should change the options when options prop is changed' , ( ) => {
183
+ const wrapper = mount ( < Select { ...initialProps } /> ) ;
184
+
185
+ let innerSelectProps = wrapper . find ( ReactSelect ) . props ( ) . options ;
186
+
187
+ expect ( isEqual ( innerSelectProps , initialProps . options ) ) . toEqual ( true ) ;
188
+
189
+ wrapper . setProps ( { options : NEW_OPTIONS } ) ;
190
+ wrapper . update ( ) ;
191
+ innerSelectProps = wrapper . find ( ReactSelect ) . props ( ) . options ;
192
+
193
+ expect ( innerSelectProps ) . toEqual ( NEW_OPTIONS ) ;
194
+ } ) ;
195
+
196
+ it ( 'should change the options when loadOptions prop is changed' , ( done ) => {
197
+ const wrapper = mount ( < Select { ...initialProps } loadOptions = { asyncLoading } /> ) ;
198
+
199
+ setImmediate ( ( ) => {
200
+ wrapper . update ( ) ;
201
+ let innerSelectProps = wrapper . find ( ReactSelect ) . props ( ) . options ;
202
+
203
+ expect ( isEqual ( innerSelectProps , initialProps . options ) ) . toEqual ( true ) ;
204
+
205
+ wrapper . setProps ( { loadOptions : asyncLoadingNew } ) ;
206
+
207
+ setImmediate ( ( ) => {
208
+ wrapper . update ( ) ;
209
+ innerSelectProps = wrapper . find ( ReactSelect ) . props ( ) . options ;
210
+
211
+ expect ( isEqual ( innerSelectProps , NEW_OPTIONS ) ) . toEqual ( true ) ;
212
+ done ( ) ;
213
+ } ) ;
214
+ } ) ;
215
+ } ) ;
216
+
217
+ it ( 'should change the value when new options do not include it' , ( ) => {
218
+ const wrapper = mount ( < Select { ...initialProps } value = { 1 } /> ) ;
219
+
220
+ wrapper . setProps ( { options : NEW_OPTIONS } ) ;
221
+ wrapper . update ( ) ;
222
+
223
+ expect ( onChange ) . toHaveBeenCalledWith ( undefined ) ;
224
+ } ) ;
225
+
226
+ it ( 'not should change the value when new options include it' , ( ) => {
227
+ const wrapper = mount ( < Select { ...initialProps } value = { 2 } /> ) ;
228
+
229
+ wrapper . setProps ( { options : NEW_OPTIONS } ) ;
230
+ wrapper . update ( ) ;
231
+
232
+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
233
+ } ) ;
234
+
235
+ it ( 'should reset the value when loadOptions prop is changed and new options do not include the value' , ( done ) => {
236
+ const wrapper = mount ( < Select { ...initialProps } loadOptions = { asyncLoading } value = { 1 } /> ) ;
237
+
238
+ setImmediate ( ( ) => {
239
+ wrapper . update ( ) ;
240
+ wrapper . setProps ( { loadOptions : asyncLoadingNew } ) ;
241
+
242
+ setImmediate ( ( ) => {
243
+ wrapper . update ( ) ;
244
+
245
+ expect ( onChange ) . toHaveBeenCalledWith ( undefined ) ;
246
+ done ( ) ;
247
+ } ) ;
248
+ } ) ;
249
+ } ) ;
250
+
251
+ it ( 'should not reset the value when loadOptions prop is changed and new options includes the value' , ( done ) => {
252
+ const wrapper = mount ( < Select { ...initialProps } loadOptions = { asyncLoading } value = { 2 } /> ) ;
253
+
254
+ setImmediate ( ( ) => {
255
+ wrapper . update ( ) ;
256
+ wrapper . setProps ( { loadOptions : asyncLoadingNew } ) ;
257
+
258
+ setImmediate ( ( ) => {
259
+ wrapper . update ( ) ;
260
+
261
+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
262
+ done ( ) ;
263
+ } ) ;
264
+ } ) ;
265
+ } ) ;
266
+ } ) ;
169
267
} ) ;
0 commit comments