1

So my acceptance test keeps on destroying itself before my promise finishes. I'm aware that I need to wrap my promise in Ember run loop but I just can't get it to work. Here's how my component looks:

export default Ember.Component.extend({
  store: Ember.inject.service(),

  didReceiveAttrs() {
    this.handleSearchQueryChange();
  },

  /**
   * Search based on the searchQuery
   */
  handleSearchQueryChange() {
    this.get('store').query('animals', {
      orderBy: 'name',
      startAt: this.attrs.searchQuery
    }).then(searchResults => {
      this.set('searchResults', searchResults);
    });
  }
});

I've already tried wrapping this.handleSearchQueryChange(), this.get('store').query... and this.set('searchResults', searchResults) in a run loop but still, the acceptance test just doesn't wait for store.query to finish.

One thing to note that this store query performs a request on a live Firebase back-end.

I'm currently using Pretender to mock the data and solve this issue. But I'd like to solve it through Ember.run as well. Anyone care to provide a solution?

Mikko Paderes
  • 830
  • 9
  • 18

1 Answers1

0

It sounds like your problem may have the same cause as the errors I've been seeing

tl;dr

To work around this issue, I've been using a custom test waiter. You can install it with ember install ember-cli-test-model-waiter (for Ember v2.0+) and it should just make your test work without any further setup (if not, please file a bug).

Longer answer:

The root cause of this problem is that the ember testing system doesn't know how to handle Firebase's asynchronicity. With most adapters, this isn't a problem, because the testing system instruments AJAX calls and ensures they have completed before proceeding, but this doesn't work with Firebase's websockets communication.

The custom test waiter I mentioned above works by waiting for all models to resolve before proceeding with the test, and so should work with any non-AJAX adapter.

Community
  • 1
  • 1
Gabriel Grant
  • 5,415
  • 2
  • 32
  • 40
  • Thanks I was afraid that I'd get that long answer. Oh well. Like I said, I'm currently working around this by using pretender but ideally I'd like to use it on a running test database in Firebase so that I could also test out my security rules. I'll look into your addon when I'm able to. Thanks! – Mikko Paderes Oct 30 '15 at 03:42
  • Yes, I wrote the waiter for exactly that reason: wanting to test against a live (test instance) firebase. Just installing it should be all that's needed to make your tests work (if not, please let me know). Another option that I haven't looked into too much is [firebase-server](https://github.com/urish/firebase-server) -- it's [promoted by firebase as an official solution for end-to-end testing](https://www.firebase.com/blog/2015-04-24-end-to-end-testing-firebase-server.html) – Gabriel Grant Oct 30 '15 at 16:48
  • Actually, that's not quite true if you need your test to go through the firebase login process. There's still a (related) bug blocking that. I've filed [a pull request to fix it](https://github.com/firebase/emberfire/pull/330), but until that lands you'll need a little manual bookkeeping around the login: https://github.com/gabrielgrant/empty/commit/257776d32dc300f0c0dde8d2904274172e2067d5 – Gabriel Grant Oct 30 '15 at 17:00
  • For sign in, I'm currently using Torii's built-in test-helper for stubbing sessions during acceptance tests. That way I could easily test session-based scenarios. I haven't tested it on a live firebase though, only in a pretender. Though I'm expecting it seamlessly work with it and your addon. – Mikko Paderes Oct 31 '15 at 06:19
  • Yes, that should work to get things running; I wanted to use the actual login so I could test my real security rules – Gabriel Grant Oct 31 '15 at 15:01