Skip to content

Instantly share code, notes, and snippets.

@samwgoldman
Created October 16, 2012 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samwgoldman/3902649 to your computer and use it in GitHub Desktop.
Save samwgoldman/3902649 to your computer and use it in GitHub Desktop.
Async testing pattern. Better way?
class App.Components.Select extends Backbone.View
template: HandlebarsTemplates.select
initialize: ->
@labelField = @options.labelField
@valueField = @options.valueField
@collection.bind("reset", => @render)
render: ->
options = @collection.map (item) =>
value: item.get(@valueField)
label: item.get(@labelField)
@$el.html(@template(options: options))
return this
describe App.Components.Select, ->
models = [{ foo: "value1", bar: "label1" }, { foo: "value2", bar: "label2" }]
# simulate async data loading
class collectionClass extends Backbone.Collection
sync: (method, model, options) ->
assert.equal(method, "read")
_.defer options.success, models
beforeEach ->
@collection = new collectionClass
@select = new App.Components.Select
collection: @collection
valueField: "foo"
labelField: "bar"
it "indicates a loading status", ->
expect(@select.render().$el.text()).to.match(/\s*Loading...\s*/)
context "once the collection has loaded", ->
beforeEach (done) ->
@collection.fetch success: -> done()
it "renders the select options", ->
html = @select.render().$el.find("select option")
options = _.map html, (option) -> { foo: option.value, bar: option.text }
expect(options).to.eql(models)
{{#if options}}
<select>
{{#each options}}
<option value="{{value}}">{{label}}</option>
{{/each}}
</select>
{{else}}
Loading...
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment