Skip to content

Instantly share code, notes, and snippets.

@cyril-sf
Created October 17, 2013 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyril-sf/7033227 to your computer and use it in GitHub Desktop.
Save cyril-sf/7033227 to your computer and use it in GitHub Desktop.
Test for saving async polymorphic associations
test("The store can save an async polymorphic belongsTo association", function() {
ajaxResponse({ my_post: { id: "1", name: "Awesome" } });
var post = store.push('myPost', { id: "1", name: "OMG" });
var comment = store.createRecord('myComment', { id: "2", name: "Awesome"});
comment.set('message', post);
comment.save().then(async(function(comment) {
ajaxParams = lastAjaxCall();
equal(ajaxParams.passedUrl, "/my_comments");
equal(ajaxParams.passedVerb, "POST");
deepEqual(ajaxParams.passedHash.data, { my_comment: { id: "2", name: "Awesome", message: {id: "1", type: "my_post"}, user_id: null } });
equal(comment.get('isDirty'), false, "the post isn't dirty anymore");
equal(comment.get('name'), "Awesome", "the post was updated");
}));
});
test("The store can save an async polymorphic hasMany association", function() {
ajaxResponse({
my_user: {
id: "1",
name: "Cedric Fluck",
messages: [{
type: "my_comment",
id: 2
}]
}
});
var comment = store.push('myComment', {id: 2, name: "OMG!!"});
var user = store.createRecord('myUser', {name: "Cyril Fluck"});
user.get('messages').pushObject( comment );
user.save().then(async(function(user) {
ajaxParams = lastAjaxCall();
equal(ajaxParams.passedUrl, "/my_users");
equal(ajaxParams.passedVerb, "POST");
deepEqual(ajaxParams.passedHash.data, { my_user: { name: "Cyril Fluck" }});
equal(user.get('isDirty'), false, "the user isn't dirty anymore");
equal(user.get('id'), "1", "the user was updated");
equal(user.get('messages.length'), 1, "The user still has one message");
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment