Skip to content

Allow fromJSON to set keys to dirty #1295

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

Merged
merged 12 commits into from
Feb 11, 2021
29 changes: 29 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2012,4 +2012,33 @@ describe('Parse Object', () => {
const fetched = await query.get(user.id);
assert.equal(fetched.isDataAvailable(), true);
});

it('from json save data', async () => {
const json = {
className: 'TestObject',
date: new Date(),
array: [],
object: {},
string: '',
};
const obj = Parse.Object.fromJSON(json, false, true);
expect(obj.get('date')).toBeDefined();
expect(obj.get('date')).toBeInstanceOf(Date);
expect(obj.get('array')).toBeDefined();
expect(obj.get('array')).toBeInstanceOf(Array);
expect(obj.get('object')).toBeDefined();
expect(obj.get('object')).toBeInstanceOf(Object);
expect(obj.get('string')).toBeDefined();
expect(obj.get('string')).toBeInstanceOf(String);
await obj.save();
await obj.fetch();
expect(obj.get('date')).toBeDefined();
expect(obj.get('date')).toBeInstanceOf(Date);
expect(obj.get('array')).toBeDefined();
expect(obj.get('array')).toBeInstanceOf(Array);
expect(obj.get('object')).toBeDefined();
expect(obj.get('object')).toBeInstanceOf(Object);
expect(obj.get('string')).toBeDefined();
expect(obj.get('string')).toBeInstanceOf(String);
});
});
6 changes: 5 additions & 1 deletion src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -1823,10 +1823,11 @@ class ParseObject {
* @param {object} json The JSON map of the Object's data
* @param {boolean} override In single instance mode, all old server data
* is overwritten if this is set to true
* @param {boolean} dirty Whether the Parse.Object should set JSON keys to dirty
* @static
* @returns {Parse.Object} A Parse.Object reference
*/
static fromJSON(json: any, override?: boolean) {
static fromJSON(json: any, override?: boolean, dirty?: boolean) {
if (!json.className) {
throw new Error('Cannot create an object without a className');
}
Expand All @@ -1836,6 +1837,9 @@ class ParseObject {
for (const attr in json) {
if (attr !== 'className' && attr !== '__type') {
otherAttributes[attr] = json[attr];
if (dirty) {
o.set(attr, json[attr]);
}
}
}
if (override) {
Expand Down
26 changes: 24 additions & 2 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ describe('ParseObject', () => {
createdAt: '2013-12-14T04:51:19Z',
objectId: 'I1',
size: 'medium',
date: date
date: date,
};
const o = ParseObject.fromJSON(json);
expect(o.className).toBe('Item');
Expand All @@ -271,12 +271,34 @@ describe('ParseObject', () => {
size: 'medium',
createdAt: new Date(Date.UTC(2013, 11, 14, 4, 51, 19)),
updatedAt: new Date(Date.UTC(2013, 11, 14, 4, 51, 19)),
date
date,
});
expect(o.dirty()).toBe(false);
expect(o.get('date')).toBeInstanceOf(Date);
});

it('can be dirty with fromJSON', () => {
const date = new Date();
const json = {
className: 'Item',
createdAt: '2013-12-14T04:51:19Z',
objectId: 'I1',
size: 'medium',
date: date,
};
const o = ParseObject.fromJSON(json, false, true);
expect(o.className).toBe('Item');
expect(o.id).toBe('I1');
expect(o.attributes).toEqual({
size: 'medium',
createdAt: new Date(Date.UTC(2013, 11, 14, 4, 51, 19)),
updatedAt: new Date(Date.UTC(2013, 11, 14, 4, 51, 19)),
date,
});
expect(o.dirty()).toBe(true);
expect(o.dirtyKeys()).toEqual(['size', 'date']);
});

it('can override old data when inflating from the server', () => {
const o = ParseObject.fromJSON({
className: 'Item',
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3260,7 +3260,12 @@ describe('ParseQuery LocalDatastore', () => {
updatedAt: new Date('2018-08-12T00:00:00.000Z'),
};

mockLocalDatastore._serializeObjectsFromPinName.mockImplementation(() => [obj1, obj3, obj2, obj4]);
mockLocalDatastore._serializeObjectsFromPinName.mockImplementation(() => [
obj1,
obj3,
obj2,
obj4,
]);

mockLocalDatastore.checkIfEnabled.mockImplementation(() => true);

Expand Down