From d5dc1609f4046f457f1508c200ed4212bef43fcf Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Mon, 2 Jul 2018 11:28:38 +0900 Subject: [PATCH] Support getSnapshotBeforeUpdate --- packages/enzyme/src/ShallowWrapper.js | 33 ++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 219418a19..0f41a3f18 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -288,27 +288,24 @@ class ShallowWrapper { if ( shouldRender && !this[OPTIONS].disableLifecycleMethods && - instance && - typeof instance.componentDidUpdate === 'function' + instance ) { + // getSnapshotBeforeUpdate + let snapshot; if ( - !this[OPTIONS].disableLifecycleMethods && - instance + adapter.options.supportGetSnapshotBeforeUpdate + && typeof instance.getSnapshotBeforeUpdate === 'function' ) { - if ( - adapter.options.supportGetSnapshotBeforeUpdate - && typeof instance.getSnapshotBeforeUpdate === 'function' - ) { - const snapshot = instance.getSnapshotBeforeUpdate(prevProps, state); - if (typeof instance.componentDidUpdate === 'function') { - instance.componentDidUpdate(prevProps, state, snapshot); - } - } else if (typeof instance.componentDidUpdate === 'function') { - if (adapter.options.supportPrevContextArgumentOfComponentDidUpdate) { - instance.componentDidUpdate(prevProps, state, prevContext); - } else { - instance.componentDidUpdate(prevProps, state); - } + snapshot = instance.getSnapshotBeforeUpdate(prevProps, state); + } + // componentDidUpdate + if (typeof instance.componentDidUpdate === 'function') { + if (adapter.options.supportGetSnapshotBeforeUpdate) { + instance.componentDidUpdate(prevProps, state, snapshot); + } else if (adapter.options.supportPrevContextArgumentOfComponentDidUpdate) { + instance.componentDidUpdate(prevProps, state, prevContext); + } else { + instance.componentDidUpdate(prevProps, state); } } // If it doesn't need to rerender, update only its props.