From 0ad45ed6885649d5551062af065b1acd571db7ad Mon Sep 17 00:00:00 2001 From: Doma Date: Sat, 18 Sep 2021 19:24:21 +0800 Subject: [PATCH 1/2] Add regex support for class assertion --- chai-dom.js | 10 ++++++++++ test/tests.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/chai-dom.js b/chai-dom.js index d24c88a..5659496 100644 --- a/chai-dom.js +++ b/chai-dom.js @@ -79,6 +79,16 @@ chai.Assertion.addMethod('class', function(className) { var el = flag(this, 'object') + + if (className instanceof RegExp) { + return this.assert( + Array.from(el.classList).some(function(cls) { return className.test(cls) }) + , 'expected ' + elToString(el) + ' to have class matching #{exp}' + , 'expected ' + elToString(el) + ' not to have class matching #{exp}' + , className + ) + } + this.assert( el.classList.contains(className) , 'expected ' + elToString(el) + ' to have class #{exp}' diff --git a/test/tests.js b/test/tests.js index 09b8c99..5a6cd74 100644 --- a/test/tests.js +++ b/test/tests.js @@ -132,21 +132,41 @@ describe('DOM assertions', function() { subject.should.have.class('baz'); }) + it('passes when the element has the class matching given regex', function () { + subject.should.have.class(/foo/) + }) + it('passes negated when the element does not have the class', function() { subject.should.not.have.class('bar') }) + it('passes negated when the element does not have the class matching given regex', function () { + subject.should.not.have.class(/bar/) + }) + it('fails when the element does not have the class', function() { (function() { subject.should.have.class('bar') }).should.fail('expected div.foo.shazam.baz to have class \'bar\'') }) + it('fails when the element does not have the class matching given regex', function() { + (function() { + subject.should.have.class(/bar/) + }).should.fail('expected div.foo.shazam.baz to have class matching /bar/') + }) + it('fails negated when the element has the class', function() { (function() { subject.should.not.have.class('foo') }).should.fail('expected div.foo.shazam.baz not to have class \'foo\'') }) + + it('fails negated when the element has the class matching given regex', function() { + (function() { + subject.should.not.have.class(/foo/) + }).should.fail('expected div.foo.shazam.baz not to have class matching /foo/') + }) }) describe('id', function() { From d535fba558ac1b0133ba9ab3e28cb4f4ef2b520e Mon Sep 17 00:00:00 2001 From: Doma Date: Sat, 18 Sep 2021 19:31:12 +0800 Subject: [PATCH 2/2] Update doc for class assertion --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 6f6a6c7..e604d10 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,13 @@ document.getElementsByName('bar').should.have.class('foo') expect(document.querySelector('main article')).to.have.class('foo') ``` +Also accepts regex as argument. + +```js +document.getElementsByName('bar').should.have.class(/foo/) +expect(document.querySelector('main article')).to.have.class(/foo/) +``` + ### `id(id)` Assert that the [HTMLElement][] has the given id.