Skip to content

Commit

Permalink
add tests shallow.hasClass()
Browse files Browse the repository at this point in the history
  • Loading branch information
alvteren committed Mar 18, 2019
1 parent c193b50 commit 27ea407
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -8739,4 +8739,42 @@ describe('shallow', () => {
expect(spy).to.have.property('callCount', 1);
});
});

describe('.hasClass()', () => {
it('works for standalone classNames', () => {
const wrapper = shallow(<div className="foo" />);
expect(wrapper.hasClass('foo')).to.equal(true);
expect(wrapper.hasClass('bar')).to.equal(false);
});

it('works for multiple classNames', () => {
const wrapper = shallow(<div className="foo bar baz" />);
expect(wrapper.hasClass('foo')).to.equal(true);
expect(wrapper.hasClass('bar')).to.equal(true);
expect(wrapper.hasClass('baz')).to.equal(true);
expect(wrapper.hasClass('bax')).to.equal(false);
});

it('also allows hyphens', () => {
const wrapper = shallow(<div className="foo-bar" />);
expect(wrapper.hasClass('foo-bar')).to.equal(true);
});

it('works if className has a function in toString property', () => {
function classes() {}
classes.toString = () => 'foo-bar';
const wrapper = shallow(<div className={classes} />);
expect(wrapper.hasClass('foo-bar')).to.equal(true);
});

it('works if searching with a RegExp', () => {
const wrapper = shallow(<div className="ComponentName-classname-123" />);
expect(wrapper.hasClass(/(ComponentName)-(classname)-(\d+)/)).to.equal(true);
});

it('fails if searching for a missing classname with a RegExp', () => {
const wrapper = shallow(<div className="ComponentName-classname-123 ComponentName-otherclassname-23" />);
expect(wrapper.hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(false);
});
});
});

0 comments on commit 27ea407

Please sign in to comment.