samedi 17 février 2018

How to test computed property that returns PromiseArray in Ember

I have a computed property that asks server for user data and then the other one that computes number of users. To propagate changes into the template, I'm using DS.PromiseArray wrapper. With this wrapper, I can't find an easy way to test this property.

  // model
  users: computed('name', function () {
    let name = get(this, 'name');
    return DS.PromiseArray.create({
      promise: this.store.query('user', { name })
    });
  }),

  numberOfUsers: computed('users', function () {
    return get(this, 'users.length') || 0;
  }),

  // ....

  test('numberOfUsers returns correct number', function (assert) {
    let model = this.subject({
      store: EmberObject.create({
        query() {
          return Promise.resolve([
            { name: 'Thomas' },
            { name: 'Thomas' },
            { name: 'Thomas' },
          ]);
        }
      }),
      name: 'Thomas',
    });

    assert.equal(model.get('numberOfUsers'), 3);
  });

This test fails with 0 !== 3. Any ideas?




Aucun commentaire:

Enregistrer un commentaire