dagda1
dagda1

Reputation: 28890

Testing Ember.js and the runloop

While running code in production, I don't have to interact with the Ember.js runloop but in my tests, I sometimes have to call Enber.run.end() to make the tests pass. This might have knock on effects of making other tests fail.

For example, here is my spec_helper:

beforeEach ->
  $('#jasmine_content').append('<div id="fieldset"></div>')
  Ember.test = true
  Lead.run()

$ ->
  $('body').append($('<div id="jasmine_content"></div>'))

I am setting the Ember.test flag to true in the above code which I am not entirely sure what this does.

Lead.run() creates the Ember application:

window.Lead = Ember.Application.create
  Controllers: {Leads: {}}
  Models: {}
  Views: {}

  run: ->
    @initEvents()
    @.set('search', Lead.Controllers.UrlSearch.create())
    # etc.

In some tests, I find myself having to call ember.run.end() to ensure that items will be in the DOM etc.

describe 'Controllers', ->
  describe 'UrlSearch', ->
    it 'should append view', ->
      Ember.run.end() 
      expect($('#goButton').length).toEqual(1)

Ending the runloop from individual tests feels wrong.

Whenever I update my spec_helper to this:

beforeEach ->
  $('#jasmine_content').append('<div id="fieldset"></div>')
  Ember.test = true
  Lead.run()
  Ember.run.end()

I get error messages like:

Cannot perform operations on a Metamorph that is not in the DOM

or

Must have a current run loop.

Are there any guidance or best practices for negotiating the runloop from tests?

Upvotes: 3

Views: 1507

Answers (1)

pangratz
pangratz

Reputation: 16163

You should take a look at the tests, for example each_test.js. As you can see, adding the view to DOM or manipulating properties of controllers, ... is always done within Ember.run to assure all changes and view updates are flushed and the bindings are synced:

Ember.run(function(){
  view.append();
  ...
});

Upvotes: 1

Related Questions