Skip to content

[Model Fragments 5.0] Composing fragments with FactoryGuy#make/store#push result in their properties being undefined  #366

@patocallaghan

Description

@patocallaghan

I'm opening this here but to be honest I'm not sure if the issue is in the Model Fragments RecordData refactor or the Ember Data upgrade (considering we need to upgrade to Ember Data 3.16).

Background

The Factory Guy addon gives you a set of APIs to bootstrap your models in tests. The make helper allows you to push records directly into the store rather than need to use the push, pushPayload or createRecord APIs to bootstrap your models. A common pattern when using make is to compose your test models with other models created using make. For example take the following person Ember Data model which contains a fragment:

// models/user.js
export default class User extends Model {
  @fragment('info') info
}

// models/info.js
export default class Info extends Fragment {
  @attr name
}

This can be created in a test by doing the following (once you've created your factories correctly). Here we create a user model and pass it an info fragment model.

let user = make('user', 
  info: make('info'),
});

The problem

It appears passing a fragment to a model like above causes the properties of info to be undefined rather than the values which were originally on the model.

console.log(info.name); //=> "Jon Snow" 
console.log(user.info.name); //=> undefined

Strangely, if you pass a model to another model, or a fragment to another fragment it works correctly. It specifically seems to be the case where you pass a fragment to a model which isn't working. I've pushed a draft PR here which has a failing test (and some passing ones) to illustrate the problem.

Potential bug

The problem seems specifically to be an issue all the way down in EmberObject when we are pushing the _internalModel property on the object to create the model instance. Looking at the screenshot below, the passed in props have the correct value, but once the model is created they are "lost" and come back as undefined.

var instance = this.class.create(props);

image

And just to clarify, the failing test in the PR passes on the commit before the RecordData refactor.

/cc @igorT @richgt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions