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

Added .shallow() method to ShallowWrapper #16

Merged
merged 1 commit into from
Nov 16, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [parents()](/docs/api/ShallowWrapper/parents.md)
* [parent()](/docs/api/ShallowWrapper/parent.md)
* [closest(selector)](/docs/api/ShallowWrapper/closest.md)
* [shallow()](/docs/api/ShallowWrapper/shallow.md)
* [text()](/docs/api/ShallowWrapper/text.md)
* [get(index)](/docs/api/ShallowWrapper/get.md)
* [first()](/docs/api/ShallowWrapper/first.md)
Expand Down
47 changes: 47 additions & 0 deletions docs/api/ShallowWrapper/shallow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# `.shallow() => ShallowWrapper`

Shallow renders the current node and returns a shallow wrapper around it.

NOTE: can only be called on wrapper of a single node.




#### Returns

`ShallowWrapper`: A new wrapper that wraps the current node after it's been shallow rendered.



#### Examples

```jsx
class Bar extends React.Component {
render() {
return (
<div>
<div className="in-bar" />
</div>
);
}
}
```

```jsx
class Foo extends React.Component {
render() {
return (
<div>
<Bar />
</div>
);
}
}
```

```jsx
const wrapper = shallow(<Foo />);
expect(wrapper.find('.in-bar')).to.have.length(0);
expect(wrapper.find(Bar)).to.have.length(1);
expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1);
```
3 changes: 3 additions & 0 deletions docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Get a wrapper with the direct parent of the current node.
#### [`.closest(selector) => ShallowWrapper`](ShallowWrapper/closest.md)
Get a wrapper with the first ancestor of the current node to match the provided selector.

#### [`.shallow() => ShallowWrapper`](ShallowWrapper/shallow.md)
Shallow renders the current node and returns a shallow wrapper around it.

#### [`.text() => String`](ShallowWrapper/text.md)
Returns a string representation of the text nodes in the current render tree.

Expand Down
11 changes: 11 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ export default class ShallowWrapper {
return this.parents().filter(selector).first();
}

/**
* Shallow renders the current node and returns a shallow wrapper around it.
*
* NOTE: can only be called on wrapper of a single node.
*
* @returns {ShallowWrapper}
*/
shallow() {
return this.single((n) => new ShallowWrapper(n));
}

/**
* Returns the value of prop with the given name of the root node.
*
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,35 @@ describe('shallow', () => {
});
});

describe('.shallow()', () => {

it('should return a shallow rendered instance of the current node', () => {
class Bar extends React.Component {
render() {
return (
<div>
<div className="in-bar" />
</div>
);
}
}
class Foo extends React.Component {
render() {
return (
<div>
<Bar />
</div>
);
}
}
const wrapper = shallow(<Foo />);
expect(wrapper.find('.in-bar')).to.have.length(0);
expect(wrapper.find(Bar)).to.have.length(1);
expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1);
});

});

describe('.first()', () => {
it('should return the first node in the current set', () => {
const wrapper = shallow(
Expand Down