@@ -66,7 +66,13 @@ function filterWhereUnwrapped(wrapper, predicate) {
6666 * @param {Object } options
6767 */
6868function validateOptions ( options ) {
69- const { lifecycleExperimental, disableLifecycleMethods } = options ;
69+ const {
70+ lifecycleExperimental,
71+ disableLifecycleMethods,
72+ enableComponentDidUpdateOnSetState,
73+ supportPrevContextArgumentOfComponentDidUpdate,
74+ lifecycles,
75+ } = options ;
7076 if (
7177 typeof lifecycleExperimental !== 'undefined' &&
7278 typeof lifecycleExperimental !== 'boolean'
@@ -88,6 +94,48 @@ function validateOptions(options) {
8894 ) {
8995 throw new Error ( 'lifecycleExperimental and disableLifecycleMethods cannot be set to the same value' ) ;
9096 }
97+
98+ if (
99+ typeof enableComponentDidUpdateOnSetState !== 'undefined' &&
100+ lifecycles . componentDidUpdate &&
101+ lifecycles . componentDidUpdate . onSetState !== enableComponentDidUpdateOnSetState
102+ ) {
103+ throw new TypeError ( 'the legacy enableComponentDidUpdateOnSetState option should be replaced by `lifecycles: { componentDidUpdate: { onSetState: true } }`' ) ;
104+ }
105+
106+ if (
107+ typeof supportPrevContextArgumentOfComponentDidUpdate !== 'undefined' &&
108+ lifecycles . componentDidUpdate &&
109+ lifecycles . componentDidUpdate . prevContext !== supportPrevContextArgumentOfComponentDidUpdate
110+ ) {
111+ throw new TypeError ( 'the legacy supportPrevContextArgumentOfComponentDidUpdate option should be replaced by `lifecycles: { componentDidUpdate: { prevContext: true } }`' ) ;
112+ }
113+ }
114+
115+ function getAdapterLifecycles ( { options } ) {
116+ const {
117+ lifecycles = { } ,
118+ enableComponentDidUpdateOnSetState,
119+ supportPrevContextArgumentOfComponentDidUpdate,
120+ } = options ;
121+
122+ const hasLegacySetStateArg = typeof enableComponentDidUpdateOnSetState !== 'undefined' ;
123+ const hasLegacyPrevContextArg = typeof supportPrevContextArgumentOfComponentDidUpdate !== 'undefined' ;
124+ const componentDidUpdate = hasLegacySetStateArg || hasLegacyPrevContextArg
125+ ? {
126+ ...( hasLegacySetStateArg && {
127+ onSetState : ! ! enableComponentDidUpdateOnSetState ,
128+ } ) ,
129+ ...( hasLegacyPrevContextArg && {
130+ prevContext : ! ! supportPrevContextArgumentOfComponentDidUpdate ,
131+ } ) ,
132+ }
133+ : null ;
134+
135+ return {
136+ ...lifecycles ,
137+ ...( componentDidUpdate && { componentDidUpdate } ) ,
138+ } ;
91139}
92140
93141function getRootNode ( node ) {
@@ -302,20 +350,24 @@ class ShallowWrapper {
302350 if ( originalShouldComponentUpdate ) {
303351 instance . shouldComponentUpdate = originalShouldComponentUpdate ;
304352 }
353+ const lifecycles = getAdapterLifecycles ( adapter ) ;
305354 if (
306355 ! this [ OPTIONS ] . disableLifecycleMethods &&
307356 instance
308357 ) {
309358 if (
310- adapter . options . supportGetSnapshotBeforeUpdate
359+ lifecycles . getSnapshotBeforeUpdate
311360 && typeof instance . getSnapshotBeforeUpdate === 'function'
312361 ) {
313362 const snapshot = instance . getSnapshotBeforeUpdate ( prevProps , state ) ;
314363 if ( typeof instance . componentDidUpdate === 'function' ) {
315364 instance . componentDidUpdate ( prevProps , state , snapshot ) ;
316365 }
317366 } else if ( typeof instance . componentDidUpdate === 'function' ) {
318- if ( adapter . options . supportPrevContextArgumentOfComponentDidUpdate ) {
367+ if (
368+ lifecycles . componentDidUpdate &&
369+ lifecycles . componentDidUpdate . prevContext
370+ ) {
319371 instance . componentDidUpdate ( prevProps , state , prevContext ) ;
320372 } else {
321373 instance . componentDidUpdate ( prevProps , state ) ;
@@ -379,6 +431,9 @@ class ShallowWrapper {
379431 this . single ( 'setState' , ( ) => {
380432 withSetStateAllowed ( ( ) => {
381433 const adapter = getAdapter ( this [ OPTIONS ] ) ;
434+
435+ const lifecycles = getAdapterLifecycles ( adapter ) ;
436+
382437 const instance = this . instance ( ) ;
383438 const prevProps = instance . props ;
384439 const prevState = instance . state ;
@@ -391,7 +446,8 @@ class ShallowWrapper {
391446 let originalShouldComponentUpdate ;
392447 if (
393448 ! this [ OPTIONS ] . disableLifecycleMethods &&
394- adapter . options . enableComponentDidUpdateOnSetState &&
449+ lifecycles . componentDidUpdate &&
450+ lifecycles . componentDidUpdate . onSetState &&
395451 instance &&
396452 typeof instance . shouldComponentUpdate === 'function'
397453 ) {
@@ -405,22 +461,24 @@ class ShallowWrapper {
405461 // We don't pass the setState callback here
406462 // to guarantee to call the callback after finishing the render
407463 instance . setState ( state ) ;
464+
408465 if (
409466 shouldRender &&
410467 ! this [ OPTIONS ] . disableLifecycleMethods &&
411- adapter . options . enableComponentDidUpdateOnSetState &&
468+ lifecycles . componentDidUpdate &&
469+ lifecycles . componentDidUpdate . onSetState &&
412470 instance
413471 ) {
414472 if (
415- adapter . options . supportGetSnapshotBeforeUpdate &&
473+ lifecycles . getSnapshotBeforeUpdate &&
416474 typeof instance . getSnapshotBeforeUpdate === 'function'
417475 ) {
418476 const snapshot = instance . getSnapshotBeforeUpdate ( prevProps , prevState ) ;
419477 if ( typeof instance . componentDidUpdate === 'function' ) {
420478 instance . componentDidUpdate ( prevProps , prevState , snapshot ) ;
421479 }
422480 } else if ( typeof instance . componentDidUpdate === 'function' ) {
423- if ( adapter . options . supportPrevContextArgumentOfComponentDidUpdate ) {
481+ if ( lifecycles . componentDidUpdate . prevContext ) {
424482 instance . componentDidUpdate ( prevProps , prevState , prevContext ) ;
425483 } else {
426484 instance . componentDidUpdate ( prevProps , prevState ) ;
0 commit comments