Search code examples
javascriptember.jsjasminejasmine-jquery

testing a views controller with emberjs


I have a BuildingBlocks view that has a template like this :

{{#each controller}}
  {{view App.BuildingBlockView contentBinding="this"}}
{{/each}}

I am trying to test that this view loops over each post using the controller.
I am using Jasmine.

I have created the view like this

App.buildingBlocksView = App.BuildingBlocksView.create()
Em.run ->
  App.buildingBlocksView.append()

And i want to populate the controller for this view with 3 BuildingBlocks so i can test that this view contains 3 elements.

Please can anyone tell me how to do this ?

I am doing this so far but does not work.

App.buildingBlocksController = App.BuildingBlocksController.create()
App.buildingBlocksView.set("controller", App.buildingBlocksController.content)
App.buildingBlocksView.controller.pushObject(App.BuildingBlock.createRecord(), App.BuildingBlock.createRecord(), App.BuildingBlock.createRecord())
expect(App.buildingBlocksView.$('.building-block').length).toEqual(3)

But i am getting error Cannot call method 'pushObject' of undefined

Please help Rick


Solution

  • Looks like you are on the right track - props for using jasmine.

    First, I don't see the the definition in your question so double check that App.BuildingBlocksController is a subclass of Ember.ArrayController

    Assuming that's the case, you'll want to set the initial content array when creating the instance. Then set controller property of the view to the controller instance, and push objects as before.

    App.buildingBlocksController = App.BuildingBlocksController.create({ content: [] })
    App.buildingBlocksView.set("controller", App.buildingBlocksController)    
    App.buildingBlocksView.controller.pushObject(App.BuildingBlock.createRecord(), App.BuildingBlock.createRecord(), App.BuildingBlock.createRecord())
    

    That should solve the problem you're having with pushObject. I suspect you'll need to move the above within the Ember.run() loop, otherwise expectation will fail since Ember will not have updated the DOM.