diff --git a/API.md b/API.md
index edbd8a5ec..9346ed303 100644
--- a/API.md
+++ b/API.md
@@ -163,14 +163,18 @@ assert.dom('#title').hasTextContaining('Welcome');
### hasValue
+- **See: [#hasAnyValue](#hasanyvalue)**
- **See: [#hasNoValue](#hasnovalue)**
Assert that the `value` property of an [HTMLInputElement][] matches
the `expected` text or regular expression.
+If no `expected` value is provided, the assertion will fail if the
+`value` is an empty string.
+
**Parameters**
-- `expected` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))**
+- `expected` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) \| [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?)**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
**Examples**
@@ -179,9 +183,27 @@ the `expected` text or regular expression.
assert.dom('input.username').hasValue('HSimpson');
```
+### hasAnyValue
+
+- **See: [#hasValue](#hasvalue)**
+- **See: [#hasNoValue](#hasnovalue)**
+
+Assert that the `value` property of an [HTMLInputElement][] is not empty.
+
+**Parameters**
+
+- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**
+
+**Examples**
+
+```javascript
+assert.dom('input.username').hasAnyValue();
+```
+
### hasNoValue
- **See: [#hasValue](#hasvalue)**
+- **See: [#hasAnyValue](#hasanyvalue)**
Assert that the `value` property of an [HTMLInputElement][] is empty.
diff --git a/lib/__tests__/has-any-value.js b/lib/__tests__/has-any-value.js
new file mode 100644
index 000000000..771d807b1
--- /dev/null
+++ b/lib/__tests__/has-any-value.js
@@ -0,0 +1,67 @@
+/* eslint-env jest */
+
+import TestAssertions from "../helpers/test-assertions";
+
+describe('assert.dom(...).hasAnyValue()', () => {
+ let assert;
+
+ beforeEach(() => {
+ assert = new TestAssertions();
+
+ document.body.innerHTML = '';
+ document.querySelector('input.username').value = 'HSimpson';
+ });
+
+ test('succeeds for correct content', () => {
+ assert.dom('input.username').hasAnyValue('custom message');
+ assert.dom(document.querySelector('input.username')).hasAnyValue('custom message');
+
+ expect(assert.results).toEqual([{
+ actual: 'Element input.username has a value',
+ expected: 'Element input.username has a value',
+ message: 'custom message',
+ result: true,
+ }, {
+ actual: 'Element input.input.username has a value',
+ expected: 'Element input.input.username has a value',
+ message: 'custom message',
+ result: true,
+ }]);
+ });
+
+ test('fails for wrong content', () => {
+ document.body.innerHTML = '';
+
+ assert.dom('input.username').hasAnyValue('custom message');
+ assert.dom(document.querySelector('input.username')).hasAnyValue('custom message');
+
+ expect(assert.results).toEqual([{
+ actual: 'Element input.username has no value',
+ expected: 'Element input.username has a value',
+ message: 'custom message',
+ result: false,
+ }, {
+ actual: 'Element input.input.username has no value',
+ expected: 'Element input.input.username has a value',
+ message: 'custom message',
+ result: false,
+ }]);
+ });
+
+ test('fails for missing element', () => {
+ assert.dom('#missing').hasAnyValue();
+
+ expect(assert.results).toEqual([{
+ message: 'Element #missing exists',
+ result: false,
+ }]);
+ });
+
+ test('throws for unexpected parameter types', () => {
+ expect(() => assert.dom(5).hasAnyValue()).toThrow('Unexpected Parameter: 5');
+ expect(() => assert.dom(true).hasAnyValue()).toThrow('Unexpected Parameter: true');
+ expect(() => assert.dom(undefined).hasAnyValue()).toThrow('Unexpected Parameter: undefined');
+ expect(() => assert.dom({}).hasAnyValue()).toThrow('Unexpected Parameter: [object Object]');
+ expect(() => assert.dom(document).hasAnyValue()).toThrow('Unexpected Parameter: [object HTMLDocument]');
+ });
+});
diff --git a/lib/__tests__/has-value.js b/lib/__tests__/has-value.js
index ae19e8296..3eaeff67c 100644
--- a/lib/__tests__/has-value.js
+++ b/lib/__tests__/has-value.js
@@ -103,6 +103,82 @@ describe('assert.dom(...).hasValue()', () => {
});
});
+ describe('no arguments', () => {
+ test('succeeds for correct content', () => {
+ assert.dom('input.username').hasValue();
+ assert.dom(document.querySelector('input.username')).hasValue();
+
+ expect(assert.results).toEqual([{
+ actual: 'Element input.username has a value',
+ expected: 'Element input.username has a value',
+ message: 'Element input.username has a value',
+ result: true,
+ }, {
+ actual: 'Element input.input.username has a value',
+ expected: 'Element input.input.username has a value',
+ message: 'Element input.input.username has a value',
+ result: true,
+ }]);
+ });
+
+ test('fails for wrong content', () => {
+ document.body.innerHTML = '';
+
+ assert.dom('input.username').hasValue();
+ assert.dom(document.querySelector('input.username')).hasValue();
+
+ expect(assert.results).toEqual([{
+ actual: 'Element input.username has no value',
+ expected: 'Element input.username has a value',
+ message: 'Element input.username has a value',
+ result: false,
+ }, {
+ actual: 'Element input.input.username has no value',
+ expected: 'Element input.input.username has a value',
+ message: 'Element input.input.username has a value',
+ result: false,
+ }]);
+ });
+ });
+
+ describe('{ any: true }', () => {
+ test('succeeds for correct content', () => {
+ assert.dom('input.username').hasValue({ any: true }, 'custom message');
+ assert.dom(document.querySelector('input.username')).hasValue({ any: true }, 'custom message');
+
+ expect(assert.results).toEqual([{
+ actual: 'Element input.username has a value',
+ expected: 'Element input.username has a value',
+ message: 'custom message',
+ result: true,
+ }, {
+ actual: 'Element input.input.username has a value',
+ expected: 'Element input.input.username has a value',
+ message: 'custom message',
+ result: true,
+ }]);
+ });
+
+ test('fails for wrong content', () => {
+ document.body.innerHTML = '';
+
+ assert.dom('input.username').hasValue({ any: true }, 'custom message');
+ assert.dom(document.querySelector('input.username')).hasValue({ any: true }, 'custom message');
+
+ expect(assert.results).toEqual([{
+ actual: 'Element input.username has no value',
+ expected: 'Element input.username has a value',
+ message: 'custom message',
+ result: false,
+ }, {
+ actual: 'Element input.input.username has no value',
+ expected: 'Element input.input.username has a value',
+ message: 'custom message',
+ result: false,
+ }]);
+ });
+ });
+
test('fails for missing element', () => {
assert.dom('#missing').hasValue('foo');
diff --git a/lib/assertions.js b/lib/assertions.js
index f67f6dd7f..569b1fb92 100644
--- a/lib/assertions.js
+++ b/lib/assertions.js
@@ -180,18 +180,26 @@ export default class DOMAssertions {
* Assert that the `value` property of an [HTMLInputElement][] matches
* the `expected` text or regular expression.
*
- * @param {string|RegExp} expected
+ * If no `expected` value is provided, the assertion will fail if the
+ * `value` is an empty string.
+ *
+ * @param {string|RegExp|object?} expected
* @param {string?} message
*
* @example
* assert.dom('input.username').hasValue('HSimpson');
+ * @see {@link #hasAnyValue}
* @see {@link #hasNoValue}
*/
hasValue(expected, message) {
let element = this.findTargetElement();
if (!element) return;
+ if (arguments.length === 0) {
+ expected = { any: true };
+ }
+
if (expected instanceof RegExp) {
let result = expected.test(element.value);
let actual = element.value;
@@ -202,6 +210,18 @@ export default class DOMAssertions {
this.pushResult({ result, actual, expected, message });
+ } else if (expected.any === true) {
+ let result = Boolean(element.value);
+
+ let expected = `Element ${this.targetDescription} has a value`;
+ let actual = result ? expected : `Element ${this.targetDescription} has no value`;
+
+ if (!message) {
+ message = expected;
+ }
+
+ this.pushResult({ result, actual, expected, message });
+
} else {
let actual = element.value;
let result = actual === expected;
@@ -214,6 +234,21 @@ export default class DOMAssertions {
}
}
+ /**
+ * Assert that the `value` property of an [HTMLInputElement][] is not empty.
+ *
+ * @param {string?} message
+ *
+ * @example
+ * assert.dom('input.username').hasAnyValue();
+ *
+ * @see {@link #hasValue}
+ * @see {@link #hasNoValue}
+ */
+ hasAnyValue(message) {
+ this.hasValue({ any: true }, message);
+ }
+
/**
* Assert that the `value` property of an [HTMLInputElement][] is empty.
*
@@ -223,6 +258,7 @@ export default class DOMAssertions {
* assert.dom('input.username').hasNoValue();
*
* @see {@link #hasValue}
+ * @see {@link #hasAnyValue}
*/
hasNoValue(message) {
this.hasValue('', message);