Skip to content

Commit

Permalink
Merge pull request #299 from koba04/feature-tap-method
Browse files Browse the repository at this point in the history
Add tap() into ShallowWrapper and ReactWrapper
  • Loading branch information
lelandrichardson committed Apr 13, 2016
2 parents 5039580 + 0785ca6 commit 8544413
Show file tree
Hide file tree
Showing 6 changed files with 121 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;
}
}
16 changes: 16 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<ul>
<li>xxx</li>
<li>yyy</li>
<li>zzz</li>
</ul>
).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 {
Expand Down
17 changes: 17 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<ul>
<li>xxx</li>
<li>yyy</li>
<li>zzz</li>
</ul>
).find('li');
const result = wrapper.tap(spy);
expect(spy.calledWith(wrapper)).to.equal(true);
expect(result).to.equal(wrapper);
});
});

});

0 comments on commit 8544413

Please sign in to comment.