Skip to content
This repository has been archived by the owner on Sep 20, 2018. It is now read-only.

Commit

Permalink
[added] nth() method
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Feb 27, 2016
1 parent fca13e1 commit 0b774a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ let common = {
: $(this, assertLength(this, 'last')[this.length - 1], this)
},

nth(n, selector) {
n = Math.max(0, Math.min(n, this.length))
return selector
? this.find(selector).nth(n)
: $(this, assertLength(this, 'nth')[n], this)
},

only() {
if (this.length !== 1)
throw new Error('The query found: ' + this.length + ' items not 1')
Expand Down
16 changes: 16 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,22 @@ describe('common', ()=> {
.should.throw('the method `last()` found no matching elements')
})

it('.nth()', ()=> {
render(<Example />)
.nth(1, 'li')
.text().should.equal('item 2')

render(<Example />)
.find('li')
.nth(1)
.text().should.equal('item 2')
})

it('.nth() should throw if there are no elements', ()=> {
;(() => render(<Example />).nth(0, 'article'))
.should.throw('the method `nth()` found no matching elements')
})

it('.single()', ()=> {
render(<Example />).single(Stateless).length.should.equal(1)
render(<Example />).find(Stateless).single().length.should.equal(1)
Expand Down

0 comments on commit 0b774a0

Please sign in to comment.