Skip to content

Commit

Permalink
Add test for hasClass used on SFCs, Remove limitation for use of getD…
Browse files Browse the repository at this point in the history
…OMNode on SFCs
  • Loading branch information
nfcampos authored and Nuno Campos committed Dec 22, 2016
1 parent 6456995 commit 4d0eb62
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
9 changes: 1 addition & 8 deletions src/ReactWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
typeOfNode,
displayNameOfNode,
ITERATOR_SYMBOL,
isFunctionalComponent,
} from './Utils';
import {
debugInsts,
Expand Down Expand Up @@ -139,13 +138,7 @@ class ReactWrapper {
* @returns {DOMComponent}
*/
getDOMNode() {
return this.single('getDOMNode', (n) => {
if (isFunctionalComponent(n)) {
throw new TypeError('Method “getDOMNode” cannot be used on functional components.');
}

return findDOMNode(n);
});
return this.single('getDOMNode', findDOMNode);
}

/**
Expand Down
39 changes: 35 additions & 4 deletions test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,21 @@ describeWithDOM('mount', () => {
});
});

context('When using a Composite component', () => {
describeIf(!REACT013, 'with stateless components', () => {
it('should return whether or not node has a certain class', () => {
const Foo = () => <div className="foo bar baz some-long-string FoOo" />;
const wrapper = mount(<Foo />);

expect(wrapper.hasClass('foo')).to.equal(true);
expect(wrapper.hasClass('bar')).to.equal(true);
expect(wrapper.hasClass('baz')).to.equal(true);
expect(wrapper.hasClass('some-long-string')).to.equal(true);
expect(wrapper.hasClass('FoOo')).to.equal(true);
expect(wrapper.hasClass('doesnt-exist')).to.equal(false);
});
});

context('When using a Composite class component', () => {
it('should return whether or not node has a certain class', () => {
class Foo extends React.Component {
render() {
Expand Down Expand Up @@ -3389,11 +3403,28 @@ describeWithDOM('mount', () => {
});

describeIf(!REACT013, 'stateless components', () => {
const SFC = () => (<div />);
const SFC = () => (
<div className="outer">
<div className="inner">
<span />
<span />
</div>
</div>
);

it('should throw when wrapping an SFC', () => {
it('should return the outer most DOMComponent of the root wrapper', () => {
const wrapper = mount(<SFC />);
expect(() => wrapper.getDOMNode()).to.throw(TypeError, 'Method “getDOMNode” cannot be used on functional components.');
expect(wrapper.getDOMNode()).to.have.property('className', 'outer');
});

it('should return the outer most DOMComponent of the inner div wrapper', () => {
const wrapper = mount(<SFC />);
expect(wrapper.find('.inner').getDOMNode()).to.have.property('className', 'inner');
});

it('should throw when wrapping multiple elements', () => {
const wrapper = mount(<SFC />).find('span');
expect(() => wrapper.getDOMNode()).to.throw(Error);
});
});
});
Expand Down

0 comments on commit 4d0eb62

Please sign in to comment.