Skip to content

Commit

Permalink
feat: Added hostNodes method
Browse files Browse the repository at this point in the history
fixes #1174
  • Loading branch information
FezVrasta authored and Federico Zivolo committed Sep 29, 2017
1 parent f1a192a commit 9fc16df
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/api/ReactWrapper/hostNodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `.hostNodes() => ReactWrapper`

Returns a new wrapper with only host nodes.



#### Returns

`ReactWrapper`: A new wrapper that wraps the filtered nodes.



#### Examples

```jsx
const wrapper = mount(<div><MyComponent className="foo" /><div className="foo" /></div>);
expect(wrapper.find('.foo').hostNodes()).to.have.length(1);
```
18 changes: 18 additions & 0 deletions docs/api/ShallowWrapper/hostNodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `.hostNodes() => ShallowWrapper`

Returns a new wrapper with only host nodes.



#### Returns

`ShallowWrapper`: A new wrapper that wraps the filtered nodes.



#### Examples

```jsx
const wrapper = mount(<div><MyComponent className="foo" /><div className="foo" /></div>);
expect(wrapper.find('.foo').hostNodes()).to.have.length(1);
```
13 changes: 13 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,19 @@ describeWithDOM('mount', () => {
expect(wrapper.find('input.foo').length).to.equal(1);
});

it('should strip out any non-hostNode', () => {
const Foo = props => <div {...props} />;
const wrapper = mount(
<div>
<Foo className="foo" />
</div>,
);
const hostNodes = wrapper.find('.foo').hostNodes();
expect(wrapper.find('.foo')).to.have.length(2);
expect(hostNodes).to.have.length(1);
expect(hostNodes.is('div')).to.equal(true);
});

it('should work on non-single nodes', () => {
const wrapper = mount(
<div className="a">
Expand Down
14 changes: 14 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,20 @@ describe('shallow', () => {
expect(wrapper.find('.foo').type()).to.equal('input');
});

it('should strip out any non-hostNode', () => {
const Foo = props => <div {...props} />;
const wrapper = mount(
<div>
<Foo className="foo" />
<div className="foo" />
</div>,
);
const hostNodes = wrapper.find('.foo').hostNodes();
expect(wrapper.find('.foo')).to.have.length(2);
expect(hostNodes).to.have.length(1);
expect(hostNodes.is('div')).to.equal(true);
});

it('should find an element that has dot in attribute', () => {
const wrapper = shallow(
<div>
Expand Down
10 changes: 10 additions & 0 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,16 @@ class ReactWrapper {
}
this[RENDERER].unmount();
}

/**
* Strips out all the not host-nodes from the list of nodes
*
* This method is useful if you want to check for the presence of host nodes
* (actually rendered HTML elements) ignoring the React nodes.
*/
hostNodes() {
return this.filter(n => typeof n.type() === 'string');
}
}


Expand Down
10 changes: 10 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,16 @@ class ShallowWrapper {
return this.wrap(el, null, { ...this[OPTIONS], ...options });
});
}

/**
* Strips out all the not host-nodes from the list of nodes
*
* This method is useful if you want to check for the presence of host nodes
* (actually rendered HTML elements) ignoring the React nodes.
*/
hostNodes() {
return this.filter(n => typeof n.type() === 'string');
}
}

if (ITERATOR_SYMBOL) {
Expand Down

0 comments on commit 9fc16df

Please sign in to comment.