Skip to content

Commit

Permalink
Merge pull request #1179 from FezVrasta/1174
Browse files Browse the repository at this point in the history
feat: Added hostNodes method to ReactWrapper
  • Loading branch information
ljharb committed Oct 3, 2017
2 parents 412e307 + 96039f2 commit 4fec9bb
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 3 deletions.
18 changes: 18 additions & 0 deletions docs/api/ReactWrapper/hostNodes.md
@@ -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
@@ -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);
```
3 changes: 3 additions & 0 deletions docs/api/mount.md
Expand Up @@ -71,6 +71,9 @@ Remove nodes in the current wrapper that do not match the provided selector.
#### [`.filterWhere(predicate) => ReactWrapper`](ReactWrapper/filterWhere.md)
Remove nodes in the current wrapper that do not return true for the provided predicate function.

#### [`.hostNodes() => ReactWrapper`](ReactWrapper/hostNodes.md)
Removes nodes that are not host nodes; e.g., this will only return HTML nodes.

#### [`.contains(nodeOrNodes) => Boolean`](ReactWrapper/contains.md)
Returns whether or not a given node or array of nodes is somewhere in the render tree.

Expand Down
3 changes: 3 additions & 0 deletions docs/api/shallow.md
Expand Up @@ -70,6 +70,9 @@ Remove nodes in the current wrapper that do not match the provided selector.
#### [`.filterWhere(predicate) => ShallowWrapper`](ShallowWrapper/filterWhere.md)
Remove nodes in the current wrapper that do not return true for the provided predicate function.

#### [`.hostNodes() => ShallowWrapper`](ShallowWrapper/hostNodes.md)
Removes nodes that are not host nodes; e.g., this will only return HTML nodes.

#### [`.contains(nodeOrNodes) => Boolean`](ShallowWrapper/contains.md)
Returns whether or not a given node or array of nodes is somewhere in the render tree.

Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -104,6 +104,5 @@
"sinon": "^2.4.1",
"webpack": "^1.13.3"
},
"dependencies": {
}
"dependencies": {}
}
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/package.json
Expand Up @@ -40,4 +40,4 @@
"react": "^15.5.0",
"react-dom": "^15.5.0"
}
}
}
26 changes: 26 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -356,6 +356,32 @@ describeWithDOM('mount', () => {
});
});

describe('.hostNodes()', () => {
it('should strip out any non-hostNode', () => {
class Foo extends React.Component {
render() {
return <div {...this.props} />;
}
}
const wrapper = mount(
<div>
<Foo className="foo" />
<span className="foo" />
</div>,
);

const foos = wrapper.find('.foo');
expect(foos).to.have.lengthOf(3);

const hostNodes = foos.hostNodes();
expect(hostNodes).to.have.lengthOf(2);
expect(hostNodes.filter('.foo')).to.have.lengthOf(2);

expect(hostNodes.filter('div')).to.have.lengthOf(1);
expect(hostNodes.filter('span')).to.have.lengthOf(1);
});
});

describe('.find(selector)', () => {
it('should find an element based on a class name', () => {
const wrapper = mount(
Expand Down
25 changes: 25 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -383,6 +383,31 @@ describe('shallow', () => {
});
});

describe('.hostNodes()', () => {
it('should strip out any non-hostNode', () => {
class Foo extends React.Component {
render() {
return <div {...this.props} />;
}
}
const wrapper = shallow(
<div>
<Foo className="foo" />
<div className="foo" />
</div>,
);

const foos = wrapper.find('.foo');
expect(foos).to.have.lengthOf(2);

const hostNodes = foos.hostNodes();
expect(hostNodes).to.have.lengthOf(1);

expect(hostNodes.is('div')).to.equal(true);
expect(hostNodes.hasClass('foo')).to.equal(true);
});
});

describe('.find(selector)', () => {
it('should be able to match the root DOM element', () => {
const wrapper = shallow(<div id="ttt" className="ttt">hello</div>);
Expand Down
10 changes: 10 additions & 0 deletions packages/enzyme/src/ReactWrapper.js
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.filterWhere(n => typeof n.type() === 'string');
}
}


Expand Down
10 changes: 10 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
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.filterWhere(n => typeof n.type() === 'string');
}
}

if (ITERATOR_SYMBOL) {
Expand Down

0 comments on commit 4fec9bb

Please sign in to comment.