Skip to content

Commit

Permalink
restore back-compat behavior for wrapper.node(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Oct 24, 2016
1 parent 5014baa commit 4cf36f2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
17 changes: 10 additions & 7 deletions src/ReactWrapper.jsx
Expand Up @@ -82,19 +82,22 @@ class ReactWrapper {
/>,
options);
this.root = this;
this.wrappedComponent = this.component.getWrappedComponent();
this.node = this.component.getWrappedComponent();
this.nodes = [this.node];
this.length = 1;
} else {
this.component = null;
this.root = root;
if (!nodes) {
this.initialNodes = [];
this.nodes = [];
} else if (!Array.isArray(nodes)) {
this.initialNodes = [nodes];
this.node = nodes;
this.nodes = [nodes];
} else {
this.initialNodes = nodes;
this.node = nodes[0];
this.nodes = nodes;
}
this.length = this.initialNodes.length;
this.length = this.nodes.length;
}
this.options = options;
this.complexSelector = new ComplexSelector(
Expand All @@ -115,7 +118,7 @@ class ReactWrapper {
'ReactWrapper::getNode() can only be called when wrapping one node'
);
}
return this.initialNodes ? this.initialNodes[0] : this.wrappedComponent;
return this.nodes[0];
}

/**
Expand All @@ -124,7 +127,7 @@ class ReactWrapper {
* @return {Array<ReactComponent>}
*/
getNodes() {
return this.initialNodes || [this.wrappedComponent];
return this.nodes;
}

/**
Expand Down
25 changes: 14 additions & 11 deletions src/ShallowWrapper.js
Expand Up @@ -84,17 +84,21 @@ class ShallowWrapper {
}
});
});
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
this.length = 1;
} else {
this.root = root;
this.unrendered = null;
this.renderer = null;
if (!Array.isArray(nodes)) {
this.initialNodes = [nodes];
this.node = nodes;
this.nodes = [nodes];
} else {
this.initialNodes = nodes;
this.node = nodes[0];
this.nodes = nodes;
}
this.length = this.initialNodes.length;
this.length = this.nodes.length;
}
this.options = options;
this.complexSelector = new ComplexSelector(buildPredicate, findWhereUnwrapped, childrenOfNode);
Expand All @@ -111,7 +115,7 @@ class ShallowWrapper {
'ShallowWrapper::getNode() can only be called when wrapping one node'
);
}
return this.initialNodes ? this.initialNodes[0] : this.renderer.getRenderOutput();
return this.root === this ? this.renderer.getRenderOutput() : this.node;
}

/**
Expand All @@ -120,7 +124,7 @@ class ShallowWrapper {
* @return {Array<ReactElement>}
*/
getNodes() {
return this.initialNodes || [this.renderer.getRenderOutput()];
return this.root === this ? [this.renderer.getRenderOutput()] : this.nodes;
}

/**
Expand Down Expand Up @@ -156,12 +160,8 @@ class ShallowWrapper {
throw new Error('ShallowWrapper::update() can only be called on the root');
}
this.single('update', () => {
const instance = this.instance();
if (instance) {
withSetStateAllowed(() => {
instance.forceUpdate();
});
}
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
});
return this;
}
Expand Down Expand Up @@ -227,6 +227,7 @@ class ShallowWrapper {
) {
instance.componentDidUpdate(prevProps, state, prevContext);
}
this.update();
}
if (originalComponentWillReceiveProps) {
instance.componentWillReceiveProps = originalComponentWillReceiveProps;
Expand Down Expand Up @@ -280,6 +281,7 @@ class ShallowWrapper {
this.single('setState', () => {
withSetStateAllowed(() => {
this.instance().setState(state, callback);
this.update();
});
});
return this;
Expand Down Expand Up @@ -571,6 +573,7 @@ class ShallowWrapper {
batchedUpdates(() => {
handler(...args);
});
this.root.update();
});
}
return this;
Expand Down
12 changes: 0 additions & 12 deletions test/ShallowWrapper-spec.jsx
Expand Up @@ -3788,10 +3788,6 @@ describe('shallow', () => {
});
}

mutateState() {
this.state.showSpan = true;
}

callbackSetState() {
this.safeSetState({ showSpan: true });
}
Expand All @@ -3801,7 +3797,6 @@ describe('shallow', () => {
<div>
{this.state && this.state.showSpan && <span className="show-me" />}
<button className="async-btn" onClick={() => this.asyncSetState()} />
<button className="mutates-btn" onClick={() => this.mutateState()} />
<Child callback={() => this.callbackSetState()} />
</div>
);
Expand All @@ -3822,13 +3817,6 @@ describe('shallow', () => {
wrapper.find(Child).props().callback();
expect(wrapper.find('.show-me').length).to.equal(1);
});

it('should have updated output after state mutation when .update() is called', () => {
const wrapper = shallow(<Test />);
wrapper.find('.mutates-btn').simulate('click');
wrapper.update();
expect(wrapper.find('.show-me').length).to.equal(1);
});
});

describe('#single()', () => {
Expand Down

0 comments on commit 4cf36f2

Please sign in to comment.