Skip to content

Commit

Permalink
Add tap() into ShallowWrapper and ReactWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Apr 7, 2016
1 parent ff492a9 commit 2cc2f8e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/api/ReactWrapper/tap.md
Original file line number Diff line number Diff line change
@@ -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(
<ul>
<li>xxx</li>
<li>yyy</li>
<li>zzz</li>
</ul>
)
.find('li')
.tap(n => console.log(n.debug()))
.map(n => n.text())
;
```
33 changes: 33 additions & 0 deletions docs/api/ShallowWrapper/tap.md
Original file line number Diff line number Diff line change
@@ -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(
<ul>
<li>xxx</li>
<li>yyy</li>
<li>zzz</li>
</ul>
)
.find('li')
.tap(n => console.log(n.debug()))
.map(n => n.text())
;
```
11 changes: 11 additions & 0 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,24 @@ describeWithDOM('mount', () => {
});
});

describe('.tap()', () => {
it('should call the passed function with current ShallowWrapper and returns itself', () => {
const spy = sinon.spy();
const wrapper = mount(
<ul>
<li>xxx</li>
<li>yyy</li>
<li>zzz</li>
</ul>
);
const result = wrapper.find('li').tap(spy);
expect(spy.calledOnce).to.equal(true);
expect(spy.args[0][0].length).to.equal(3);
expect(spy.args[0][0]).to.equal(result);
expect(spy.args[0][0].at(0).type()).to.equal('li');
});
});

describe('attachTo option', () => {
it('should attach and stuff', () => {
class Foo extends React.Component {
Expand Down
19 changes: 19 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2194,4 +2194,23 @@ 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(
<ul>
<li>xxx</li>
<li>yyy</li>
<li>zzz</li>
</ul>
);
const result = wrapper.find('li').tap(spy);
expect(spy.calledOnce).to.equal(true);
expect(spy.args[0][0].length).to.equal(3);
expect(spy.args[0][0]).to.equal(result);
expect(spy.args[0][0].at(0).type()).to.equal('li');
});
});

});

0 comments on commit 2cc2f8e

Please sign in to comment.