Ember computed property, load data from store in integration tests


#1

Hello,

Sorry, I’m new in ember, and I have a problem with the tests. This code, work perfectly in development:

filteredLists: Ember.computed('currentUser.lists.@each.current', function(){
        var ret  = [];
        var lists = this.get('user.currentUser.lists');
        lists.forEach(function(list){

//Here the problem, the playlist is in state of destroying and does not set the properties
//always return 'undefined'. I tried putting an Ember.run in some places unsuccessfully.

            if(list.get('current') === false){
                ret.pushObject(playlist);
            }
        });
        return ret;
    }),

I’m trying to do an integration test:

Ember.run(function(){
//The user is loaded correctly from ember mirage.
        store.find('user', 1 ).then(function(user){
            userStub.set('currentUser', user);
        });
    });
    return wait().then(() => {
        Ember.run(function(){
           //Load the computed property
            lists  = ctrl.get('filteredLists');
        });
        return wait().then(()=>{
            assert.equal(lists.length, 5);
        });
    });

I tried a lot of things, and there is no way to do it work because the list object is always destroying when the program access it to check the property.

This test is working for other code, but the other code has not to load the relationship and filter with it.

Somebody know how to fix ‘loading’ problems in tests with nested loads???

Thanks.


#2

I fixed the problem. I had to use a PromiseArray:

filteredLists: Ember.computed('currentUser.lists.@each.current', function(){
        var self = this;
        var promise = self.get('currentUser.lists')
                        .then(function(lists){
                            var ret = [];
                            lists.forEach(function(list){
                                if(list.get('current') === false){
                                    ret.pushObject(list);
                                }
                            });
                            return ret;
                        });

        return DS.PromiseArray.create({
           promise: promise
        });        
    }),

Easy, great Ember. :two_hearts:


#3

If you used “ember-data-factory-guy” ( addon ) you would not have to use promises in integration tests. you can put data ( with relationships ) directly in to the data store. Makes unit tests and integration tests so so easy.