Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checked assertion, fixes #39 #56

Merged
merged 1 commit into from Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})
})
})