Skip to content

Commit

Permalink
Merge pull request #25 from Turbo87/has-any-value
Browse files Browse the repository at this point in the history
 Add hasAnyValue() assertion and support for calling hasValue() without arguments
  • Loading branch information
Turbo87 authored Oct 10, 2017
2 parents ab6f055 + 12bee59 commit 953496c
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 2 deletions.
24 changes: 23 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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.

Expand Down
67 changes: 67 additions & 0 deletions lib/__tests__/has-any-value.js
Original file line number Diff line number Diff line change
@@ -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 = '<input class="input username">';
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 = '<input class="input username">';

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]');
});
});
76 changes: 76 additions & 0 deletions lib/__tests__/has-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<input class="input username">';

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 = '<input class="input username">';

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');

Expand Down
38 changes: 37 additions & 1 deletion lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
*
Expand All @@ -223,6 +258,7 @@ export default class DOMAssertions {
* assert.dom('input.username').hasNoValue();
*
* @see {@link #hasValue}
* @see {@link #hasAnyValue}
*/
hasNoValue(message) {
this.hasValue('', message);
Expand Down

0 comments on commit 953496c

Please sign in to comment.