-
Notifications
You must be signed in to change notification settings - Fork 125
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
Turbo87
merged 7 commits into
mainmatter:master
from
Zureka:feature/does-not-include-text
Nov 7, 2017
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f98059
doesNotIncludeText implementation and tests
AlexZurek 51fc45e
Update API docs
AlexZurek 50983ae
Add aliases for doesNotIncludeText
AlexZurek 4591945
Change variable name to unexpected, update message phrasing
AlexZurek 3be11fe
Better expected/actual values for QUnit errors
AlexZurek e48f447
message = expected
AlexZurek e49cf11
add space after 'if'
AlexZurek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
actual = `Element ${this.targetDescription} includes text "${text}"`; | ||
} | ||
|
||
if (!message) { | ||
message = `Expected element ${this.targetDescription} to not include text "${text}"`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can |
||
} | ||
|
||
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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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