diff --git a/docs/api/ReactWrapper/hostNodes.md b/docs/api/ReactWrapper/hostNodes.md new file mode 100644 index 000000000..508da5a12 --- /dev/null +++ b/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(
); +expect(wrapper.find('.foo').hostNodes()).to.have.length(1); +``` diff --git a/docs/api/ShallowWrapper/hostNodes.md b/docs/api/ShallowWrapper/hostNodes.md new file mode 100644 index 000000000..c34204f2e --- /dev/null +++ b/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(
); +expect(wrapper.find('.foo').hostNodes()).to.have.length(1); +``` diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 25f2a326c..20afd04ad 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -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 =>
; + const wrapper = mount( +
+ +
, + ); + 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(
diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index a724d8b6b..6900ea16c 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -399,6 +399,20 @@ describe('shallow', () => { expect(wrapper.find('.foo').type()).to.equal('input'); }); + it('should strip out any non-hostNode', () => { + const Foo = props =>
; + const wrapper = mount( +
+ +
+
, + ); + 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(
diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index b967b3cb8..8511d2824 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -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'); + } } diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index f1c207109..90169dc1d 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -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) {