Skip to content

Commit

Permalink
Merge pull request #56 from lolmaus/feature/39-checked
Browse files Browse the repository at this point in the history
Add checked assertion, fixes #39
  • Loading branch information
nathanboktae committed Nov 27, 2021
2 parents 631d720 + ad72295 commit 1bffc6d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -200,6 +200,15 @@ document.querySelector('input').should.have.focus
expect(document.querySelector('.container')).not.to.have.focus
```

### `checked`

Assert that the [HTMLElement][] is an [HTMLInputElement][] with `type` of "checkbox" or "radio", and that its `checked` state is true or false.

```js
document.querySelector('input').should.be.checked
expect(document.querySelector('.checkbox')).not.to.be.checked
```

## Installation

### npm
Expand Down
15 changes: 15 additions & 0 deletions chai-dom.js
Expand Up @@ -411,4 +411,19 @@

}
})

chai.Assertion.overwriteProperty('checked', function() {
return function () {
var el = flag(this, 'object')

if(!(el instanceof HTMLInputElement && (el.type === 'checkbox' || el.type === 'radio'))) {
throw new TypeError(elToString(el) + ' is not a checkbox or radio input');
}

this.assert(
el.checked
, 'expected ' + elToString(el) + ' to be checked'
, 'expected ' + elToString(el) + ' to not be checked')
}
})
}));
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions test/tests.js
Expand Up @@ -1055,4 +1055,53 @@ describe('DOM assertions', function() {
}).should.fail("expected " + inspect(focused) + " not to have focus");
});
})

describe('checked', function() {
var span = parse('<span>Test</span>')
, input = parse('<input>')
, checkedCheckbox = parse('<input type="checkbox" checked>')
, uncheckedCheckbox = parse('<input type="checkbox">')
, checkedRadio = parse('<input type="radio" checked>')
, uncheckedRadio = parse('<input type="radio">')

it('throws when the element is not an radio or checkbox', function() {
(function() {
span.should.be.checked
}).should.throw('span is not a checkbox or radio input')

;(function() {
input.should.be.checked
}).should.throw('input is not a checkbox or radio input')
})

it('passes when checked, positive assertion', function() {
checkedCheckbox.should.be.checked
checkedRadio.should.be.checked
})

it('passes when not checked, negative assertion', function() {
uncheckedCheckbox.should.not.be.checked
uncheckedRadio.should.not.be.checked
})

it('fails when checked, negative assertion', function() {
(function() {
checkedCheckbox.should.not.be.checked
}).should.throw('expected input[type="checkbox"][checked] to not be checked')

;(function() {
checkedRadio.should.not.be.checked
}).should.throw('expected input[type="radio"][checked] to not be checked')
})

it('fails when unchecked, positive assertion', function() {
(function() {
uncheckedCheckbox.should.be.checked
}).should.throw('expected input[type="checkbox"] to be checked')

;(function() {
uncheckedRadio.should.be.checked
}).should.throw('expected input[type="radio"] to be checked')
})
})
})

0 comments on commit 1bffc6d

Please sign in to comment.