Skip to content

Commit

Permalink
Merge pull request #36 from andipabst/master
Browse files Browse the repository at this point in the history
adding tagName to check an elements tagName, i.e. <div> or <span>
  • Loading branch information
nathanboktae committed Dec 19, 2019
2 parents 24e1d9a + a8e4f9a commit 75c0e73
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions chai-dom.js
Expand Up @@ -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
)
})
}));
26 changes: 26 additions & 0 deletions test/tests.js
Expand Up @@ -856,6 +856,32 @@ describe('DOM assertions', function() {
})
})

describe('tagName', function() {
var div = document.createElement('div'),
divWithContent = parse('<div><span>test</span></div>'),
span = parse('<span>Test</span>')

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('<span class="foo" bar="baz"></span>')).should.equal('span.foo[bar="baz"]')
Expand Down

0 comments on commit 75c0e73

Please sign in to comment.