Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]
shallow
/mount
: Add wrapProp() method
- Loading branch information
Showing
7 changed files
with
256 additions
and
4 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,60 @@ | ||
# `.wrapProp(propName[, options]) => ReactWrapper` | ||
|
||
Returns a new wrapper around the component provided to the original wrapper's prop `propName`.` | ||
|
||
#### Arguments | ||
|
||
1. `propName` (`String`): Name of the prop to be wrapped | ||
1. `options` (`Object` [optional]): Will be passed to the renderer constructor. | ||
Refer to the [`mount()` options](https://enzymejs.github.io/enzyme/docs/api/mount.html#arguments). | ||
|
||
This essentially does: | ||
|
||
```jsx | ||
const Node = () => wrapper.prop('node'); | ||
const node = mount(<Node />); | ||
``` | ||
|
||
#### Returns | ||
|
||
`ReactWrapper`: A new wrapper that wraps the node from the provided prop. | ||
|
||
#### Examples | ||
|
||
##### Test Setup | ||
|
||
```jsx | ||
class Inner extends React.Component { | ||
render() { | ||
return <div className="inner" />; | ||
} | ||
} | ||
|
||
class Outer extends React.Component { | ||
render() { | ||
if (!this.props.renderNode) return <div />; | ||
return this.props.node; | ||
} | ||
} | ||
|
||
class Container extends React.Component { | ||
render() { | ||
/* | ||
* Just as an example, <Outer> can render or not the provided prop. | ||
* Independent of what it does, you want to test the component given to node. | ||
*/ | ||
return <Outer renderNode={false} node={<Inner />} />; | ||
} | ||
} | ||
``` | ||
|
||
##### Testing with no arguments | ||
|
||
```jsx | ||
const wrapper = mount(<Container />) | ||
.find(Outer) | ||
.wrapProp('node'); | ||
|
||
expect(wrapper.find('div').equals(<div className="inner" />)).to.equal(true); | ||
expect(wrapper.html()).to.equal('<div class="inner"></div>'); | ||
``` |
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,59 @@ | ||
# `.wrapProp(propName[, options]) => ShallowWrapper` | ||
|
||
Returns a new wrapper around the component provided to the original wrapper's prop `propName`.` | ||
|
||
#### Arguments | ||
|
||
1. `propName` (`String`): Name of the prop to be wrapped | ||
1. `options` (`Object` [optional]): Will be passed to the renderer constructor. | ||
Refer to the [`shallow()` options](https://enzymejs.github.io/enzyme/docs/api/shallow.html#arguments). | ||
|
||
This essentially does: | ||
|
||
```jsx | ||
const Node = () => wrapper.prop('node'); | ||
const node = shallow(<Node />); | ||
``` | ||
|
||
#### Returns | ||
|
||
`ShallowWrapper`: A new wrapper that wraps the node from the provided prop. | ||
|
||
#### Examples | ||
|
||
##### Test Setup | ||
|
||
```jsx | ||
class Inner extends React.Component { | ||
render() { | ||
return <div className="inner" />; | ||
} | ||
} | ||
|
||
class Outer extends React.Component { | ||
render() { | ||
if (!this.props.renderNode) return <div />; | ||
return this.props.node; | ||
} | ||
} | ||
|
||
class Container extends React.Component { | ||
render() { | ||
/* | ||
* Just as an example, <Outer> can render or not the provided prop. | ||
* Independent of what it does, you want to test the component given to node. | ||
*/ | ||
return <Outer renderNode={false} node={<Inner />} />; | ||
} | ||
} | ||
``` | ||
|
||
##### Testing with no arguments | ||
|
||
```jsx | ||
const wrapper = shallow(<Container />) | ||
.find(Outer) | ||
.wrapProp('node'); | ||
|
||
expect(wrapper.equals(<div className="inner" />)).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
71 changes: 71 additions & 0 deletions
71
packages/enzyme-test-suite/test/shared/methods/wrapProp.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,71 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import wrap from 'mocha-wrap'; | ||
|
||
import getAdapter from 'enzyme/build/getAdapter'; | ||
|
||
export default function describeWrapProp({ | ||
Wrap, | ||
WrapRendered, | ||
WrapperName, | ||
isShallow, | ||
}) { | ||
wrap() | ||
.withConsoleThrows() | ||
.describe('.wrapProp()', () => { | ||
class Inner extends React.Component { | ||
render() { | ||
return <div className="inner" />; | ||
} | ||
} | ||
class Outer extends React.Component { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
class Container extends React.Component { | ||
render() { | ||
const node = this.props.replace || <Inner />; | ||
return <Outer node={node} />; | ||
} | ||
} | ||
|
||
it('returns a wrapper around the node provided by the given prop', () => { | ||
const wrapper = Wrap(<Container />); | ||
const wrappedPropWrapper = wrapper.find(Outer).wrapProp('node'); | ||
expect(wrappedPropWrapper.find('div').equals(<div className="inner" />)).to.equal(true); | ||
if (isShallow) expect(wrappedPropWrapper.equals(<div className="inner" />)).to.equal(true); | ||
}); | ||
|
||
it('throws on a non-string prop name', () => { | ||
const wrapper = Wrap(<Container />); | ||
expect(() => wrapper.find(Outer).wrapProp([])).to.throw( | ||
TypeError, | ||
`${WrapperName}::wrapProp(): \`propName\` must be a string`, | ||
); | ||
}); | ||
|
||
it('throws on a missing prop', () => { | ||
const wrapper = Wrap(<Container />); | ||
expect(() => wrapper.find(Outer).wrapProp('missing')).to.throw( | ||
Error, | ||
`${WrapperName}::wrapProp(): no prop called "missing" found`, | ||
); | ||
}); | ||
|
||
it('throws on an invalid element prop value', () => { | ||
const wrapper = Wrap(<Container replace={() => <div />} />); | ||
expect(() => wrapper.find(Outer).wrapProp('node')).to.throw( | ||
TypeError, | ||
`${WrapperName}::wrapProp(): prop "node" does not contain a valid element`, | ||
); | ||
}); | ||
|
||
wrap() | ||
.withOverride(() => getAdapter(), 'wrap', () => undefined) | ||
.it('throws with a react adapter that lacks a `.wrap`', () => { | ||
const wrapper = Wrap(<Container />); | ||
expect(() => wrapper.find(Outer).wrapProp('foo')).to.throw(RangeError); | ||
}); | ||
}); | ||
} |
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