ember.jsintegration-testingember-qunit

How to fix 'service injection' for EmberJS integration test?


I have a tasks-table component that uses 'current-user' service. I inject the service into the component as displayed below

import { inject as service } from '@ember/service';
export default Component.extend({
   currentUser: service(),
   showBatchAction: computed('currentUser.user.directRoles.@each.title', function() {
    return this.get('currentUser.user.directRoles').toArray().some((role) => {
      return (role.id == 13) || (role.id == 15)
    });
  }),
});

The code works fine however, the integration test fails.

Test:

module('Integration | Component | tasks-table export csv button', function(hooks) {
  setupRenderingTest(hooks);

  hooks.beforeEach(function() {
    run(() => {
      this.owner.unregister('service:current-user');
    });

    this.owner.register('serivce:current-user', Service.extend({
      user: EmberObject.create({
        first_name: 'Bob',
        last_name: 'Newby',
        role: 'client',
        directRoles: {title: 'employee', id: 1}
      })
    }));
  });

  test('it renders', async function(assert) {
    var component = this.subject({
      authManager: stubMyService.create()
    });
    await render(hbs `{{tasks-table}}`);

    assert.equal(this.$('.export-csv').text().trim(), 'Export as CSV');
  });

});

The error I'm getting is: TypeError: undefined is not an object (evaluating 'this.get('currentUser.user.directRoles').toArray')


Solution

  • directRoles is not an array? How come you loop through (some method) over an object. By saying object.toArray() does not work.