From ad72295f8b2e1864d0871231c258b9ae1bc75cb3 Mon Sep 17 00:00:00 2001 From: "Andrey Mikhaylov (lolmaus)" Date: Fri, 26 Nov 2021 14:13:07 +0300 Subject: [PATCH] Add checked assertion, fixes #39 --- README.md | 9 +++++++++ chai-dom.js | 15 +++++++++++++++ package-lock.json | 5 ++--- test/tests.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e604d10..4efc591 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/chai-dom.js b/chai-dom.js index 5659496..ce542e0 100644 --- a/chai-dom.js +++ b/chai-dom.js @@ -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') + } + }) })); diff --git a/package-lock.json b/package-lock.json index a42076d..500350c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "chai-dom", - "version": "1.8.2", + "version": "1.10.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.8.2", + "version": "1.10.0", "license": "MIT", "devDependencies": { "chai": ">= 3", @@ -552,7 +552,6 @@ "dependencies": { "anymatch": "~3.1.1", "braces": "~3.0.2", - "fsevents": "~2.3.1", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", diff --git a/test/tests.js b/test/tests.js index 5a6cd74..d922028 100644 --- a/test/tests.js +++ b/test/tests.js @@ -1055,4 +1055,53 @@ describe('DOM assertions', function() { }).should.fail("expected " + inspect(focused) + " not to have focus"); }); }) + + describe('checked', function() { + var span = parse('Test') + , input = parse('') + , checkedCheckbox = parse('') + , uncheckedCheckbox = parse('') + , checkedRadio = parse('') + , uncheckedRadio = parse('') + + 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') + }) + }) })