From 7f93d39fc77538b0acebcda04c87891282e662c2 Mon Sep 17 00:00:00 2001 From: Leland Richardson Date: Sun, 15 Nov 2015 09:34:10 -0800 Subject: [PATCH] Added .shallow() method to ShallowWrapper --- docs/README.md | 1 + docs/api/ShallowWrapper/shallow.md | 47 ++++++++++++++++++++++++++++ docs/api/shallow.md | 3 ++ src/ShallowWrapper.js | 11 +++++++ src/__tests__/ShallowWrapper-spec.js | 29 +++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 docs/api/ShallowWrapper/shallow.md diff --git a/docs/README.md b/docs/README.md index 2fa417c8e..c3e282d43 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) diff --git a/docs/api/ShallowWrapper/shallow.md b/docs/api/ShallowWrapper/shallow.md new file mode 100644 index 000000000..4b6c94b58 --- /dev/null +++ b/docs/api/ShallowWrapper/shallow.md @@ -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 ( +
+
+
+ ); + } +} +``` + +```jsx +class Foo extends React.Component { + render() { + return ( +
+ +
+ ); + } +} +``` + +```jsx +const wrapper = shallow(); +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); +``` diff --git a/docs/api/shallow.md b/docs/api/shallow.md index f720b2fb8..c02d81f7f 100644 --- a/docs/api/shallow.md +++ b/docs/api/shallow.md @@ -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. diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 445aa8a08..b6b11857d 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -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. * diff --git a/src/__tests__/ShallowWrapper-spec.js b/src/__tests__/ShallowWrapper-spec.js index 07c88b407..4acfeae4c 100644 --- a/src/__tests__/ShallowWrapper-spec.js +++ b/src/__tests__/ShallowWrapper-spec.js @@ -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 ( +
+
+
+ ); + } + } + class Foo extends React.Component { + render() { + return ( +
+ +
+ ); + } + } + const wrapper = shallow(); + 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(