Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to make models in the new state #269

Closed
jakeonfire opened this issue Jan 17, 2017 · 11 comments
Closed

add ability to make models in the new state #269

jakeonfire opened this issue Jan 17, 2017 · 11 comments

Comments

@jakeonfire
Copy link

jakeonfire commented Jan 17, 2017

factory-guy cannot make models in the new state for use in component integration tests. i had to use the following code to set a model in a component which - when saved - would issue a create (POST) in ember-data:

  let store = this.container.lookup('service:store');
  let adapterAttrs = make('adapter').toJSON({ includeId: false });
  let adapter = run(() => store.createRecord('adapter', adapterAttrs));
  this.set('model', { adapter });
@danielspaniel
Copy link
Collaborator

ummmmmmm .. mmmmm .. hmmmm .. interesting.
Would you like FactoryGuy be able to do that for you in one swift operation?
Can you propose a way that would work?

I have not needed this ( does not mean its not a good idea ) .. so I am not sure right now what is best way.

you could change this:

  let adapterAttrs = build('adapter', id: null).get();
  let adapter = run(() => FactoryGuy.store.createRecord('adapter', adapterAttrs)); 
  this.set('model', adapter); // did you really mean to do { adapter } ?  seems odd 

I usually do this way because I am usually testing one or two attributes only and I don't mind if the rest are blank.

  let adapter = run(() => FactoryGuy.store.createRecord('adapter', {name: 'dude'})); 
  this.set('model', adapter); 

@jakeonfire
Copy link
Author

jakeonfire commented Jan 17, 2017

your suggestion is more succinct, thanks. but yes, was wondering if it's possible to do a make to create a model in the 'new' state with less code. we have several components which behave differently for new (creating) and existing (updating) records. as far as syntax - i'm not sure; just getting to know factory-guy. maybe:

let adapter = make('adapter', isNew: true);

...since that is the recommended attribute for differentiating in user code, and it's read-only in ember-data. i am totally unfamiliar with the level of difficulty for implementation, however. do you think its possible?

@danielspaniel
Copy link
Collaborator

danielspaniel commented Jan 17, 2017

This ( using make ) would be confusing though because "make" is supposed to make a 'loaded' model.
FactoryGuy has the "build" method to make a json payload.

So, maybe there needs to be another name for "make me a new record" like make createNew ? or makeNew ? .. then you could use the same traits and options as you would with make , build.

let newCan = makeNew('cat', 'calico', {name: 'Meow'})

@jakeonfire
Copy link
Author

oh, and this code:

let adapterAttrs = build('adapter', id: null).get();

produces hyphenated attributes (using JSONAPIAdapter with defaults) so won't work for handing to store.createRecord :o(

@danielspaniel
Copy link
Collaborator

danielspaniel commented Jan 23, 2017

Howdy partner, this new behaviour is enabled in:
https://github.com/danielspaniel/ember-data-factory-guy/releases/tag/v2.10.1

@jakeonfire
Copy link
Author

amazing - thanks!

@jakeonfire
Copy link
Author

jakeonfire commented Jan 30, 2017

@danielspaniel I'm having an issue with makeNew related to my last comment before you closed the issue. convertForBuild with JSONAPIAdapter creates hyphenated attributes which don't match up to the camelCased ones in the model, so any attributes that have more than one word in them in the fixture aren't applied. e.g. in the new method, applying some logging:

  makeNew() {
    let args = extractArguments.apply(this, arguments);
    ...
    let modelName = lookupModelForFixtureName(args.name);
    let fixture = this.buildRaw.apply(this, arguments);
    console.log("==> fixture:", fixture);
    let data = this.fixtureBuilder(modelName).convertForBuild(modelName, fixture);
    data = data.get();
    delete data.id;
    console.log("==> data:", data);

    const model = Ember.run(()=> this.store.createRecord(modelName, data));
    console.log("==> model:", model.toJSON());
    return model;
  }

yields:
==> fixture:

{
  id: 1,
  name: "Adapter 1",
  coverageMergerClassName: "CoverageMergers::Synchronize"
}

==> data:

{
  name: "Adapter 1",
  coverage-merger-class-name: "CoverageMergers::Synchronize"
}

==> model:

{
  name: "Adapter 1",
  coverageMergerClassName: null,
  ...
}

Is this particular to my ember-data adapter setup or an issue with factory-guy? (sorry for the confusing use of adapter as the model in my examples!)

@jakeonfire
Copy link
Author

per your original comment, the shortest i can write the code using make would be:

let adapterAttrs = make('adapter').toJSON({ includeId: false });
let adapter = run(() => FactoryGuy.store.createRecord('adapter', adapterAttrs));

@danielspaniel
Copy link
Collaborator

danielspaniel commented Jan 30, 2017

hmm .. let me write a test for this @jakeonfire .. might be that I have to use

  let data = this.fixtureBuilder(modelName).convertForMake(modelName, fixture);

instead of

  let data = this.fixtureBuilder(modelName).convertForBuild(modelName, fixture);

can you do me favour and swap that out and see what happens?

good catch in any case

@danielspaniel
Copy link
Collaborator

@jakeonfire .. this is now fixe on v2.11.4 .. happy testing!

@jakeonfire
Copy link
Author

perfect, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants