From 0f98059074e22c6f19e77490473a5706dd59a5fd Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Fri, 3 Nov 2017 16:24:29 -0400 Subject: [PATCH 1/7] doesNotIncludeText implementation and tests --- lib/__tests__/does-not-include-text.js | 109 +++++++++++++++++++++++++ lib/assertions.js | 27 ++++++ 2 files changed, 136 insertions(+) create mode 100644 lib/__tests__/does-not-include-text.js diff --git a/lib/__tests__/does-not-include-text.js b/lib/__tests__/does-not-include-text.js new file mode 100644 index 000000000..740130b0b --- /dev/null +++ b/lib/__tests__/does-not-include-text.js @@ -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 = '

foo

bar'; + + assert.dom('h1').doesNotIncludeText('baz', 'bing'); + + expect(assert.results).toEqual([{ + actual: 'foo', + expected: 'baz', + message: 'bing', + result: true, + }]); + }); + + describe('with HTMLElement', () => { + let element; + + beforeEach(() => { + document.body.innerHTML = '

foo

bar'; + element = document.querySelector('h1'); + }); + + test('succeeds for correct content', () => { + assert.dom(element).doesNotIncludeText('baz'); + + expect(assert.results).toEqual([{ + actual: 'foo', + expected: 'baz', + message: 'Element h1.baz does not have text containing "baz"', + result: true, + }]); + }); + + test('fails for wrong content', () => { + assert.dom(element).doesNotIncludeText('foo'); + + expect(assert.results).toEqual([{ + actual: 'foo', + expected: 'foo', + message: 'Element h1.baz does not have text containing "foo"', + result: false, + }]); + }); + + test('fails for missing element', () => { + assert.dom(null).doesNotIncludeText('foo'); + + expect(assert.results).toEqual([{ + message: 'Element exists', + result: false, + }]); + }); + }); + + describe('with selector', () => { + beforeEach(() => { + document.body.innerHTML = '

foo

bar'; + }); + + test('succeeds for correct content', () => { + assert.dom('h1').doesNotIncludeText('bar'); + + expect(assert.results).toEqual([{ + actual: 'foo', + expected: 'bar', + message: 'Element h1 does not have text containing "bar"', + result: true, + }]); + }); + + test('fails for wrong content', () => { + assert.dom('h1').doesNotIncludeText('foo'); + + expect(assert.results).toEqual([{ + actual: 'foo', + expected: 'foo', + message: 'Element h1 does not have text containing "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]'); + }); +}); diff --git a/lib/assertions.js b/lib/assertions.js index 6f252dd7b..a4d31b399 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -327,6 +327,33 @@ export default class DOMAssertions { this.includesText(expected, message); } + /** + * Assert that the text of the [HTMLElement][] or an [HTMLElement][] + * matching the `selector` does not contain the given `text`, using the + * [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) + * attribute. + * + * @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 actual = element.textContent; + let expected = text; + + if (!message) { + message = `Element ${this.targetDescription} does not have text containing "${text}"`; + } + + this.pushResult({ result, actual, expected, message }); + } + /** * Assert that the `value` property of an [HTMLInputElement][] matches * the `expected` text or regular expression. From 51fc45ed508fd13f1a8b3cd6ae7c9c79a431f44b Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Fri, 3 Nov 2017 16:24:47 -0400 Subject: [PATCH 2/7] Update API docs --- API.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/API.md b/API.md index 80511dc03..aebf0184b 100644 --- a/API.md +++ b/API.md @@ -224,6 +224,24 @@ attribute. assert.dom('#title').includesText('Welcome'); ``` +### doesNotIncludeText + +Assert that the text of the [HTMLElement][] or an [HTMLElement][] +matching the `selector` does not contain the given `text`, using the +[`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) +attribute. + +**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)** From 50983aed06b9903b7c0c1043a81bb4142f1ef82b Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Fri, 3 Nov 2017 16:50:36 -0400 Subject: [PATCH 3/7] Add aliases for doesNotIncludeText --- API.md | 2 ++ lib/assertions.js | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/API.md b/API.md index aebf0184b..afb6f5b56 100644 --- a/API.md +++ b/API.md @@ -231,6 +231,8 @@ matching the `selector` does not contain 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)** diff --git a/lib/assertions.js b/lib/assertions.js index a4d31b399..d6f97c180 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -333,6 +333,8 @@ export default class DOMAssertions { * [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) * attribute. * + * **Aliases:** `doesNotContainText`, `doesNotHaveTextContaining` + * * @param {string} text * @param {string?} message * @@ -354,6 +356,14 @@ export default class DOMAssertions { this.pushResult({ result, actual, expected, message }); } + doesNotContainText(expected, message) { + this.doesNotIncludeText(expected, message); + } + + doesNotHaveTextContaining(expected, message) { + this.doesNotIncludeText(expected, message); + } + /** * Assert that the `value` property of an [HTMLInputElement][] matches * the `expected` text or regular expression. From 4591945a095e7757d4b81758217baeda9decba43 Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Mon, 6 Nov 2017 08:44:26 -0500 Subject: [PATCH 4/7] Change variable name to unexpected, update message phrasing --- API.md | 2 +- lib/__tests__/does-not-include-text.js | 8 ++++---- lib/assertions.js | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/API.md b/API.md index afb6f5b56..bec54c32a 100644 --- a/API.md +++ b/API.md @@ -227,7 +227,7 @@ assert.dom('#title').includesText('Welcome'); ### doesNotIncludeText Assert that the text of the [HTMLElement][] or an [HTMLElement][] -matching the `selector` does not contain the given `text`, using the +matching the `selector` does not include the given `text`, using the [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) attribute. diff --git a/lib/__tests__/does-not-include-text.js b/lib/__tests__/does-not-include-text.js index 740130b0b..dd6765715 100644 --- a/lib/__tests__/does-not-include-text.js +++ b/lib/__tests__/does-not-include-text.js @@ -36,7 +36,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { expect(assert.results).toEqual([{ actual: 'foo', expected: 'baz', - message: 'Element h1.baz does not have text containing "baz"', + message: 'Element h1.baz does not include text "baz"', result: true, }]); }); @@ -47,7 +47,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { expect(assert.results).toEqual([{ actual: 'foo', expected: 'foo', - message: 'Element h1.baz does not have text containing "foo"', + message: 'Element h1.baz does not include text "foo"', result: false, }]); }); @@ -73,7 +73,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { expect(assert.results).toEqual([{ actual: 'foo', expected: 'bar', - message: 'Element h1 does not have text containing "bar"', + message: 'Element h1 does not include text "bar"', result: true, }]); }); @@ -84,7 +84,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { expect(assert.results).toEqual([{ actual: 'foo', expected: 'foo', - message: 'Element h1 does not have text containing "foo"', + message: 'Element h1 does not include text "foo"', result: false, }]); }); diff --git a/lib/assertions.js b/lib/assertions.js index d6f97c180..9c6d7fa92 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -329,7 +329,7 @@ export default class DOMAssertions { /** * Assert that the text of the [HTMLElement][] or an [HTMLElement][] - * matching the `selector` does not contain the given `text`, using the + * matching the `selector` does not include the given `text`, using the * [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) * attribute. * @@ -347,21 +347,21 @@ export default class DOMAssertions { let result = element.textContent.indexOf(text) === -1; let actual = element.textContent; - let expected = text; + let unexpected = text; if (!message) { - message = `Element ${this.targetDescription} does not have text containing "${text}"`; + message = `Element ${this.targetDescription} does not include text "${text}"`; } - this.pushResult({ result, actual, expected, message }); + this.pushResult({ result, actual, expected: unexpected, message }); } - doesNotContainText(expected, message) { - this.doesNotIncludeText(expected, message); + doesNotContainText(unexpected, message) { + this.doesNotIncludeText(unexpected, message); } - doesNotHaveTextContaining(expected, message) { - this.doesNotIncludeText(expected, message); + doesNotHaveTextContaining(unexpected, message) { + this.doesNotIncludeText(unexpected, message); } /** From 3be11fe3ea03b8b07ae6d36a868627f26be29af8 Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Tue, 7 Nov 2017 08:55:41 -0500 Subject: [PATCH 5/7] Better expected/actual values for QUnit errors --- lib/__tests__/does-not-include-text.js | 28 +++++++++++++------------- lib/assertions.js | 12 +++++++---- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/__tests__/does-not-include-text.js b/lib/__tests__/does-not-include-text.js index dd6765715..594a5a276 100644 --- a/lib/__tests__/does-not-include-text.js +++ b/lib/__tests__/does-not-include-text.js @@ -15,8 +15,8 @@ describe('assert.dom(...).doesNotIncludeText()', () => { assert.dom('h1').doesNotIncludeText('baz', 'bing'); expect(assert.results).toEqual([{ - actual: 'foo', - expected: 'baz', + actual: "Element h1 does not include text \"baz\"", + expected: "Element h1 does not include text \"baz\"", message: 'bing', result: true, }]); @@ -34,9 +34,9 @@ describe('assert.dom(...).doesNotIncludeText()', () => { assert.dom(element).doesNotIncludeText('baz'); expect(assert.results).toEqual([{ - actual: 'foo', - expected: 'baz', - message: 'Element h1.baz does not include text "baz"', + 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, }]); }); @@ -45,9 +45,9 @@ describe('assert.dom(...).doesNotIncludeText()', () => { assert.dom(element).doesNotIncludeText('foo'); expect(assert.results).toEqual([{ - actual: 'foo', - expected: 'foo', - message: 'Element h1.baz does not include text "foo"', + 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, }]); }); @@ -71,9 +71,9 @@ describe('assert.dom(...).doesNotIncludeText()', () => { assert.dom('h1').doesNotIncludeText('bar'); expect(assert.results).toEqual([{ - actual: 'foo', - expected: 'bar', - message: 'Element h1 does not include text "bar"', + 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, }]); }); @@ -82,9 +82,9 @@ describe('assert.dom(...).doesNotIncludeText()', () => { assert.dom('h1').doesNotIncludeText('foo'); expect(assert.results).toEqual([{ - actual: 'foo', - expected: 'foo', - message: 'Element h1 does not include text "foo"', + 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, }]); }); diff --git a/lib/assertions.js b/lib/assertions.js index 9c6d7fa92..9618ebf1f 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -346,14 +346,18 @@ export default class DOMAssertions { if (!element) return; let result = element.textContent.indexOf(text) === -1; - let actual = element.textContent; - let unexpected = text; + let expected = `Element ${this.targetDescription} does not include text "${text}"`; + let actual = expected; + + if(!result) { + actual = `Element ${this.targetDescription} includes text "${text}"`; + } if (!message) { - message = `Element ${this.targetDescription} does not include text "${text}"`; + message = `Expected element ${this.targetDescription} to not include text "${text}"`; } - this.pushResult({ result, actual, expected: unexpected, message }); + this.pushResult({ result, actual, expected, message }); } doesNotContainText(unexpected, message) { From e48f4473e6596bc7c19f29ed7643db52b0c73285 Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Tue, 7 Nov 2017 10:49:14 -0500 Subject: [PATCH 6/7] message = expected --- lib/__tests__/does-not-include-text.js | 8 ++++---- lib/assertions.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/__tests__/does-not-include-text.js b/lib/__tests__/does-not-include-text.js index 594a5a276..0b10ee9dc 100644 --- a/lib/__tests__/does-not-include-text.js +++ b/lib/__tests__/does-not-include-text.js @@ -36,7 +36,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { 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"', + message: "Element h1.baz does not include text \"baz\"", result: true, }]); }); @@ -47,7 +47,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { 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"', + message: 'Element h1.baz does not include text "foo"', result: false, }]); }); @@ -73,7 +73,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { 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"', + message: 'Element h1 does not include text "bar"', result: true, }]); }); @@ -84,7 +84,7 @@ describe('assert.dom(...).doesNotIncludeText()', () => { 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"', + message: 'Element h1 does not include text "foo"', result: false, }]); }); diff --git a/lib/assertions.js b/lib/assertions.js index 9618ebf1f..e2b14c959 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -354,7 +354,7 @@ export default class DOMAssertions { } if (!message) { - message = `Expected element ${this.targetDescription} to not include text "${text}"`; + message = expected; } this.pushResult({ result, actual, expected, message }); From e49cf110185ef28adb399a9d00415a80a20c635a Mon Sep 17 00:00:00 2001 From: Alex Zurek Date: Tue, 7 Nov 2017 10:50:21 -0500 Subject: [PATCH 7/7] add space after 'if' --- lib/assertions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/assertions.js b/lib/assertions.js index e2b14c959..d583fee0d 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -349,7 +349,7 @@ export default class DOMAssertions { let expected = `Element ${this.targetDescription} does not include text "${text}"`; let actual = expected; - if(!result) { + if (!result) { actual = `Element ${this.targetDescription} includes text "${text}"`; }