1
votes

I'm currently writing tests for my App written with EmberJS. I'm using Mirage. I have the two following models:

mirage/models/paperwork.js

export default Model.extend({
customer: belongsTo('customer'),
paperwork_products: hasMany('paperwork-product', { inverse: 'paperwork' }),

mirage/models/paperwork-product.js

export default Model.extend({
paperwork: belongsTo('paperwork', { inverse: 'paperwork_products' }),
});

In my scenario, I'm creating my datas like this:

const paperwork = server.create('paperwork');                                                                                   
const paperworkProduct = server.create('paperwork-product', { paperwork });                      
paperwork.paperwork_products.add(paperworkProduct);

My route:

export default ApplicationRoute.extend({
    model(params) {
        return this.store.findRecord('paperwork', params.paperwork_id, { include: 'paperwork_products' }),        
    },
});

The problem is that I can't access paperwork.paperwork_products in my template. It's undefined (other paperwork attributes are here, but not relationship). I already even tried to put a debugger in my mirage/config.js when routes are declared. My paperwork exists, and his "paperwork_products" too. But I can't get paperwork_products data in my template. What am I doing wrong ? I think I must change something in my :

this.get('v1/paperworks/:id');

But I don't know what ... Thanks in advance !

Edit: Here are my real Ember models:

models/paperwork.js

export default DS.Model.extend({
    customer: DS.belongsTo('customer'),
    paperwork_products: DS.hasMany('paperwork-product', { async: true }),
});

models/paperwork-product.js

export default DS.Model.extend({
  paperwork: DS.belongsTo('paperwork'),
});

Yesterday I tried to compare the real JsonApi response from my back, and Mirage response, and I saw that in the relationships hash, my relationship "paperwork_products" was changed to paperwork-products (with Mirage). So there is a problem with relationships with an underscore or models with dash ... In config.js, I tried to mock JSONAPI Backend, and it works wells. Just replaced "paperwork-products" by "paperwork_products"

Mirage response :

"relationships":{  
    "customer":{  
        "data":{  
            "type":"customers",
            "id":"1"
        }
    },
    "paperwork-products":{  
        "data":[  
            {  
                "type":"paperwork-products",
                "id":"1"
            }
        ]
    }
}

Should be :

"relationships":{  
    "customer":{  
        "data":{  
            "type":"customers",
            "id":"1"
        }
    },
    "paperwork_products":{  
        "data":[  
            {  
                "type":"paperwork_products",
                "id":"1"
            }
        ]
    }
}

My other models with hasMany relationships do not have any problems.

1

1 Answers

0
votes

To confirm, do you have Ember Data models setup with the same relationships? Without those, things may. It work very well ...

If you do, could you post those models as well?

Also, as an FYI, Mirage 0.3.0 comes with an auto-sync setup that will read your Ember Data models and create corresponding Mirage models without any work. It's been lovely ...

Edit: I would suggest you rework your Ember Data model to use camel cased relationships. If you do the following:

models/paperwork.js

export default DS.Model.extend({
    customer: DS.belongsTo('customer'),
    paperworkProducts: DS.hasMany('paperwork-product', { async: true }),
});

I would expect it to work without issue, as Ember Data automatically translates camelCased relationships to the appropriate JSON-API key

Does that work for you?