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 doesNotIncludeText() assertion #36

Merged
merged 7 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 20 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ attribute.
assert.dom('#title').includesText('Welcome');
```

### doesNotIncludeText

Assert that the text of the [HTMLElement][] or an [HTMLElement][]
matching the `selector` does not include the given `text`, using the
[`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
attribute.

**Aliases:** `doesNotContainText`, `doesNotHaveTextContaining`

**Parameters**

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

**Examples**

```javascript
assert.dom('#title').doesNotIncludeText('Welcome');
```

### hasValue

- **See: [#hasAnyValue](#hasanyvalue)**
Expand Down
109 changes: 109 additions & 0 deletions lib/__tests__/does-not-include-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* eslint-env jest */

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

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

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

test('with custom message', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

assert.dom('h1').doesNotIncludeText('baz', 'bing');

expect(assert.results).toEqual([{
actual: "Element h1 does not include text \"baz\"",
expected: "Element h1 does not include text \"baz\"",
message: 'bing',
result: true,
}]);
});

describe('with HTMLElement', () => {
let element;

beforeEach(() => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';
element = document.querySelector('h1');
});

test('succeeds for correct content', () => {
assert.dom(element).doesNotIncludeText('baz');

expect(assert.results).toEqual([{
actual: "Element h1.baz does not include text \"baz\"",
expected: "Element h1.baz does not include text \"baz\"",
message: 'Expected element h1.baz to not include text "baz"',
result: true,
}]);
});

test('fails for wrong content', () => {
assert.dom(element).doesNotIncludeText('foo');

expect(assert.results).toEqual([{
actual: 'Element h1.baz includes text "foo"',
expected: 'Element h1.baz does not include text "foo"',
message: 'Expected element h1.baz to not include text "foo"',
result: false,
}]);
});

test('fails for missing element', () => {
assert.dom(null).doesNotIncludeText('foo');

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

describe('with selector', () => {
beforeEach(() => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';
});

test('succeeds for correct content', () => {
assert.dom('h1').doesNotIncludeText('bar');

expect(assert.results).toEqual([{
actual: 'Element h1 does not include text "bar"',
expected: 'Element h1 does not include text "bar"',
message: 'Expected element h1 to not include text "bar"',
result: true,
}]);
});

test('fails for wrong content', () => {
assert.dom('h1').doesNotIncludeText('foo');

expect(assert.results).toEqual([{
actual: 'Element h1 includes text "foo"',
expected: 'Element h1 does not include text "foo"',
message: 'Expected element h1 to not include text "foo"',
result: false,
}]);
});

test('fails for missing element', () => {
assert.dom('h2').doesNotIncludeText('foo');

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

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(5).doesNotIncludeText('foo')).toThrow('Unexpected Parameter: 5');
expect(() => assert.dom(true).doesNotIncludeText('foo')).toThrow('Unexpected Parameter: true');
expect(() => assert.dom(undefined).doesNotIncludeText('foo')).toThrow('Unexpected Parameter: undefined');
expect(() => assert.dom({}).doesNotIncludeText('foo')).toThrow('Unexpected Parameter: [object Object]');
expect(() => assert.dom(document).doesNotIncludeText('foo')).toThrow('Unexpected Parameter: [object HTMLDocument]');
});
});
41 changes: 41 additions & 0 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,47 @@ export default class DOMAssertions {
this.includesText(expected, message);
}

/**
* Assert that the text of the [HTMLElement][] or an [HTMLElement][]
* matching the `selector` does not include the given `text`, using the
* [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
* attribute.
*
* **Aliases:** `doesNotContainText`, `doesNotHaveTextContaining`
*
* @param {string} text
* @param {string?} message
*
* @example
* assert.dom('#title').doesNotIncludeText('Welcome');
*/
doesNotIncludeText(text, message) {
let element = this.findTargetElement();
if (!element) return;

let result = element.textContent.indexOf(text) === -1;
let expected = `Element ${this.targetDescription} does not include text "${text}"`;
let actual = expected;

if(!result) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a space missing here after the if

actual = `Element ${this.targetDescription} includes text "${text}"`;
}

if (!message) {
message = `Expected element ${this.targetDescription} to not include text "${text}"`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can message = expected here like in a few of the other assertions

}

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

doesNotContainText(unexpected, message) {
this.doesNotIncludeText(unexpected, message);
}

doesNotHaveTextContaining(unexpected, message) {
this.doesNotIncludeText(unexpected, message);
}

/**
* Assert that the `value` property of an [HTMLInputElement][] matches
* the `expected` text or regular expression.
Expand Down