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

Add tap() into ShallowWrapper and ReactWrapper #299

Merged
merged 1 commit into from
Apr 13, 2016
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
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);
});
});

});