Ember.run.later testing behavior

I created a simple flash messaging service in an Ember-CLI app based on this post: https://medium.com/delightful-ui-for-ember-apps/adding-flash-messages-to-an-ember-app-437b13e49c1b

As the code in the post shows, the service relies on Ember.run.later to destroy messages after a specified period of time.

I use this service in various parts of my application, at the route and controller level.

I wanted to test this functionality with some integration tests. For example, on an application route, a user can send a search query to the server. If the request is not well formed I am making a mistake using the flash messaging service. Likewise, I can check if the authentication is successful:

`import Ember from 'ember'`
`import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin'`

ApplicationRoute = Ember.Route.extend ApplicationRouteMixin,
  setupController: (ctrl) ->
    flashes = @get 'flashes'
    ctrl.set 'flashes', flashes
  actions:
    error: (e) -> 
      if e.jqXHR and e.jqXHR.status is 404
        @get('flashes').warning('no data from the server')
    sessionAuthenticationFailed: (e) ->
      @get('flashes').danger('another relevant error message')

`export default ApplicationRoute`

      

To test this, I would simulate the desired behavior and then check for the presence of the message:

test 'checking if the error appears on a 404', ->
  # series of steps that lead to a 404
  andThen ->
    equal(find('.ember-notify').text(), 'no data from the server')

      

Where "ember-notify" is the class name of the div containing the collapsed message.

I see the message popping up in the test container at will, and then the tests pause "until the message disappears. That means my test fails because it checks for the existence of the div after it disappears.

I understand this is explained in this thread: https://github.com/emberjs/ember.js/issues/5362

for example, "Checking Ember tests to complete scheduled timers before your application crashes."

So my test environment will always wait for Ember.run.later to finish.

Any thoughts / ideas on how to approach this? Is there a better way to check for error events at the route level?

My thought was that I could directly check if an error event was fired on the application route, for example, but I don't know how to do that (and how can I extend this pattern to testing on controllers where I also use the flash service messages?)

Thank!

+3


source to share





All Articles