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

Support for Stateless Components #53

Merged
merged 1 commit into from
Dec 6, 2015
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
4 changes: 2 additions & 2 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class ReactWrapper {
/>
);
this.root = this;
this.node = this.component.refs.component;
this.node = this.component.getWrappedComponent();
this.nodes = [this.node];
this.length = 1;
} else {
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class ReactWrapper {
* @returns {ReactComponent}
*/
instance() {
return this.component.refs.component;
return this.component.getInstance();
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,30 @@ export default class ReactWrapperComponent extends React.Component {
return new Promise(resolve => this.setState(newProps, resolve));
}

getInstance() {
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 inst;
}

getWrappedComponent() {
const component = this._reactInternalInstance._renderedComponent;
const inst = component.getPublicInstance();
if (inst === null) {
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 case where inst is falsy but not null? if not, you could return component.getPublicInstance() || component? nbd

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

AFAICT its always null

return component;
}
return inst;
}

render() {
const { Component } = this.props;
return (
<Component ref="component" {...this.state} />
<Component {...this.state} />
);
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@ import {
ReactWrapper,
describeWithDOM,
} from '../';
import { describeIf } from './_helpers';
import { REACT013 } from '../version';

describeWithDOM('mount', () => {

describeIf(!REACT013, 'stateless components', () => {
it('works with stateless components', () => {
const Foo = ({ foo }) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);
const wrapper = mount(<Foo foo="qux" />);
expect(wrapper.type()).to.equal(Foo);
expect(wrapper.find('.bar')).to.have.length(1);
expect(wrapper.find('.qoo').text()).to.equal('qux');
});
});

describe('.contains(node)', () => {

it('should allow matches on the root node', () => {
Expand Down
18 changes: 17 additions & 1 deletion src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ import React from 'react';
import { expect } from 'chai';
import { shallow, render, ShallowWrapper } from '../';
import sinon from 'sinon';

import { describeIf } from './_helpers';
import { REACT013 } from '../version';

describe('shallow', () => {

describeIf(!REACT013, 'stateless components', () => {
it('works with stateless components', () => {
const Foo = ({ foo }) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);
const wrapper = shallow(<Foo foo="qux" />);
expect(wrapper.type()).to.equal('div');
expect(wrapper.find('.bar')).to.have.length(1);
expect(wrapper.find('.qoo').text()).to.equal('qux');
});
});

describe('.contains(node)', () => {

it('should allow matches on the root node', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/_helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Simple wrapper around mocha describe which allows a boolean to be passed in first which
* determines whether or not the test will be run
*/
export function describeIf(test, a, b) {
if (test) {
describe(a, b);
} else {
describe.skip(a, b);
}
}