Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Match if the surrounding whitespace in the input matches the element text #163

Merged
merged 1 commit into from

2 participants

@f1sherman

Currently the text in the element is trimmed before comparing. This is helpful
in some cases, but we're unable to match intentional surrounding whitespace.

For instance, given an element like this:

<div> foo</div>

The following matcher will always fail:

expect($div).toHaveText(' foo');

This fixes that case so that the matcher passes if the surrounding whitespace
in the input matches the surrounding whitespace in the element text exactly.

@f1sherman f1sherman Match if the surrounding whitespace in the input matches the element …
…text

Currently the text in the element is trimmed before comparing. This is helpful
in some cases, but we're unable to match intentional surrounding whitespace.

For instance, given an element like this:
<div> foo</div>
The following matcher will always fail:
expect($div).toHaveText(' foo');

This fixes that case so that the matcher passes if the surrounding whitespace
in the input matches the surrounding whitespace in the element text exactly.
c13da48
@travisjeffery travisjeffery merged commit c13da48 into velesin:master

1 check passed

Details default The Travis CI build passed
@travisjeffery travisjeffery referenced this pull request from a commit
@travisjeffery travisjeffery "Merge pull request #163 from f1sherman/trim-to-have-text-input\n\nMa…
…tch if the surrounding whitespace in the input matches the element text"

* f1sherman/trim-to-have-text-input:
  Match if the surrounding whitespace in the input matches the element text

Conflicts:
	lib/jasmine-jquery.js
3433183
@travisjeffery
Collaborator

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 13, 2014
  1. @f1sherman

    Match if the surrounding whitespace in the input matches the element …

    f1sherman authored
    …text
    
    Currently the text in the element is trimmed before comparing. This is helpful
    in some cases, but we're unable to match intentional surrounding whitespace.
    
    For instance, given an element like this:
    <div> foo</div>
    The following matcher will always fail:
    expect($div).toHaveText(' foo');
    
    This fixes that case so that the matcher passes if the surrounding whitespace
    in the input matches the surrounding whitespace in the element text exactly.
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 4 deletions.
  1. +4 −3 lib/jasmine-jquery.js
  2. +7 −1 spec/suites/jasmine-jquery-spec.js
View
7 lib/jasmine-jquery.js
@@ -393,12 +393,13 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
},
toHaveText: function (text) {
- var trimmedText = $.trim(this.actual.text())
+ var actualText = this.actual.text()
+ var trimmedText = $.trim(actualText)
if (text && $.isFunction(text.test)) {
- return text.test(trimmedText)
+ return text.test(actualText) || text.test(trimmedText)
} else {
- return trimmedText == text
+ return actualText == text || trimmedText == text
}
},
View
8 spec/suites/jasmine-jquery-spec.js
@@ -643,12 +643,18 @@ describe("jQuery matchers", function () {
expect(element.get(0)).toHaveText(text)
})
- it("should ignore surrounding whitespace", function () {
+ it("should ignore surrounding whitespace in the element", function () {
element = $('<div>\n' + text + '\n</div>')
expect(element).toHaveText(text)
expect(element.get(0)).toHaveText(text)
})
+ it("should match with surrounding whitespace in the input", function () {
+ element = $('<div>\n' + text + '\n</div>')
+ expect(element).toHaveText('\n' + text + '\n')
+ expect(element.get(0)).toHaveText(text)
+ })
+
it("should pass negated when text does not match", function () {
expect(element).not.toHaveText(wrongText)
expect(element.get(0)).not.toHaveText(wrongText)
Something went wrong with that request. Please try again.