Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[refactor] don’t bother spying on setState when lifecycle methods are…
… disabled
  • Loading branch information
ljharb committed Aug 17, 2018
commit 8b97f1fa50ede90d3b25c5e761c0b306f2798757
35 changes: 16 additions & 19 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,37 @@ class ShallowWrapper {
const adapter = getAdapter(options);
const lifecycles = getAdapterLifecycles(adapter);

let instance;
let renderedNode;
if (!root) {
privateSet(this, ROOT, this);
privateSet(this, UNRENDERED, nodes);
const renderer = adapter.createRenderer({ mode: 'shallow', ...options });
privateSet(this, RENDERER, renderer);
this[RENDERER].render(nodes, options.context);
({ instance } = this[RENDERER].getNode());
privateSetNodes(this, getRootNode(this[RENDERER].getNode()));
renderedNode = this[RENDERER].getNode();
privateSetNodes(this, getRootNode(renderedNode));
} else {
privateSet(this, ROOT, root);
privateSet(this, UNRENDERED, null);
privateSet(this, RENDERER, root[RENDERER]);
privateSetNodes(this, nodes);
renderedNode = this[RENDERER].getNode();
}
privateSet(this, OPTIONS, root ? root[OPTIONS] : options);

if (!instance) {
({ instance } = this[RENDERER].getNode());
}
// Ensure to call componentDidUpdate when instance.setState is called
if (instance && lifecycles.componentDidUpdate.onSetState && !instance[SET_STATE]) {
privateSet(instance, SET_STATE, instance.setState);
instance.setState = (...args) => this.setState(...args);
}
const { instance } = renderedNode;
if (instance && !options.disableLifecycleMethods) {
// Ensure to call componentDidUpdate when instance.setState is called
if (lifecycles.componentDidUpdate.onSetState && !instance[SET_STATE]) {
privateSet(instance, SET_STATE, instance.setState);
instance.setState = (...args) => this.setState(...args);
}

if (
!options.disableLifecycleMethods
&& instance
&& typeof instance.componentDidMount === 'function'
) {
this[RENDERER].batchedUpdates(() => {
instance.componentDidMount();
});
if (typeof instance.componentDidMount === 'function') {
this[RENDERER].batchedUpdates(() => {
instance.componentDidMount();
});
}
}
}

Expand Down