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 hasAnyText() assertion #64

Merged
merged 3 commits into from
Mar 16, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,40 @@ Assert an [HTMLElement][] matching the `selector` does not exists.
assert.dom('.should-not-exist').doesNotExist();
```

### isChecked

- **See: [#isNotChecked](#isnotchecked)**

Assert that the [HTMLElement][] or an [HTMLElement][] matching the
`selector` is currently checked.

**Parameters**

- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**

**Examples**

```javascript
assert.dom('input.active').isChecked();
```

### isNotChecked

- **See: [#isChecked](#ischecked)**

Assert that the [HTMLElement][] or an [HTMLElement][] matching the
`selector` is currently unchecked.

**Parameters**

- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**

**Examples**

```javascript
assert.dom('input.active').isNotChecked();
```

### isFocused

- **See: [#isNotFocused](#isnotfocused)**
Expand Down Expand Up @@ -202,6 +236,22 @@ assert.dom('#title').hasText('Welcome to QUnit');
assert.dom('.foo').hasText(/[12]\d{3}/);
```

### hasAnyText

- **See: [#hasText](#hastext)**

Assert that the `textContent` property of an [HTMLElement][] is not empty.

**Parameters**

- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**

**Examples**

```javascript
assert.dom('button.share').hasAnyText();
```

### includesText

- **See: [#hasText](#hastext)**
Expand Down
66 changes: 66 additions & 0 deletions lib/__tests__/has-any-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-env jest */

import TestAssertions from "../helpers/test-assertions";

describe('assert.dom(...).hasAnyText()', () => {
let assert;

beforeEach(() => {
assert = new TestAssertions();

document.body.innerHTML = '<button class="share"></button>';
});

test('succeeds for correct content', () => {
document.querySelector('button.share').textContent = 'Share';

assert.dom('button.share').hasAnyText('custom message');
assert.dom(document.querySelector('button.share')).hasAnyText('custom message');

expect(assert.results).toEqual([{
actual: 'Element button.share has a text',
expected: 'Element button.share has a text',
message: 'custom message',
result: true,
}, {
actual: 'Element button.share has a text',
expected: 'Element button.share has a text',
message: 'custom message',
result: true,
}]);
});

test('fails for wrong content', () => {
assert.dom('button.share').hasAnyText('custom message');
assert.dom(document.querySelector('button.share')).hasAnyText('custom message');

expect(assert.results).toEqual([{
actual: 'Element button.share has no text',
expected: 'Element button.share has a text',
message: 'custom message',
result: false,
}, {
actual: 'Element button.share has no text',
expected: 'Element button.share has a text',
message: 'custom message',
result: false,
}]);
});

test('fails for missing element', () => {
assert.dom('#missing').hasAnyText();

expect(assert.results).toEqual([{
message: 'Element #missing exists',
result: false,
}]);
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(5).hasAnyText()).toThrow('Unexpected Parameter: 5');
expect(() => assert.dom(true).hasAnyText()).toThrow('Unexpected Parameter: true');
expect(() => assert.dom(undefined).hasAnyText()).toThrow('Unexpected Parameter: undefined');
expect(() => assert.dom({}).hasAnyText()).toThrow('Unexpected Parameter: [object Object]');
expect(() => assert.dom(document).hasAnyText()).toThrow('Unexpected Parameter: [object HTMLDocument]');
});
});
25 changes: 25 additions & 0 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ export default class DOMAssertions {
message = `Element ${this.targetDescription} has text matching ${expected}`;
}

this.pushResult({ result, actual, expected, message });
} else if (expected.any === true) {
let result = Boolean(element.textContent);

let expected = `Element ${this.targetDescription} has a text`;
let actual = result ? expected : `Element ${this.targetDescription} has no text`;

if (!message) {
message = expected;
}

this.pushResult({ result, actual, expected, message });

} else if (typeof expected === 'string') {
Expand All @@ -343,6 +354,20 @@ export default class DOMAssertions {
}
}

/**
* Assert that the `textContent` property of an [HTMLElement][] is not empty.
*
* @param {string?} message
*
* @example
* assert.dom('button.share').hasAnyText();
*
* @see {@link #hasText}
*/
hasAnyText(message) {
this.hasText({ any: true }, message);
}

/**
* Assert that the text of the [HTMLElement][] or an [HTMLElement][]
* matching the `selector` contains the given `text`, using the
Expand Down