Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context method for SFCs #256

Merged
merged 1 commit into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export default class ReactWrapper {
* @returns {ReactComponent}
*/
instance() {
if (this.root !== this) {
throw new Error('ReactWrapper::instance() can only be called on the root');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any test for this throw?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not explicitly... though it would have thrown before (in a different, less informative way) anyway. This method on anything but the root doesn't make a whole lot of sense

}
return this.component.getInstance();
}

Expand Down
2 changes: 1 addition & 1 deletion src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function createWrapperComponent(node, options = {}) {
const component = this._reactInternalInstance._renderedComponent;
const inst = component.getPublicInstance();
if (inst === null) {
throw new Error('You cannot get an instance of a stateless component.');
return component._instance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly is component._instance in the case of a stateless component?

}
return inst;
},
Expand Down
22 changes: 22 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ describeWithDOM('mount', () => {
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});

describeIf(!REACT013, 'stateless components', () => {
it('works with stateless components', () => {
const Foo = ({ foo }, {_}) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);

Foo.contextTypes = {
_: React.PropTypes.string,
};

const wrapper = mount(<Foo foo="qux" />, {
context: {
_: 'foo',
},
});
expect(wrapper.context('_')).to.equal('foo');
});
});
});

describeIf(!REACT013, 'stateless components', () => {
Expand Down