From a8e4f9aca3904248a6ba1fb2c4ea138646c14298 Mon Sep 17 00:00:00 2001 From: Andreas Pabst Date: Thu, 19 Jul 2018 20:17:57 +0200 Subject: [PATCH] adding tagName to check an elements tagName, i.e.
or --- README.md | 8 ++++++++ chai-dom.js | 12 ++++++++++++ test/tests.js | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/README.md b/README.md index ea60bf4..29926b8 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,14 @@ document.querySelector('dl dd').should.be.visible expect(document.querySelector('.invisible')).not.to.be.visible ``` +### `tagName(name)` +Assert that the [HTMLElement][] has the given tagName. + +```js +document.querySelector('.container').should.have.tagName('div') +expect(document.querySelector('.container')).not.to.have.tagName('span') +``` + ## Installation ### npm diff --git a/chai-dom.js b/chai-dom.js index e14de34..a9c0bcd 100644 --- a/chai-dom.js +++ b/chai-dom.js @@ -343,4 +343,16 @@ , actual ) }) + + chai.Assertion.addMethod('tagName', function(tagName) { + var el = flag(this, 'object'), + actual = el.tagName; + + this.assert( + actual.toUpperCase() === tagName.toUpperCase() + , 'expected ' + elToString(el) + ' to have tagName ' + tagName + ', but it was ' + actual + , 'expected ' + elToString(el) + ' to not have tagName ' + tagName + ', but it was ' + actual + , actual + ) + }) })); diff --git a/test/tests.js b/test/tests.js index 85bb0c1..933acb3 100644 --- a/test/tests.js +++ b/test/tests.js @@ -856,6 +856,32 @@ describe('DOM assertions', function() { }) }) + describe('tagName', function() { + var div = document.createElement('div'), + divWithContent = parse('
test
'), + span = parse('Test') + + it('passes when the tagName is equal', function() { + div.should.have.tagName('div') + divWithContent.should.have.tagName('div') + span.should.have.tagName('span') + }) + + it('passes case insensitive', function() { + div.should.have.tagName('DIV') + div.should.have.tagName('diV') + }) + + it('passes negated', function() { + div.should.not.have.tagName('somethingelse') + span.should.not.have.tagName('div') + }) + + it('should be chainable', function() { + div.should.have.tagName('div').and.exist.and.be.ok + }) + }) + describe('util.elToString', function() { it('should give a friendly name for a HTMLElement', function() { chai.util.elToString(parse('')).should.equal('span.foo[bar="baz"]')