简体   繁体   中英

Using filter inside Ember.RSVP.hash fails to retrieve data in qunit test

I have a qunit test on a route that uses TWO models and use Ember.RSVP.hash to accomplish this. Things work as expected but as soon as I add a filter to Ember.RSVP.hash it stops retrieving data in qunit. Outside of qunit it still works so I believe it's an issue with filter , Ember.RSVP.hash and qunit .

Here's my test:

module('Ember.js Library', {
  setup: function() {
    Ember.run(App, App.advanceReadiness);
  },
  teardown: function() {
    App.reset();
  }
});

test('foo', function() {
  visit('/books');
  andThen(function() {
    equal(currentRouteName(), 'books.index');
  });
});

Here's the working route that does not use filter :

App.BooksRoute = Ember.Route.extend({
  model: function() {
    var id = 1;

    // note this does not filter "books" properly
    return Ember.RSVP.hash({
      books: this.store.find('books'),
      library: this.store.find('library', id)
    });
  }
});

Here's the version that uses filter and does not work:

App.BooksRoute = Ember.Route.extend({
  model: function() {
    var id = 1;

    // note: filters "books" properly but doesn't work in qunit
    return Ember.RSVP.hash({
      books: this.store.filter('books', function(record) {
        return record.get('libraryId') === id;
      }),
      library: this.store.find('library', id)
    });
  }
});

The end result is that in the working version, books is present as expected. In the second version, books ends up being empty. In both versions the library is always present so I believe filter is the problem.

How can I use filter and get things to work with qunit?

store.filter doesn't actually load the records, it will only return records that are already in the store. What you probably want to do is something like...

App.BooksRoute = Ember.Route.extend({
  model: function() {
    var id = 1;

    // note: filters "books" properly but doesn't work in qunit
    return Ember.RSVP.hash({
      books: this.store.find('books').then(function(books){
        return books.filterBy('libraryId', id)
      }),
      library: this.store.find('library', id)
    });
  }
});

(disclaimer: the code above is untested but thats the idea)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM