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

Change renderProp API #1891

Merged
merged 1 commit into from
Nov 8, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions docs/api/ReactWrapper/renderProp.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# `.renderProp(propName, ...args) => ReactWrapper`
# `.renderProp(propName)(...args) => ReactWrapper`

Calls the current wrapper's property with name `propName` and the `args` provided.
Returns the result in a new wrapper.
Returns a function that, when called with arguments `args`, will return a new wrapper based on the render prop in the original wrapper's prop `propName`.

NOTE: can only be called on wrapper of a single non-DOM component element node.

Expand Down Expand Up @@ -69,7 +68,7 @@ const App = () => (
```jsx
const wrapper = mount(<App />)
.find(Mouse)
.renderProp('render');
.renderProp('render')();

expect(wrapper.equals(<h1>The mouse position is 0, 0</h1>)).to.equal(true);
```
Expand All @@ -79,7 +78,7 @@ expect(wrapper.equals(<h1>The mouse position is 0, 0</h1>)).to.equal(true);
```jsx
const wrapper = mount(<App />)
.find(Mouse)
.renderProp('render', 10, 20);
.renderProp('render')(10, 20);

expect(wrapper.equals(<h1>The mouse position is 10, 20</h1>)).to.equal(true);
```
9 changes: 4 additions & 5 deletions docs/api/ShallowWrapper/renderProp.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# `.renderProp(propName, ...args) => ShallowWrapper`
# `.renderProp(propName)(...args) => ShallowWrapper`

Calls the current wrapper's property with name `propName` and the `args` provided.
Returns the result in a new wrapper.
Returns a function that, when called with arguments `args`, will return a new wrapper based on the render prop in the original wrapper's prop `propName`.

NOTE: can only be called on wrapper of a single non-DOM component element node.

Expand Down Expand Up @@ -69,7 +68,7 @@ const App = () => (
```jsx
const wrapper = shallow(<App />)
.find(Mouse)
.renderProp('render');
.renderProp('render')();

expect(wrapper.equals(<h1>The mouse position is 0, 0</h1>)).to.equal(true);
```
Expand All @@ -79,7 +78,7 @@ expect(wrapper.equals(<h1>The mouse position is 0, 0</h1>)).to.equal(true);
```jsx
const wrapper = shallow(<App />)
.find(Mouse)
.renderProp('render', 10, 20);
.renderProp('render')(10, 20);

expect(wrapper.equals(<h1>The mouse position is 10, 20</h1>)).to.equal(true);
```
2 changes: 1 addition & 1 deletion docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Get a wrapper with the first ancestor of the current node to match the provided
#### [`.render() => CheerioWrapper`](ReactWrapper/render.md)
Returns a CheerioWrapper of the current node's subtree.

#### [`.renderProp(key) => ReactWrapper`](ReactWrapper/renderProp.md)
#### [`.renderProp(key)() => ReactWrapper`](ReactWrapper/renderProp.md)
Returns a wrapper of the node rendered by the provided render prop.

#### [`.text() => String`](ReactWrapper/text.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Shallow renders the current node and returns a shallow wrapper around it.
#### [`.render() => CheerioWrapper`](ShallowWrapper/render.md)
Returns a CheerioWrapper of the current node's subtree.

#### [`.renderProp(key) => ShallowWrapper`](ShallowWrapper/renderProp.md)
#### [`.renderProp(key)() => ShallowWrapper`](ShallowWrapper/renderProp.md)
Returns a wrapper of the node rendered by the provided render prop.

#### [`.unmount() => ShallowWrapper`](ShallowWrapper/unmount.md)
Expand Down
6 changes: 3 additions & 3 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4974,17 +4974,17 @@ describeWithDOM('mount', () => {
}

const wrapperA = mount(<div><Bar render={() => <div><Foo /></div>} /></div>);
const renderPropWrapperA = wrapperA.find(Bar).renderProp('render');
const renderPropWrapperA = wrapperA.find(Bar).renderProp('render')();
expect(renderPropWrapperA.find(Foo)).to.have.lengthOf(1);

const wrapperB = mount(<div><Bar render={() => <Foo />} /></div>);
const renderPropWrapperB = wrapperB.find(Bar).renderProp('render');
const renderPropWrapperB = wrapperB.find(Bar).renderProp('render')();
expect(renderPropWrapperB.find(Foo)).to.have.lengthOf(1);

const stub = sinon.stub().returns(<div />);
const wrapperC = mount(<div><Bar render={stub} /></div>);
stub.resetHistory();
wrapperC.find(Bar).renderProp('render', 'one', 'two');
wrapperC.find(Bar).renderProp('render')('one', 'two');
expect(stub.args).to.deep.equal([['one', 'two']]);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4758,17 +4758,17 @@ describe('shallow', () => {
}

const wrapperA = shallow(<div><Bar render={() => <div><Foo /></div>} /></div>);
const renderPropWrapperA = wrapperA.find(Bar).renderProp('render');
const renderPropWrapperA = wrapperA.find(Bar).renderProp('render')();
expect(renderPropWrapperA.find(Foo)).to.have.lengthOf(1);

const wrapperB = shallow(<div><Bar render={() => <Foo />} /></div>);
const renderPropWrapperB = wrapperB.find(Bar).renderProp('render');
const renderPropWrapperB = wrapperB.find(Bar).renderProp('render')();
expect(renderPropWrapperB.find(Foo)).to.have.lengthOf(1);

const stub = sinon.stub().returns(<div />);
const wrapperC = shallow(<div><Bar render={stub} /></div>);
stub.resetHistory();
wrapperC.find(Bar).renderProp('render', 'one', 'two');
wrapperC.find(Bar).renderProp('render')('one', 'two');
expect(stub.args).to.deep.equal([['one', 'two']]);
});

Expand Down
12 changes: 7 additions & 5 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,9 @@ class ReactWrapper {
* Returns a wrapper of the node rendered by the provided render prop.
*
* @param {String} propName
* @returns {ReactWrapper}
* @returns {Function}
*/
renderProp(propName, ...args) {
renderProp(propName) {
const adapter = getAdapter(this[OPTIONS]);
if (typeof adapter.wrap !== 'function') {
throw new RangeError('your adapter does not support `wrap`. Try upgrading it!');
Expand All @@ -808,9 +808,11 @@ class ReactWrapper {
throw new TypeError(`expected prop “${propName}“ to contain a function, but it holds “${typeof prop}“`);
}

const element = propValue(...args);
const wrapped = adapter.wrap(element);
return this.wrap(wrapped, null, this[OPTIONS]);
return (...args) => {
const element = propValue(...args);
const wrapped = adapter.wrap(element);
return this.wrap(wrapped, null, this[OPTIONS]);
};
});
}

Expand Down
12 changes: 7 additions & 5 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,9 @@ class ShallowWrapper {
* Returns a wrapper of the node rendered by the provided render prop.
*
* @param {String} propName
* @returns {ShallowWrapper}
* @returns {Function}
*/
renderProp(propName, ...args) {
renderProp(propName) {
const adapter = getAdapter(this[OPTIONS]);
if (typeof adapter.wrap !== 'function') {
throw new RangeError('your adapter does not support `wrap`. Try upgrading it!');
Expand All @@ -1062,9 +1062,11 @@ class ShallowWrapper {
throw new TypeError(`expected prop “${propName}“ to contain a function, but it holds “${typeof prop}“`);
}

const element = propValue(...args);
const wrapped = adapter.wrap(element);
return this.wrap(wrapped, null, this[OPTIONS]);
return (...args) => {
const element = propValue(...args);
const wrapped = adapter.wrap(element);
return this.wrap(wrapped, null, this[OPTIONS]);
};
});
}

Expand Down