Skip to content

Commit

Permalink
Support for Stateless Components
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Dec 5, 2015
1 parent d602bc1 commit 274731a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ReactWrapper.js
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
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) {
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
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
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
@@ -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);
}
}

0 comments on commit 274731a

Please sign in to comment.