lundi 14 décembre 2015

In an Ember route unit test how do I stub setupController and then test a routes method

I have a route with no controller file explicitly created. My route looks something like:

import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import vibrate from '../utils/utility-vibrate';

export default Ember.Route.extend(ApplicationRouteMixin, {
  setupController: function(controller, model) {
    this._super(controller, model);

    controller.set('session', Ember.inject.service('session'));

    controller.set('leftSidebarWidgets', []);
    controller.set('rightSidebarWidgets', []);
    controller.set('unpricedOrders', []);
    controller.set('loadingUnpricedOrders', {value: true});
  },
  deleteRecordValidation: function(modelName) {
    if (!modelName) {
      return true;
    }

    if (modelName === 'orderItem') {
      let rightHandWidgets = this.controller.get('rightSidebarWidgets');

      if (rightHandWidgets.any(w => Ember.get(w, 'name') === 'widgets/add-update-order-item')) {
        let orderItemDiv = Ember.$('.widgets\\/add-update-order-item');
        vibrate(orderItemDiv);

        return false;
      }
    }

    return true;
  }
  ...
});

I then have a unit test:

import { moduleFor, test } from 'ember-qunit';

moduleFor('route:application', 'Unit | Route | application', {
  needs: ['service:session']
});

test('delete record validation returns false if order item delete and update order item widget open', function(assert) {
  assert.expect(1);

  let route = this.subject();

  route.controller.get('rightHandWidgets').insertAt(0, {
    name: 'widgets/add-update-order-item'
  });

  assert.notOk(route.deleteRecordValidation('orderItem'));
});

I'm trying to add something to the controller to test a particular scenario - the issue is the test falls over with Cannot read property 'get' of undefined as route.controller is null. Do I need to stub setup controller or something else to get the 'controller' in place? If so how?




Aucun commentaire:

Enregistrer un commentaire