-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1863 from dferber90/master
[New] `mount`/`shallow`: Add renderProp - [enzyme-adapter-react-*] [New] add `.wrap` method
- Loading branch information
Showing
20 changed files
with
457 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# `.renderProp(propName, ...args) => ReactWrapper` | ||
|
||
Calls the current wrapper's property with name `propName` and the `args` provided. | ||
Returns the result in a new wrapper. | ||
|
||
NOTE: can only be called on wrapper of a single non-DOM component element node. | ||
|
||
#### Arguments | ||
|
||
1. `propName` (`String`): | ||
1. `...args` (`Array<Any>`): | ||
|
||
This essentially calls `wrapper.prop(propName)(...args)`. | ||
|
||
#### Returns | ||
|
||
`ReactWrapper`: A new wrapper that wraps the node returned from the render prop. | ||
|
||
#### Examples | ||
|
||
##### Test Setup | ||
|
||
```jsx | ||
class Mouse extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { x: 0, y: 0 }; | ||
} | ||
|
||
render() { | ||
const { render } = this.props; | ||
return ( | ||
<div | ||
style={{ height: '100%' }} | ||
onMouseMove={(event) => { | ||
this.setState({ | ||
x: event.clientX, | ||
y: event.clientY, | ||
}); | ||
}} | ||
> | ||
{render(this.state)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Mouse.propTypes = { | ||
render: PropTypes.func.isRequired, | ||
}; | ||
``` | ||
|
||
```jsx | ||
const App = () => ( | ||
<div style={{ height: '100%' }}> | ||
<Mouse | ||
render={(x = 0, y = 0) => ( | ||
<h1> | ||
The mouse position is ({x}, {y}) | ||
</h1> | ||
)} | ||
/> | ||
</div> | ||
); | ||
``` | ||
|
||
##### Testing with no arguments | ||
|
||
```jsx | ||
const wrapper = mount(<App />) | ||
.find(Mouse) | ||
.renderProp('render'); | ||
|
||
expect(wrapper.equals(<h1>The mouse position is 0, 0</h1>)).to.equal(true); | ||
``` | ||
|
||
##### Testing with multiple arguments | ||
|
||
```jsx | ||
const wrapper = mount(<App />) | ||
.find(Mouse) | ||
.renderProp('render', [10, 20]); | ||
|
||
expect(wrapper.equals(<h1>The mouse position is 10, 20</h1>)).to.equal(true); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# `.renderProp(propName, ...args) => ShallowWrapper` | ||
|
||
Calls the current wrapper's property with name `propName` and the `args` provided. | ||
Returns the result in a new wrapper. | ||
|
||
NOTE: can only be called on wrapper of a single non-DOM component element node. | ||
|
||
#### Arguments | ||
|
||
1. `propName` (`String`): | ||
1. `...args` (`Array<Any>`): | ||
|
||
This essentially calls `wrapper.prop(propName)(...args)`. | ||
|
||
#### Returns | ||
|
||
`ShallowWrapper`: A new wrapper that wraps the node returned from the render prop. | ||
|
||
#### Examples | ||
|
||
##### Test Setup | ||
|
||
```jsx | ||
class Mouse extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { x: 0, y: 0 }; | ||
} | ||
|
||
render() { | ||
const { render } = this.props; | ||
return ( | ||
<div | ||
style={{ height: '100%' }} | ||
onMouseMove={(event) => { | ||
this.setState({ | ||
x: event.clientX, | ||
y: event.clientY, | ||
}); | ||
}} | ||
> | ||
{render(this.state)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Mouse.propTypes = { | ||
render: PropTypes.func.isRequired, | ||
}; | ||
``` | ||
|
||
```jsx | ||
const App = () => ( | ||
<div style={{ height: '100%' }}> | ||
<Mouse | ||
render={(x = 0, y = 0) => ( | ||
<h1> | ||
The mouse position is ({x}, {y}) | ||
</h1> | ||
)} | ||
/> | ||
</div> | ||
); | ||
``` | ||
|
||
##### Testing with no arguments | ||
|
||
```jsx | ||
const wrapper = shallow(<App />) | ||
.find(Mouse) | ||
.renderProp('render'); | ||
|
||
expect(wrapper.equals(<h1>The mouse position is 0, 0</h1>)).to.equal(true); | ||
``` | ||
|
||
##### Testing with multiple arguments | ||
|
||
```jsx | ||
const wrapper = shallow(<App />) | ||
.find(Mouse) | ||
.renderProp('render', [10, 20]); | ||
|
||
expect(wrapper.equals(<h1>The mouse position is 10, 20</h1>)).to.equal(true); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/enzyme-adapter-utils/src/wrapWithSimpleWrapper.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import { intersects } from 'semver'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const propTypes = { | ||
children: PropTypes.element.isRequired, | ||
}; | ||
|
||
const Wrapper = (intersects('>= 0.14', React.version) | ||
// eslint-disable-next-line prefer-arrow-callback | ||
? () => Object.assign(function SimpleSFCWrapper({ children }) { | ||
return children; | ||
}, { propTypes }) | ||
: () => { | ||
class SimpleClassWrapper extends React.Component { | ||
render() { | ||
const { children } = this.props; | ||
return children; | ||
} | ||
} | ||
SimpleClassWrapper.propTypes = propTypes; | ||
return SimpleClassWrapper; | ||
} | ||
)(); | ||
|
||
export default function wrap(element) { | ||
console.log(React.version, Wrapper); | ||
return <Wrapper>{element}</Wrapper>; | ||
} |
Oops, something went wrong.