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

handleCreate does not mock response with relationships #141

Closed
FilmKnurd opened this issue Oct 19, 2015 · 15 comments
Closed

handleCreate does not mock response with relationships #141

FilmKnurd opened this issue Oct 19, 2015 · 15 comments

Comments

@FilmKnurd
Copy link

Am I correct in thinking that handleCreate should mock the response with values for every attribute specified on the created record? I am not seeing relationship properties on the payload and, consequently, I cannot get those properties off the mocked model.

For example, after creating a model, I transition into a route that loads a related model. In the test, however, the mocked model does not have the relationship and so Ember Data complains that I am passing undefined into store.find().

App Code (some route)

createNewChild: function (parent) {
    this.store.createRecord('campaign', {
      name: 'Foo',
      website: parent.get('website')
    });
}

App Code (edit route)

afterModel: function(campaign) {
    ...
    return this.store.find('website', campaign.get('website.id'));
}

Factory

FactoryGuy.define('campaign', {
    sequences: {
        name: function(num) {
            return 'Campaign ' + num;
        }
    },

    default: {
        name: FactoryGuy.generate('name'),
        website: FactoryGuy.belongsTo('website'),
    }
}

Test

test('can make new variations', function(assert) {
    var campaign = make('campaign', 'with_children');

    visit(`/campaigns/${campaign.id}/edit`);

    FactoryHelper.handleCreate('campaign');

    click('.nav-bar_left a:last');  // Triggers createNew action

    andThen(function() {
        // assertions
    });
});

I have tried adding .andReturn({ website: [website] }) Where website was both the website from the campaign, or the website id. But neither had any affect. Name and any non relationship property comes through just fine. It's only related models with which I am having trouble.

Am I using handleCreate correctly?

@danielspaniel
Copy link
Collaborator

hmm .. @FilmKnurd .. give this a try

test('can make new variations', function(assert) {
    var website = make('website');
    var campaign = make('campaign', 'with_children', {website: website});

    visit(`/campaigns/${campaign.id}/edit`);

    FactoryHelper.handleCreate('campaign');

    click('.nav-bar_left a:last');  // Triggers createNew action

    andThen(function() {
        // assertions
    });
});

@danielspaniel
Copy link
Collaborator

I actually remove any belongsTo associations in handleCreate Response .. because there were problems ( forgot what they were ) .. and it was redundant because if create a model with a belongsTo that exists in the store .. it will just 'be there' when the payload returns and makes the model for real
so let me know if what I gave you does not work.

@FilmKnurd
Copy link
Author

Thank you for your help. Unfortunately, it didn't work. The campaign that is made explicitly with make is fine. It has a website associated with it. It's the campaign that is created by handleCreate that doesn't have the website, even though in my app code I save it with a website: this.store.createRecord('campaign', { website: website }).save()

@danielspaniel
Copy link
Collaborator

can you do this

console.log('website', website+'')
this.store.createRecord('campaign', { website: website }).save()

in your app code and then run the test.
my guess is that the website is null, ( if not let me know )

@FilmKnurd
Copy link
Author

website <DS.PromiseObject:ember675>

@danielspaniel
Copy link
Collaborator

that is interesting .. can you how me the model def's .. are they async: true ?
that should not be a promise object .. should be model if you created it already .. no?

also .. you said before that this was the code:

createNewChild: function (parent) {
    this.store.createRecord('campaign', {
      name: 'Foo',
      website: parent.get('website')
    });
}

that makes the campaign .. so now I am thoroughly confused .. have no idea what is where ..
might have to send me a gist when all the code lined up .. because I don't see the big picture here

@FilmKnurd
Copy link
Author

Sorry, I was trying to pair the code down to the essentials, because the model has bunches of other properties on it.

Basically, we are dealing with two campaign models. The first is a parent and the second is a child. The parent already exists and is the parameter that is passed into createNewChild. It's also the model that is made in the test (var campaign = make('campaign', 'with_children', {website: website});).

The createNewChild action takes the parent and creates a new campaign model using the website that is on the parent.

Website is declared with async: true

Is that helpful?

@danielspaniel
Copy link
Collaborator

kind of .. what would really make me see the light and allow me to fix/analyze quickly is if you cloned the repo and made a failing test that showed this setup .. then it's awholelottta better to see and muchmore painlesser to fix

@FilmKnurd
Copy link
Author

Hmmmm... I'm having trouble making a red test in your repo :-) There must be something fishy going on in my code.

@danielspaniel
Copy link
Collaborator

Yeah .. I think there is too .. just something a teeny bit different somewhere .. I think that the fact that
website DS.PromiseObject:ember675 happened was a red flag for me .. something just not quite right or something I don't understand about what your doing ( which is hard with just snippets ) .. I would have to have your code to play around with. Maybe if you don't figure it out, I can look at it on screenhero this weekend.

@FilmKnurd
Copy link
Author

That is super generous of you. I'm going to tinker with it some more tomorrow and I'll let you know. It's not a huge blocker for me at the moment.

@danielspaniel
Copy link
Collaborator

Have you made more progress. I am just way behind on my other work, so I just worked on that all day. Which is a bummer, because i am curious to fix this one.

@FilmKnurd
Copy link
Author

I have not had a chance to take another stab at it yet. I was planning on not bothering you until I've taken a second look :-)

@danielspaniel
Copy link
Collaborator

I appreciate that .. cause I am maniacly busy

@danielspaniel
Copy link
Collaborator

I think v2.6.2 fixed this issue .. if not let me know .. and I will reopen this. but I am confident :)

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