mercredi 25 octobre 2017

How to use fixture with reflexive relation in ember-cli-mirage?

I have an article model that is reflexive to itself through a related_articles fields. In order to have a finer control of the context in my tests, I want to use fixture.

model

// app/models/article.js
export default Model.extend({
  featured: attr('boolean'),
  related_articles: hasMany('article', { inverse: null })
});

fixture

// mirage/fixtures/articles.js
  export default [{
    id: 2,
    featured: true,
    related_articles: [1, 2]
  }];

scenario

// mirage/scenarios/default.js
export default function(server) {
  server.createList('category', 10);
  server.loadFixtures('articles');
}

Test

test('has related-articles block', function(assert) {
  authenticate(this.application);
  server.loadFixtures('articles');

  visit('/app/articles/2');
  andThen(function() {
    let relatedArticlesBlock = document.querySelectorAll('.related-articles');
    assert.ok(relatedArticlesBlock.length);
  });
});

Error

Mirage: Your GET handler for the url /api/v2/employee/articles/2 threw an error: model.hasInverseFor is not a function

Question

There is no related articles in the related_articles key returned by mirage. What's the matter?




Aucun commentaire:

Enregistrer un commentaire