We have a number of unit tests on views that all look have the following boilerplate at the top:
var dispatcher;
before(function() {
dispatcher = Em.EventDispatcher.create();
eventDispatcher.setup();
});
after(function() {
dispatcher.destroy();
});
We pulled that from Ember’s own tests on the action
helper.
That has been working fine so far, but just today I discovered that it only works because of a happy accident. Ember.EventDispatcher#setup
ensures there aren’t any other dispatchers on the same element or any parent element. As soon as DOM-ready fires, Ember.Application
sets up an Ember.EventDispatcher
on <body>
. Our tests were only passing because something happened to be blocking DOM-ready, preventing the global dispatcher from being initialized. As soon as we fixed that, all the tests that create their own dispatchers began failing.
I have a few ideas:
- prevent the instance of
Ember.Application
from being created in test mode. - tear down the instance of
Ember.Application
after each spec and rebuild it before. This is probably not possible for us given the amount of initialization code that happens on app boot. It would slow down our test suite from ~10m to ~60m. - use some other way of testing the events in these views.