vendredi 26 octobre 2018

Mocking an ember in-repo-addon service

I have an in-repo-addon:

/my-app/lib/my-service/app/services/my-service.js

import AjaxService from 'ember-ajax/services/ajax';
import ENV from '../config/environment';

export default AjaxService.extend({
    host: ENV.APP.BACKEND_URL,
    contentType: 'application/json; charset=utf-8',

    subscribe(emailAddress) {
       ...
       return this.request(url, {method: 'POST', data: data});
    }

I have a main app component that uses this in-repo-addon.

/my-app/app/components/my-comp.js

import Ember from "ember";

export default Ember.Component.extend({
   myService: Ember.inject.service(),
   ...
});

And that works when the application is run normally. I also have a test for that component.

/my-app/tests/integration/components/my-comp-test.js

import { moduleForComponent, test } from 'ember-qunit';
import ENV from 'my-app/config/environment';

moduleForComponent('sign-up', 'Integration | Component | my comp', {
    integration: true,
});

test('email addresses are trimmed before submission to remote service', function (assert) {
  const signupEmail = " NotAnEmail@example.com ";

  let myService = Ember.Service.extend({

    subscribeUrl() {
      return "";
    },

    request(url, payload) {
      assert.equal("NotAnEmail@example.com", payload.data.emailAddress);
      return new Ember.RSVP.Promise((res) => {
        res({message: 'success'});
      });
    }
  });
console.log("myService=", myService);
this.register('service:my-service', myService);
...

The test tries to create myService and extend it to override/mock various bits to test an assert to make sure the email address has been trimmed.

But myService is always null. It looks like Ember.Service is not including the in-repo-addons when it builds the test, so myService is never found.

I have tried to import the service directly with the following but ember doesn't seem to like that either:

import myService from 'my-app/lib/my-service/app/services/my-service/my-service';
// or
import myService from 'my-app/lib/my-service/app/services/my-service';

Is this a bug with Ember test? How can I fix this?




Aucun commentaire:

Enregistrer un commentaire