Skip to content

Commit

Permalink
Merge pull request #5 from rupurt/ak-include-text
Browse files Browse the repository at this point in the history
Add support for text contains
  • Loading branch information
nathanboktae committed Nov 7, 2015
2 parents f60cde7 + 7d7a4ae commit ed89810
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ expect(document.querySelector('#title')).to.have.html('Chai Tea')
```

### `text(text)`
Assert that the text of the [HTMLElement][] is equal to the given text, using [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).
Assert that the text of the [HTMLElement][] is equal to or contains the given text, using [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).

```js
document.querySelector('.name').should.have.text('John Doe')
expect(document.querySelector('#title')).to.have.text('Chai Tea')
```

```js
document.querySelector('.name').should.contain.text('John')
expect(document.querySelector('#title')).to.contain.text('Chai')
```

### `value(value)`
Assert that the [HTMLElement][] has the given value

Expand Down
25 changes: 18 additions & 7 deletions chai-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,24 @@

chai.Assertion.addMethod('text', function(text) {
var el = flag(this, 'object'), actual = flag(this, 'object').textContent
this.assert(
actual === text
, 'expected ' + elToString(el) + ' to have text #{exp}, but the text was #{act}'
, 'expected ' + elToString(el) + ' not to have text #{exp}'
, text
, actual
)

if (flag(this, 'contains')) {
this.assert(
actual.indexOf(text) >= 0
, 'expected #{act} to contain #{exp}'
, 'expected #{act} not to contain #{exp}'
, text
, actual
)
} else {
this.assert(
actual === text
, 'expected ' + elToString(el) + ' to have text #{exp}, but the text was #{act}'
, 'expected ' + elToString(el) + ' not to have text #{exp}'
, text
, actual
)
}
})

chai.Assertion.addMethod('value', function(value) {
Expand Down
20 changes: 20 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ describe('DOM assertions', function() {
subject.should.not.have.text('foo')
}).should.fail('expected div not to have text \'foo\'')
})

it('passes when the text contains', function() {
subject.should.contains.text('fo')
})

it('passes negated when the text doesn\'t contain', function() {
subject.should.not.contain.text('bar')
})

it('fails when the text doesn\'t contain', function() {
(function() {
subject.should.contain.text('bar')
}).should.fail('expected \'foo\' to contain \'bar\'')
})

it('fails negated when the text contains', function() {
(function() {
subject.should.not.contain.text('fo')
}).should.fail('expected \'foo\' not to contain \'fo\'')
})
})

describe('value', function() {
Expand Down

0 comments on commit ed89810

Please sign in to comment.