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

Make hasClass work for mounted composite components #677

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
12 changes: 8 additions & 4 deletions src/MountedTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 || '';
Expand Down Expand Up @@ -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: {
Expand Down
40 changes: 30 additions & 10 deletions test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2073,17 +2073,37 @@ describeWithDOM('mount', () => {
});

describe('.hasClass(className)', () => {
it('should return whether or not node has a certain class', () => {
const wrapper = mount(
<div className="foo bar baz some-long-string FoOo" />,
);
context('When using a DOM component', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb are you OK with using context blocks here? I don't think we use them anywhere else, but it makes enough sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context and describe are identical - either is fine, but i have a light preference for describe.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I know they're identical, it was just a question of preference.

it('should return whether or not node has a certain class', () => {
const wrapper = mount(
<div className="foo bar baz some-long-string FoOo" />,
);

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 (<div className="foo bar baz some-long-string FoOo" />);
}
}
const wrapper = mount(<Foo />);

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);
});
});
});

Expand Down