Skip to content

Commit

Permalink
[Tests] add createRef tests
Browse files Browse the repository at this point in the history
Closes #1704
  • Loading branch information
ljharb committed Aug 15, 2018
1 parent 1b0c212 commit d8a45b9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
42 changes: 40 additions & 2 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
createClass,
createContext,
createPortal,
createRef,
Fragment,
} from './_helpers/react-compat';
import {
Expand Down Expand Up @@ -4505,7 +4506,7 @@ describeWithDOM('mount', () => {
});

describe('.getElement()', () => {
it('returns nodes with refs as well as well', () => {
it('returns nodes with refs as well', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
Expand All @@ -4531,6 +4532,26 @@ describeWithDOM('mount', () => {
expect(wrapper.instance().node).to.equal(mockNode);
});

itIf(is('>= 16.3'), 'returns nodes with createRefs as well', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.setRef = createRef();
}

render() {
return (
<div>
<div ref={this.setRef} className="foo" />
</div>
);
}
}
const wrapper = mount(<Foo />);
const element = wrapper.find('.foo').instance();
expect(wrapper.instance().setRef).to.have.property('current', element);
});

it('does not add a "null" key to elements with a ref and no key', () => {
class Foo extends React.Component {
constructor(props) {
Expand All @@ -4549,7 +4570,24 @@ describeWithDOM('mount', () => {
}
}
const wrapper = mount(<Foo />);
expect(wrapper.getElement().key).to.equal(null);
expect(wrapper.getElement()).to.have.property('key', null);
});

itIf(is('>= 16.3'), 'does not add a "null" key to elements with a createRef and no key', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.setRef = createRef();
}

render() {
return (
<div ref={this.setRef} className="foo" />
);
}
}
const wrapper = mount(<Foo />);
expect(wrapper.getElement()).to.have.property('key', null);
});
});

Expand Down
42 changes: 40 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './_helpers/setupAdapters';
import {
createClass,
createContext,
createRef,
Fragment,
} from './_helpers/react-compat';
import {
Expand Down Expand Up @@ -5611,7 +5612,7 @@ describe('shallow', () => {
});

describe('.getElement()', () => {
it('returns nodes with refs as well as well', () => {
it('returns nodes with refs as well', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
Expand All @@ -5637,6 +5638,26 @@ describe('shallow', () => {
expect(wrapper.instance().node).to.equal(mockNode);
});

itIf(is('>= 16.3'), 'returns nodes with createRefs as well', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.setRef = createRef();
}

render() {
return (
<div>
<div ref={this.setRef} className="foo" />
</div>
);
}
}
const wrapper = shallow(<Foo />);
// shallow rendering does not invoke refs
expect(wrapper.instance().setRef).to.have.property('current', null);
});

it('does not add a "null" key to elements with a ref and no key', () => {
class Foo extends React.Component {
constructor(props) {
Expand All @@ -5655,7 +5676,24 @@ describe('shallow', () => {
}
}
const wrapper = shallow(<Foo />);
expect(wrapper.getElement().key).to.equal(null);
expect(wrapper.getElement()).to.have.property('key', null);
});

itIf(is('>= 16.3'), 'does not add a "null" key to elements with a createRef and no key', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.setRef = createRef();
}

render() {
return (
<div ref={this.setRef} className="foo" />
);
}
}
const wrapper = shallow(<Foo />);
expect(wrapper.getElement()).to.have.property('key', null);
});
});

Expand Down
4 changes: 4 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let createClass;
let renderToString;
let createPortal;
let createContext;
let createRef;
let forwardRef;
let Fragment;
let StrictMode;
Expand Down Expand Up @@ -45,12 +46,14 @@ if (is('^16.2.0-0')) {
if (is('^16.3.0-0')) {
({
createContext,
createRef,
forwardRef,
StrictMode,
unstable_AsyncMode: AsyncMode,
} = require('react'));
} else {
createContext = null;
createRef = null;
forwardRef = null;
StrictMode = null;
AsyncMode = null;
Expand All @@ -69,6 +72,7 @@ export {
renderToString,
createPortal,
createContext,
createRef,
forwardRef,
Fragment,
StrictMode,
Expand Down

0 comments on commit d8a45b9

Please sign in to comment.