ember.jsember-datafixturesember-testingember-cli-mirage

Unable to modify Ember mirage fixture from within acceptance test


I have a fixture create which looks somewhat like this.

// mirage/fixtures/people.js
 export default {
      'people': [
        {
          'id': 1,
          'name': 'Ram',
         },
         {
          'id': 2,
          'name': 'Raja',
         }
       ]
    }

Inside my acceptance test I'm using this array. But within my test, I want to modify this people array and add, suppose another object

{
   'id': 3,
   'name': 'John',
}

Note: I dont want to use factories as I dont want all datas to be dynamically generated, so I want to take this array from fixtures, push my new object into this array and then return it. What is the correct way to do it?

Note2: Don't suggest adding this object in fixtures itself, because I want to dynamically add items to the fixture based on conditions in my test.


Solution

  • This was pretty straight forward. In the mirage config, we shouldn't be doing this

    // import peopleFromFixture from '/mirage/fixtures/people'; 
    // this.get('/people', (schema, request) => {  
    // return peopleFromFixture;  }); 
    

    instead read data from factories and populate original fixture values with server.loadFixtures('people').

    So config.js will look like =>

    this.get('/people'); 
    

    Set your factory like this =>

    import { Factory } from 'ember-cli-mirage';
    export default Factory.extend({
      id(i) {  return i+1; },
      name() { return faker.name.findName(); }
    });
    

    Inside your test case, populate original and new values like this =>

    server.loadFixtures('people');
    server.create('people', { name: 'John' });