Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 14 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1455,4 +1455,18 @@ describe('Parse Object', () => {
done();
}
});

it('can clone with relation', (done) => {
const o = Parse.Object.fromJSON({
objectId: '7777777777',
className: 'Foo',
aRelation: { __type: 'Relation', className: 'Foo' },
});
const o2 = o.clone();
assert.equal(
o.relation('aRelation').targetClassName,
o2.relation('aRelation').targetClassName
);
done();
});
});
4 changes: 4 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ class ParseObject {
!(changes[k] instanceof ParseACL)
) {
newOps[k] = new SetOp(new ParseACL(changes[k]));
} else if (changes[k] instanceof ParseRelation) {
var relation = new ParseRelation(this, k);
relation.targetClassName = changes[k].targetClassName;
newOps[k] = new SetOp(relation);
} else {
newOps[k] = new SetOp(changes[k]);
}
Expand Down
51 changes: 49 additions & 2 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseObject');
jest.dontMock('../ParseOp');
jest.dontMock('../ParseRelation');
jest.dontMock('../RESTController');
jest.dontMock('../SingleInstanceStateController');
jest.dontMock('../TaskQueue');
Expand All @@ -35,13 +36,48 @@ jest.dontMock('./test_helpers/mockXHR');
jest.useFakeTimers();

const mockRelation = function(parent, key) {
this.parentClass = parent.className;
this.parentId = parent.id;
// The parent and key fields will be populated by the parent
if (parent) {
this.parentClass = parent.className;
this.parentId = parent.id;
}
this.key = key;
};
mockRelation.prototype.add = function(obj) {
this.targetClassName = obj.className;
};
mockRelation.prototype.toJSON = function() {
return {
__type: 'Relation',
className: this.targetClassName
};
};
mockRelation.prototype._ensureParentAndKey = function(parent, key) {
this.key = this.key || key;
if (this.key !== key) {
throw new Error(
'Internal Error. Relation retrieved from two different keys.'
);
}
if (this.parent) {
if (this.parent.className !== parent.className) {
throw new Error(
'Internal Error. Relation retrieved from two different Objects.'
);
}
if (this.parent.id) {
if (this.parent.id !== parent.id) {
throw new Error(
'Internal Error. Relation retrieved from two different Objects.'
);
}
} else if (parent.id) {
this.parent = parent;
}
} else {
this.parent = parent;
}
};
jest.setMock('../ParseRelation', mockRelation);

const mockQuery = function(className) {
Expand Down Expand Up @@ -514,6 +550,17 @@ describe('ParseObject', () => {
expect(rel.targetClassName).toBe('Person');
});

it('can be cloned with relation (#381)', () => {
const relationJSON = {__type: 'Relation', className: 'Bar'};
const o = ParseObject.fromJSON({
objectId: '7777777777',
className: 'Foo',
aRelation: relationJSON,
});
const o2 = o.clone();
expect(o2._getSaveJSON().aRelation).toEqual(relationJSON);
});

it('can detect dirty object children', () => {
const o = new ParseObject('Person');
o._finishFetch({
Expand Down