From 0785ca68c0979e04435aacf1fa92f8d9ffadeb10 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 7 Apr 2016 14:48:12 +0900 Subject: [PATCH] Add tap() into ShallowWrapper and ReactWrapper --- docs/api/ReactWrapper/tap.md | 33 +++++++++++++++++++++++++++++++++ docs/api/ShallowWrapper/tap.md | 33 +++++++++++++++++++++++++++++++++ src/ReactWrapper.js | 11 +++++++++++ src/ShallowWrapper.js | 11 +++++++++++ test/ReactWrapper-spec.js | 16 ++++++++++++++++ test/ShallowWrapper-spec.js | 17 +++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 docs/api/ReactWrapper/tap.md create mode 100644 docs/api/ShallowWrapper/tap.md diff --git a/docs/api/ReactWrapper/tap.md b/docs/api/ReactWrapper/tap.md new file mode 100644 index 000000000..9485a0b81 --- /dev/null +++ b/docs/api/ReactWrapper/tap.md @@ -0,0 +1,33 @@ +# `.tap(intercepter) => Self` + +Invokes intercepter and returns itself. intercepter is called with itself. +This is helpful when debugging nodes in method chains. + +#### Arguments + +1. `intercepter` (`Self`): the current ReactWrapper instance. + + + +#### Returns + +`Self`: the current ReactWrapper instance. + + + +#### Example + + +```jsx +const result = mount( + +) +.find('li') +.tap(n => console.log(n.debug())) +.map(n => n.text()) +; +``` diff --git a/docs/api/ShallowWrapper/tap.md b/docs/api/ShallowWrapper/tap.md new file mode 100644 index 000000000..e4c518aa1 --- /dev/null +++ b/docs/api/ShallowWrapper/tap.md @@ -0,0 +1,33 @@ +# `.tap(intercepter) => Self` + +Invokes intercepter and returns itself. intercepter is called with itself. +This is helpful when debugging nodes in method chains. + +#### Arguments + +1. `intercepter` (`Self`): the current ShallowWrapper instance. + + + +#### Returns + +`Self`: the current ShallowWrapper instance. + + + +#### Example + + +```jsx +const result = shallow( + +) +.find('li') +.tap(n => console.log(n.debug())) +.map(n => n.text()) +; +``` diff --git a/src/ReactWrapper.js b/src/ReactWrapper.js index 57fd827d8..b997ce9e0 100644 --- a/src/ReactWrapper.js +++ b/src/ReactWrapper.js @@ -733,6 +733,17 @@ export default class ReactWrapper { return debugInsts(this.nodes); } + /** + * Invokes intercepter and returns itself. intercepter is called with itself. + * This is helpful when debugging nodes in method chains. + * @param fn + * @returns {ReactWrapper} + */ + tap(intercepter) { + intercepter(this); + return this; + } + /** * Detaches the react tree from the DOM. Runs `ReactDOM.unmountComponentAtNode()` under the hood. * diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index cbf326b22..cded213ab 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -721,4 +721,15 @@ export default class ShallowWrapper { debug() { return debugNodes(this.nodes); } + + /** + * Invokes intercepter and returns itself. intercepter is called with itself. + * This is helpful when debugging nodes in method chains. + * @param fn + * @returns {ShallowWrapper} + */ + tap(intercepter) { + intercepter(this); + return this; + } } diff --git a/test/ReactWrapper-spec.js b/test/ReactWrapper-spec.js index 3248d7c3e..a1e55cce2 100644 --- a/test/ReactWrapper-spec.js +++ b/test/ReactWrapper-spec.js @@ -1764,6 +1764,22 @@ describeWithDOM('mount', () => { }); }); + describe('.tap()', () => { + it('should call the passed function with current ShallowWrapper and returns itself', () => { + const spy = sinon.spy(); + const wrapper = mount( + + ).find('li'); + const result = wrapper.tap(spy); + expect(spy.calledWith(wrapper)).to.equal(true); + expect(result).to.equal(wrapper); + }); + }); + describe('attachTo option', () => { it('should attach and stuff', () => { class Foo extends React.Component { diff --git a/test/ShallowWrapper-spec.js b/test/ShallowWrapper-spec.js index d63901583..37fea029a 100644 --- a/test/ShallowWrapper-spec.js +++ b/test/ShallowWrapper-spec.js @@ -2198,4 +2198,21 @@ describe('shallow', () => { expect(rendered.length).to.equal(0); expect(rendered.html()).to.equal(null); }); + + describe('.tap()', () => { + it('should call the passed function with current ShallowWrapper and returns itself', () => { + const spy = sinon.spy(); + const wrapper = shallow( + + ).find('li'); + const result = wrapper.tap(spy); + expect(spy.calledWith(wrapper)).to.equal(true); + expect(result).to.equal(wrapper); + }); + }); + });