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

Support clone with relation (fix #381) #382

Merged
merged 8 commits into from
Dec 2, 2018
Merged
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
33 changes: 33 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1455,4 +1455,37 @@ describe('Parse Object', () => {
done();
}
});

it('can clone with relation', async (done) => {
const testObject = new TestObject();
const o = new TestObject();
await o.save();
await testObject.save();
let relation = o.relation('aRelation');
relation.add(testObject);
await o.save();

const o2 = o.clone();
assert.equal(
o.relation('aRelation').targetClassName,
o2.relation('aRelation').targetClassName
);
let relations = await o.relation('aRelation').query().find();
assert.equal(relations.length, 1);

relations = await o2.relation('aRelation').query().find();
assert.equal(relations.length, 0);

relation = o2.relation('aRelation');
relation.add(testObject);
await o2.save();

relations = await o.relation('aRelation').query().find();
assert.equal(relations.length, 1);

relations = await o2.relation('aRelation').query().find();
assert.equal(relations.length, 1);

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
2 changes: 2 additions & 0 deletions src/ParseOp.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ export class RelationOp extends Op {
return this;
} else if (previous instanceof UnsetOp) {
throw new Error('You cannot modify a relation after deleting it.');
} else if (previous instanceof SetOp && previous._value instanceof ParseRelation) {
return this;
} else if (previous instanceof RelationOp) {
if (previous._targetClassName &&
previous._targetClassName !== this._targetClassName) {
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
15 changes: 15 additions & 0 deletions src/__tests__/ParseOp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ mockObject.prototype._getId = function() {
mockObject.registerSubclass = function() {};
jest.setMock('../ParseObject', mockObject);

const mockRelation = function(parent, key) {
this.parent = parent;
this.key = key;
}
jest.setMock('../ParseRelation', mockRelation);

const ParseRelation = require('../ParseRelation');
const ParseObject = require('../ParseObject');
const ParseOp = require('../ParseOp');
const {
Expand Down Expand Up @@ -364,4 +371,12 @@ describe('ParseOp', () => {
]
});
});

it('can merge Relation Op with the previous Op', () => {
const r = new RelationOp();
const relation = new ParseRelation(null, null);
const set = new SetOp(relation);

expect(r.mergeWith(set)).toEqual(r);
});
});