mercredi 25 juillet 2018

Ember JS testing - Factory guy

I am using ember 2.18 and ember-data-factory-guy 3.3.0.

I want to test an action inside a component. In the action, I am using makeList in order to create and return a list of models. In my ember app, I pass the store to the component and I am doing a query when I a call the action.

Here is my Factory:

import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define('contact', {
  sequences: {
    first_name: (num) => `FirstName${num}`,
    last_name:  (num) => `LastName${num}`,
    email:      (num) => `contact${num}@example.com`,
    phone:      (num) => `01234567${num}`,
    linkedin:   (num) => `samplelinkedinurl${num}`,
  },
  default: {
    first_name: FactoryGuy.generate('first_name'),
    last_name:  FactoryGuy.generate('last_name'),
    email:      FactoryGuy.generate('email'),
    phone:      FactoryGuy.generate('phone'),
    linkedin:   FactoryGuy.generate('linkedin'),
    position:   'CEO',
    client_office: FactoryGuy.belongsTo('office')
  },
  traits: {
    withClient: {
      client: FactoryGuy.belongsTo('client')
    },
    withOffice: {
      client_office: FactoryGuy.belongsTo('office')
    }
  }
});

Additionaly, my action is the followin:

_this.store.query('contact', {term: searchTerm}).then(function(contacts) { ... })

I have create a TestStore class in order to test actions like these. It is a minimal replication of the querying methods of ember data store. The query method looks like that:

query(modelName, attributes, trait=null) {
  console.log('ATTRIBUTES PASSED TO TEST STORE QUERY: ', attributes)

  // make a new record of selected model with factory guy
  let _this = this,
       records = Ember.isEmpty(trait) ? makeList(modelName, 4) : makeList(modelName, 4, trait);

  // Push it to test-store
  if (!this.models[modelName]) {
      this.models[modelName] = Ember.A([]);
  }

  records.forEach(function(record) { 
  _this.models[modelName].pushObject(record); })


  // Create promise to return
  let recordPromise = new Ember.RSVP.Promise((resolve) => {
    resolve(records);
  });
  return DS.PromiseArray.create({promise: recordPromise});
}

In rendering tests it is working just fine. If I am to run the action manually (e.g. like in unit tests ), I get the following error:

FactoryGuy does not have the application's

manualSetup is not working as well, as the store service is stuck in the "willDestroy()" of the store and _pushedInternalModels is null so no Models can be pushed to the store.

Any ideas/suggestions on that?




Aucun commentaire:

Enregistrer un commentaire