diff --git a/src/MountedTraversal.js b/src/MountedTraversal.js index 54810d3e4..c8b7504b8 100644 --- a/src/MountedTraversal.js +++ b/src/MountedTraversal.js @@ -46,9 +46,6 @@ export function instEqual(a, b, lenComp) { } export function instHasClassName(inst, className) { - if (!isDOMComponent(inst)) { - return false; - } const node = findDOMNode(inst); if (node.classList) { return node.classList.contains(className); @@ -61,6 +58,13 @@ export function instHasClassName(inst, className) { return ` ${classes} `.indexOf(` ${className} `) > -1; } +function hasClassName(inst, className) { + if (!isDOMComponent(inst)) { + return false; + } + return instHasClassName(inst, className); +} + export function instHasId(inst, id) { if (!isDOMComponent(inst)) return false; const instId = findDOMNode(inst).id || ''; @@ -198,7 +202,7 @@ export function buildInstPredicate(selector) { switch (selectorType(selector)) { case SELECTOR.CLASS_TYPE: - return inst => instHasClassName(inst, selector.substr(1)); + return inst => hasClassName(inst, selector.substr(1)); case SELECTOR.ID_TYPE: return inst => instHasId(inst, selector.substr(1)); case SELECTOR.PROP_TYPE: { diff --git a/test/ReactWrapper-spec.jsx b/test/ReactWrapper-spec.jsx index ac15318fe..39099d1ab 100644 --- a/test/ReactWrapper-spec.jsx +++ b/test/ReactWrapper-spec.jsx @@ -2073,17 +2073,37 @@ describeWithDOM('mount', () => { }); describe('.hasClass(className)', () => { - it('should return whether or not node has a certain class', () => { - const wrapper = mount( -
, - ); + context('When using a DOM component', () => { + it('should return whether or not node has a certain class', () => { + const wrapper = mount( +
, + ); + + expect(wrapper.hasClass('foo')).to.equal(true); + expect(wrapper.hasClass('bar')).to.equal(true); + expect(wrapper.hasClass('baz')).to.equal(true); + expect(wrapper.hasClass('some-long-string')).to.equal(true); + expect(wrapper.hasClass('FoOo')).to.equal(true); + expect(wrapper.hasClass('doesnt-exist')).to.equal(false); + }); + }); - expect(wrapper.hasClass('foo')).to.equal(true); - expect(wrapper.hasClass('bar')).to.equal(true); - expect(wrapper.hasClass('baz')).to.equal(true); - expect(wrapper.hasClass('some-long-string')).to.equal(true); - expect(wrapper.hasClass('FoOo')).to.equal(true); - expect(wrapper.hasClass('doesnt-exist')).to.equal(false); + context('When using a Composite component', () => { + it('should return whether or not node has a certain class', () => { + class Foo extends React.Component { + render() { + return (
); + } + } + const wrapper = mount(); + + expect(wrapper.hasClass('foo')).to.equal(true); + expect(wrapper.hasClass('bar')).to.equal(true); + expect(wrapper.hasClass('baz')).to.equal(true); + expect(wrapper.hasClass('some-long-string')).to.equal(true); + expect(wrapper.hasClass('FoOo')).to.equal(true); + expect(wrapper.hasClass('doesnt-exist')).to.equal(false); + }); }); });