@@ -333,6 +333,83 @@ describe('ReactTestUtils', () => {
333
333
expect ( result ) . toEqual ( < div /> ) ;
334
334
} ) ;
335
335
336
+ it ( 'passes expected params to component lifecycle methods' , ( ) => {
337
+ const componentDidUpdateParams = [ ] ;
338
+ const componentWillReceivePropsParams = [ ] ;
339
+ const componentWillUpdateParams = [ ] ;
340
+ const setStateParams = [ ] ;
341
+ const shouldComponentUpdateParams = [ ] ;
342
+
343
+ const initialProp = { prop : 'init prop' } ;
344
+ const initialState = { state : 'init state' } ;
345
+ const initialContext = { context : 'init context' } ;
346
+ const updatedState = { state : 'updated state' } ;
347
+ const updatedProp = { prop : 'updated prop' } ;
348
+ const updatedContext = { context : 'updated context' } ;
349
+
350
+ class SimpleComponent extends React . Component {
351
+ constructor ( props , context ) {
352
+ super ( props , context ) ;
353
+ this . state = initialState ;
354
+ }
355
+ componentDidUpdate ( ...args ) {
356
+ componentDidUpdateParams . push ( ...args ) ;
357
+ }
358
+ componentWillReceiveProps ( ...args ) {
359
+ componentWillReceivePropsParams . push ( ...args ) ;
360
+ this . setState ( ( ...args ) => {
361
+ setStateParams . push ( ...args ) ;
362
+ return updatedState ;
363
+ } ) ;
364
+ }
365
+ componentWillUpdate ( ...args ) {
366
+ componentWillUpdateParams . push ( ...args ) ;
367
+ }
368
+ shouldComponentUpdate ( ...args ) {
369
+ shouldComponentUpdateParams . push ( ...args ) ;
370
+ return true ;
371
+ }
372
+ render ( ) {
373
+ return null ;
374
+ }
375
+ }
376
+
377
+ const shallowRenderer = createRenderer ( ) ;
378
+
379
+ // No lifecycle hooks should be invoked on initial render
380
+ shallowRenderer . render (
381
+ React . createElement ( SimpleComponent , initialProp ) ,
382
+ initialContext ,
383
+ ) ;
384
+ expect ( componentDidUpdateParams ) . toEqual ( [ ] ) ;
385
+ expect ( componentWillReceivePropsParams ) . toEqual ( [ ] ) ;
386
+ expect ( componentWillUpdateParams ) . toEqual ( [ ] ) ;
387
+ expect ( setStateParams ) . toEqual ( [ ] ) ;
388
+ expect ( shouldComponentUpdateParams ) . toEqual ( [ ] ) ;
389
+
390
+ // Lifecycle hooks should be invoked with the correct prev/next params on update.
391
+ shallowRenderer . render (
392
+ React . createElement ( SimpleComponent , updatedProp ) ,
393
+ updatedContext ,
394
+ ) ;
395
+ expect ( componentWillReceivePropsParams ) . toEqual ( [
396
+ updatedProp ,
397
+ updatedContext ,
398
+ ] ) ;
399
+ expect ( setStateParams ) . toEqual ( [ initialState , initialProp ] ) ;
400
+ expect ( shouldComponentUpdateParams ) . toEqual ( [
401
+ updatedProp ,
402
+ updatedState ,
403
+ updatedContext ,
404
+ ] ) ;
405
+ expect ( componentWillUpdateParams ) . toEqual ( [
406
+ updatedProp ,
407
+ updatedState ,
408
+ updatedContext ,
409
+ ] ) ;
410
+ expect ( componentDidUpdateParams ) . toEqual ( [ initialProp , initialState ] ) ;
411
+ } ) ;
412
+
336
413
it ( 'can shallowly render components with ref as function' , ( ) => {
337
414
class SimpleComponent extends React . Component {
338
415
state = { clicked : false } ;
0 commit comments