Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.