Skip to content

Commit

Permalink
Add length() sugar method to lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Buob committed Sep 11, 2014
1 parent b312bb2 commit 60080e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This list of words can abstracted and interacted with via a `List` with ease. He
* [ItemSelector](#itemselector)
* [ItemClass](#itemclass)
* [Items](#items)
* [Length](#length)
* [Filter](#filter)
* [Map](#map)
* [Each](#each)
Expand All @@ -55,6 +56,16 @@ The `Widget` to be instantiated and used when interacting with each item in the

Returns a `Promise` that resolves to a list of `Widgets` present in the `DOM` at call time.

## Length

`function length()...`

Returns a `Promise` that resolves to the length of the list.

```js
new ListItems().length().should.eventually.eql(5)
```

## Filter

`function filter(<predicateMethod>(itemInstance))...`
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/Widget.List.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class @Widget.List extends @Widget
each: (iter) ->
@map.apply(this, arguments).then -> @items

length: ->
@items().then (items) -> items.length

invoke: (opts) ->
if(_.isString(opts) or _.isFunction(opts))
opts = {method: opts}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/features/list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Feature: Manipulating Lists
Scenario: Calling AT with a string
When I call at with a string I should get an error

Scenario: Getting length of a list
When I call length I should receive 5

Scenario: Nested list lookup
When I find the "span" within ".nested" I should see 3 items

Expand Down
4 changes: 4 additions & 0 deletions test/integration/steps/list_steps.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ module.exports = ->
.then (item) -> item.getHtml()
.should.eventually.eql(content)

@When /^I call length I should receive (\d+)$/, (expectedLength) ->
new @Widgets.List().length()
.should.eventually.eql(+expectedLength)

@When /^I call at with a string I should get an error$/, ->
expect( =>
new @Widgets.List().at("0")
Expand Down

0 comments on commit 60080e5

Please sign in to comment.