1

I'm quite new to the ember community, and I have created a component that generates its content base on a given DS.Model object :

// templates/test.hbs
{{my-component model=model.model modelName=model.modelName}}

// routes/test.js
import MyModel from 'app/models/my-model

export default Ember.Route.extend({
  model() {
    model: MyModel,
    modelName: 'my-model'
  }
});

// components/my-component.js
export default Ember.Component.extend({
  modelAttributes: Ember.computed(function() {
    let model = this.get('model');
    let attributes = [];
 
    // retrieves options if exists
    let labels = this.get('labels');
    let requires = this.get('requires');

    // Populating modelAttributes
    model.eachAttribute((attr, meta) => {
      let attributesInfo = {key: attr, type: meta.type, data: ''};
      // do some stuff
      attributes.push(attributesInfo);
    });
    return attributes;
  })
});

It works great but I'm quite stuck at testing this. Indeed I tried :

// tests/acceptances/components/my-component
moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true
});

const DummyModel = DS.Model.extend({
  string: DS.attr('string'),
  email: DS.attr('email')
});

test('it renders', function(assert) {
  server.createList('dummy', 5);

  this.set('modelDescription', DummyModel);
  this.set('modelName', 'dummy');

  this.render(hbs`{{easy-crud modelName=modelName model=modelDescription}}`);

  // asserting stuff
  
 });

and using mirage to fake dummy objects. but it keeps on failing with the following error :

Died on test #1 at Module.callback (http://localhost:7357/assets/tests.js:698:24)
    at Module.exports (http://localhost:7357/assets/vendor.js:123:32)
    at requireModule (http://localhost:7357/assets/vendor.js:38:18)
    at TestLoader.require (http://localhost:7357/assets/test-support.js:6979:7)
    at TestLoader.loadModules (http://localhost:7357/assets/test-support.js:6971:14)
    at Function.TestLoader.load (http://localhost:7357/assets/test-support.js:7001:22)
    at http://localhost:7357/assets/test-support.js:6885:18: Unexpected token u in JSON at position 0

Does anyone have an idea ? Or am I doing something wrong ?

Thanks

edit : adding ember functions for code comprehension

  • I don't see any Mirage code in that test. Also, the code in that component is a bit strange-looking. I think you may be off on the wrong path with how you are using the model in your component. Check out the guides a bit more and try to keep it simple while you're still learning. – AlexMA Mar 13 '17 at 18:20
  • Um, `model() { model: MyModel, modelName: 'my-model'}` is not valid JS. – Lux Mar 13 '17 at 20:44
  • This code actually work fine. In the component I'm using the model to retrieve attributes and their types in order to create a CRUD form based on the model description. I only got a issue about how to test this component. – Nicolas Kamennoff Mar 14 '17 at 13:51

0 Answers0