Skip to content

Commit

Permalink
Adds support for plain object in $add, $addUnique, $remove
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Mar 20, 2016
1 parent c39ba3b commit 2b58c54
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
53 changes: 48 additions & 5 deletions spec/ParseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,49 @@ describe('Parse.Object testing', () => {
});
});

it("addUnique with object", function(done) {
var x1 = new Parse.Object('X');
x1.set('stuff', [ 1, {'hello': 'world'}, {'foo': 'bar'}]);
x1.save().then(() => {
var objectId = x1.id;
var x2 = new Parse.Object('X', {objectId: objectId});
x2.addUnique('stuff', {'hello': 'world'});
x2.addUnique('stuff', {'bar': 'baz'});
expect(x2.get('stuff')).toEqual([{'hello': 'world'}, {'bar': 'baz'}]);
return x2.save();
}).then(() => {
var query = new Parse.Query('X');
return query.get(x1.id);
}).then((x3) => {
expect(x3.get('stuff')).toEqual([1, {'hello': 'world'}, {'foo': 'bar'}, {'bar': 'baz'}]);
done();
}, (error) => {
fail(error);
done();
});
});

it("removes with object", function(done) {
var x1 = new Parse.Object('X');
x1.set('stuff', [ 1, {'hello': 'world'}, {'foo': 'bar'}]);
x1.save().then(() => {
var objectId = x1.id;
var x2 = new Parse.Object('X', {objectId: objectId});
x2.remove('stuff', {'hello': 'world'});
expect(x2.get('stuff')).toEqual([]);
return x2.save();
}).then(() => {
var query = new Parse.Query('X');
return query.get(x1.id);
}).then((x3) => {
expect(x3.get('stuff')).toEqual([1, {'foo': 'bar'}]);
done();
}, (error) => {
fail(error);
done();
});
});

it("dirty attributes", function(done) {
var object = new Parse.Object("TestObject");
object.set("cat", "good");
Expand Down Expand Up @@ -1794,14 +1837,14 @@ describe('Parse.Object testing', () => {
done();
});
});

it('should have undefined includes when object is missing', (done) => {
let obj1 = new Parse.Object("AnObject");
let obj2 = new Parse.Object("AnObject");

Parse.Object.saveAll([obj1, obj2]).then(() => {
obj1.set("obj", obj2);
// Save the pointer, delete the pointee
// Save the pointer, delete the pointee
return obj1.save().then(() => { return obj2.destroy() });
}).then(() => {
let query = new Parse.Query("AnObject");
Expand All @@ -1823,15 +1866,15 @@ describe('Parse.Object testing', () => {
done();
})
});

it('should have undefined includes when object is missing on deeper path', (done) => {
let obj1 = new Parse.Object("AnObject");
let obj2 = new Parse.Object("AnObject");
let obj3 = new Parse.Object("AnObject");
Parse.Object.saveAll([obj1, obj2, obj3]).then(() => {
obj1.set("obj", obj2);
obj2.set("obj", obj3);
// Save the pointer, delete the pointee
// Save the pointer, delete the pointee
return Parse.Object.saveAll([obj1, obj2]).then(() => { return obj3.destroy() });
}).then(() => {
let query = new Parse.Query("AnObject");
Expand Down
3 changes: 3 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ function transformAtom(atom, force, options) {
if (FileCoder.isValidJSON(atom)) {
return (inArray || inObject ? atom : FileCoder.JSONToDatabase(atom));
}
if (inArray || inObject) {
return atom;
}

if (force) {
throw new Parse.Error(Parse.Error.INVALID_JSON,
Expand Down

0 comments on commit 2b58c54

Please sign in to comment.