vendredi 7 avril 2017

Route testing in ember with stubbed controller - 'Cannot read property 'activeTransition' of undefined'

I have a test, route and controller file like below:

test.js:

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

const QUERY_PARAMS = Object.freeze({
  q: null,
  offset: 0,
  fundingMechanism: [],
  activity: [],
  name: [],
  orgCountry: [],
  SpendingCats: [],
  applicationType: [],
});

let controller = Object.create({
  QUERY_PARAMS
});

moduleFor('route:foo', 'Unit | Route | foo', {
  // Specify the other units that are required for this test.
  needs: ['controller:foo', 'service:ajax']
});

test('it exists', function(assert) {
  let route = this.subject();
  assert.ok(route);
});

test("it paginates", function(assert) {
  let route = this.subject();
  route.setupController(controller);
  let controller = route.controllerFor('explore');
  console.log(route.controller);
  var paginationUrl = 'api/v1/foo?offset=10';
  route.send('paginateTo', paginationUrl);
  assert.equal(controller.get('offset'), 10, 'expect offset to be equal to 10');
});

route.js:

import $ from 'jquery';
import Route from 'ember-route';
import get from 'ember-metal/get';
import service from 'ember-service/inject';
import { assert } from 'ember-metal/utils';
import { QUERY_PARAMS } from './controller';
import { cleanObj } from '../../utils/object-utils';

let queryParams = { };

Object.keys(QUERY_PARAMS).map(function(key) {

  var obj2 = {
    [key]: {refreshModel: true}
  }

  for(var attrname in obj2) {
    queryParams[attrname] = obj2[attrname];
  }
});

export default Route.extend({
  queryParams,
  defaultQueryParams: QUERY_PARAMS,
  ajax: service(),

  model(params) {
    return get(this, 'ajax')
      .request('grants', { data: cleanObj(params) })
      .then((result) => {
        return result;
      }).catch((err) => {
        assert('bad explore route results:', err);
        return { id: 'error' };
      });
  },

  setupController(controller) {
    console.log(this.paramsFor('explore'));
    this._super(...arguments);
    // currentQueryParams object is passed to actions-sidebar
    controller.set('currentQueryParams', this.paramsFor('explore'));
  },

  actions: {
    clearSearch() {
      this.controller.set('q', null);
      this.controller.set('offset', 0);
    },

    resetParamsToDefault() {
      let queryParams = get(this, 'defaultQueryParams');
      this.transitionTo('explore', { queryParams });
    },

    filterTable(filterParams) {
      Object.keys(filterParams).map((key) => this.controller.set(key, filterParams[key]));
      // after filtering, go back to the first page of the results table
      this.controller.set('offset', 0);
    },

    paginateTo(paginationUrl) {
      let offset = Number(paginationUrl.match(/offset=([0-9]+)/)[1]);
      this.controller.set('offset', offset);
      $('html, body').animate({ scrollTop: 0 });
    },

    transitionToCollections() {
      this.transitionTo('collections');
    }
  }
});

controller.js

import Controller from 'ember-controller';

export const QUERY_PARAMS = Object.freeze({
  q: null,
  offset: 0,
  fundingMechanism: [],
  activity: [],
  name: [],
  orgCountry: [],
  SpendingCats: [],
  applicationType: [],
});

export default Controller.extend({
  ...QUERY_PARAMS
});

When I run ember test with PhantomJS I receive this unknown error: undefined is not an object (evaluating 'this.router.router.activeTransition') I'm not entirely sure what this relates to or where in my code is breaking the test. I'm an ember novice so I'd appreciate any tips or advice, thank you in advance.




Aucun commentaire:

Enregistrer un commentaire