Skip to content

Add sending unsaved ParseObjects to Clients #1256

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

Open
wants to merge 4 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
61 changes: 61 additions & 0 deletions src/__tests__/encode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const mockObject = function(className) {
};
mockObject.registerSubclass = function() {};
mockObject.prototype = {
id : 'objId123',
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this?

Copy link
Author

Choose a reason for hiding this comment

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

when i remove this line id is undefined and the new introduced if (value.id === undefined) matches and the test encode ParseObjects fails because the test expects a pointer. But this is exactly the case I want to treat differently. When there is no id toPointer should not be called and the object should be send as full json.

  ● encode › encodes ParseObjects
    Expected: "POINTER"
    Received: {"__type": "Object", "className": "Item"}

Copy link
Member

Choose a reason for hiding this comment

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

Update the test by setting an id within the test. The MockObject should closely resemble a real ParseObject.

_getServerData() {
return this._serverData;
},
Expand All @@ -38,6 +39,8 @@ mockObject.prototype = {
__type: 'Object',
className: this.className
};
seen = seen || [];
offline = offline || false;
for (const attr in this.attributes) {
json[attr] = encode(this.attributes[attr], false, false, seen.concat(this), offline);
}
Expand Down Expand Up @@ -167,6 +170,64 @@ describe('encode', () => {
});
});

it('encodes unsaved ParseObject', () => {
const obj = new ParseObject('Item');
obj.id = undefined;
obj._serverData = {};
obj.attributes = {
str: 'string',
date: new Date(Date.UTC(2015, 1, 1))
};

expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z'
}
});

const subobj = new ParseObject('Subitem')
subobj.id = undefined;
subobj._serverData = {};
subobj.attributes = {
str: 'substring',
};

obj.attributes = {
item : subobj
};

expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
item: {
__type: 'Object',
className: 'Subitem',
str:'substring'
}
});

obj.attributes = {
items : [subobj, subobj]
};
expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
items: [{
__type: 'Object',
className: 'Subitem',
str:'substring'
},{
__type: 'Object',
className: 'Subitem',
str:'substring'
}]
});
});

it('does not encode ParseObjects when they are disallowed', () => {
const obj = new ParseObject('Item');
expect(encode.bind(null, obj, true)).toThrow(
Expand Down
3 changes: 3 additions & 0 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function encode(value: mixed, disallowObjects: boolean, forcePointers: boolean,
if (offline && value._getId().startsWith('local')) {
return value.toOfflinePointer();
}
if (value.id === undefined){
return value._toFullJSON();
}
return value.toPointer();
}
seen = seen.concat(seenEntry);
Expand Down