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 tests for addition and removal from a hasMany relationship #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 48 additions & 35 deletions lib/ember-parse-adapter/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,52 +165,65 @@ EmberParseAdapter.Serializer = DS.RESTSerializer.extend({
serializeHasMany: function(record, json, relationship){
var key = relationship.key;
var hasMany = record.get(key);
var options = relationship.options;
if(hasMany && hasMany.get('length') > 0){

json[key] = { "objects": [] };

if(options.relation){
json[key].__op = "AddRelation";
}
// No objects, just return a blank array.
if(!hasMany || hasMany.get("length") === 0){
json[key] = [];
return;
}

if(options.array){
json[key].__op = "AddUnique";
}
var options = relationship.options;
var parseClassName = this.parseClassName(relationship.type.typeKey);
var addOperation, removeOperation;

if (hasMany && hasMany.get("length")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense to check hasMany or its length as you have already checked on line 170.

addOperation = {
__op: (options.array ? "AddUnique" : "AddRelation"),
objects: []
};
hasMany.forEach(function(child){
json[key].objects.push({
addOperation.objects.push({
"__type": "Pointer",
"className": child.parseClassName(),
"objectId": child.get('id')
"className": parseClassName,
"objectId": child.get("id")
});
});
}

if(hasMany._deletedItems && hasMany._deletedItems.length){
if(options.relation){
var addOperation = json[key];
var deleteOperation = { "__op": "RemoveRelation", "objects": [] };
hasMany._deletedItems.forEach(function(item){
deleteOperation.objects.push({
"__type": "Pointer",
"className": item.type,
"objectId": item.id
});
});
json[key] = { "__op": "Batch", "ops": [addOperation, deleteOperation] };
}
if(options.array){
json[key].deleteds = { "__op": "Remove", "objects": [] };
hasMany._deletedItems.forEach(function(item){
json[key].deleteds.objects.push({
"__type": "Pointer",
"className": item.type,
"objectId": item.id
});
});
if(hasMany && hasMany.get("_deletedItems.length")) {
removeOperation = {
__op: (options.array ? "Remove" : "RemoveRelation"),
objects: []
};
hasMany._deletedItems.forEach(function(item){
removeOperation.objects.push({
"__type": "Pointer",
"className": parseClassName,
"objectId": item.get("id")
});
});
}

if (options.array) {
if (addOperation) {
json[key] = addOperation;
}
if (removeOperation) {
if (json[key]) {
json[key].deleteds = removeOperation;
} else {
json[key] = {deleteds: removeOperation};
}
}
} else {
if (addOperation && removeOperation) {
json[key] = { "__op": "Batch", "ops": [addOperation, removeOperation] };
} else if (addOperation || removeOperation) {
json[key] = addOperation || removeOperation;
}
}

if (!json[key]) {
json[key] = [];
}
}
Expand Down
48 changes: 39 additions & 9 deletions test/ember-parse-adapter/serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,48 @@ test("many posts are extracted", function(){
equal(res[1].title, 'Test B', 'Title should be put on post namespace');
});

pending("hasMany for serialization (Parse Pointer)", function(){
test("hasMany addition for serialization (Parse Pointer)", function(){
var serialized,
hash = {},
relationship = { options: { embedded: false }},
post,
comment;
store.load(Post, "1", {title: 'Testing hasMany serialization.'});
store.load(Comment, "1", {content: 'Comment 1'});
post = store.find(Post, "1");
comment = store.find(Comment, "1");
post.get('comments').pushObject(comment);
serializer.addHasMany(hash, post, "comments", relationship);
equal(hash.comments[0]["__type"], "Pointer", "Should be a Pointer __type/class.");
equal(hash.comments[0]["className"], "Comment", "Should be Comment class.");
Ember.run(function(){
store.push('post', {id: '1', title: 'Testing hasMany serialization.'});
store.push('comment', {id: "1", content: 'Comment 1'});
});
post = store.getById('post', "1");
comment = store.getById('comment', "1");

Ember.run(function(){
post.get('comments').pushObject(comment);
});

var hash = store.serializerFor('post').serialize(post);
equal(hash.comments.__op, 'AddRelation', 'Should be a an AddRelation op');
equal(hash.comments.objects[0].__type, 'Pointer', 'Should be a Pointer __type/class');
equal(hash.comments.objects[0].className, 'Comment', 'Should be Comment class');
});

test("hasMany removal for serialization (Parse Pointer)", function(){
var serialized,
hash = {},
relationship = { options: { embedded: false }},
post,
comment;
Ember.run(function(){
store.push('comment', {id: "1", content: 'Comment 1'});
store.push('post', {id: '1', title: 'Testing hasMany serialization.', comments: ['1']});
});
post = store.getById('post', '1');
comment = store.getById('comment', '1');

Ember.run(function(){
post.get('comments').removeObject(comment);
});

var hash = store.serializerFor('post').serialize(post);
equal(hash.comments.__op, 'RemoveRelation', 'Should be a RemoveRelation op');
equal(hash.comments.objects[0].__type, 'Pointer', 'Should be a Pointer __type/class');
equal(hash.comments.objects[0].className, 'Comment', 'Should be Comment class');
});