Skip to content

Commit f12be61

Browse files
committed
stub instance.setState when the component is mounting
1 parent 3b3ab38 commit f12be61

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

packages/enzyme/src/ShallowWrapper.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ class ShallowWrapper {
171171
privateSet(this, RENDERER, renderer);
172172
this[RENDERER].render(nodes, options.context);
173173
const { instance } = this[RENDERER].getNode();
174+
const adapter = getAdapter(this[OPTIONS]);
175+
const lifecycles = getAdapterLifecycles(adapter);
176+
// Ensure to call componentDidUpdate when instance.setState is called
177+
if (instance && lifecycles.componentDidUpdate.onSetState && !instance[SET_STATE]) {
178+
privateSet(instance, SET_STATE, instance.setState);
179+
instance.setState = (...args) => this.setState(...args);
180+
}
174181
if (
175182
!options.disableLifecycleMethods
176183
&& instance
@@ -264,16 +271,7 @@ class ShallowWrapper {
264271
if (this[ROOT] !== this) {
265272
throw new Error('ShallowWrapper::instance() can only be called on the root');
266273
}
267-
const adapter = getAdapter(this[OPTIONS]);
268-
const lifecycles = getAdapterLifecycles(adapter);
269-
270-
const { instance } = this[RENDERER].getNode();
271-
// Ensure to call componentDidUpdate when instance.setState is called
272-
if (instance && lifecycles.componentDidUpdate.onSetState && !instance[SET_STATE]) {
273-
privateSet(instance, SET_STATE, instance.setState);
274-
instance.setState = (...args) => this.setState(...args);
275-
}
276-
return instance;
274+
return this[RENDERER].getNode().instance;
277275
}
278276

279277
/**

0 commit comments

Comments
 (0)