Created
October 16, 2012 23:08
-
-
Save samwgoldman/3902649 to your computer and use it in GitHub Desktop.
Async testing pattern. Better way?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment