Ember-CLI mixin unit test fails on referenced functions - RESOLVED


#1

I can unit test a computed property on a mixin with no issues, unless it calls some external functions. For example, in the property being tested, there’s a call to Ember.$.isNumeric(), or even something as simple as a call to a project util function.

What do I need to do to either stub those functions, or make them work while running the test, as they do in the actual app?

Here’s a test example:

import Ember from 'ember';
import ListingsMapControllerMixin from 'my/mixins/listings-map-controller';

module('ListingsMapControllerMixin');

test('it works', function () {
  var ListingsMapControllerObject = Ember.Object.extend(ListingsMapControllerMixin);
  var subject = ListingsMapControllerObject.create();
  var simpleModel1 = {
        StandardFields: [{UnparsedFirstLineAddress: '1234', Latitude: '12', Longitude: '34'}]
      },
      simpleModel2 = {
        StandardFields: [{UnparsedFirstLineAddress: '4567', Latitude: '45', Longitude: '67'}]
      };

  subject.set('model', [simpleModel1, simpleModel2]);
  console.log(subject.get('markers'));
});

The mixin with the markers computed property:

import Ember from 'ember';
import DS from 'ember-data';
import getStandardField from 'my/utils/get-standard-field';

export default Ember.Mixin.create({
  // Initialize with USA bounds:
  centerLat: 39,
  centerLng: -98,
  zoom: 5,

  markers: function () {
    var markers = [];

    this.get('model').forEach(function (model) {
      var listing = model,
          marker,
          title,
          lat,
          lng;

      // FAILS with an 'undefined' error:
      title = getStandardField(listing, 'UnparsedFirstLineAddress');
      lat = getStandardField(listing, 'Latitude');
      lng = getStandardField(listing, 'Longitude');

      // FAILS with an 'undefined' error:
      if (!Ember.$.isNumeric(lat) || !Ember.$.isNumeric(lng)) {
        return;
      }

      marker = {listing: listing};
      marker.title = title;
      marker.lat = lat;
      marker.lng = lng;

      markers.push(marker);
    });

    return markers;
  }.property('model')
});

#2

I think I found the reason for this, and it’s something to do with the object being sent to the util function. Gonna try to work around it. This question is no longer valid as written. May re-write if I run into more issues here.