Skip to content

fromJSON: Return date if value is a date #1293

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 6 commits into from
Feb 11, 2021
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
4 changes: 4 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,13 @@ describe('ParseObject', () => {
});

it('can be inflated from server JSON', () => {
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);
expect(o.className).toBe('Item');
Expand All @@ -268,8 +270,10 @@ 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
});
expect(o.dirty()).toBe(false);
expect(o.get('date')).toBeInstanceOf(Date);
});

it('can override old data when inflating from the server', () => {
Expand Down
29 changes: 16 additions & 13 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3251,7 +3251,16 @@ describe('ParseQuery LocalDatastore', () => {
updatedAt: new Date('2018-08-12T00:00:00.000Z'),
};

mockLocalDatastore._serializeObjectsFromPinName.mockImplementation(() => [obj1, obj2, obj3]);
const obj4 = {
className: 'Item',
objectId: 'objectId4',
password: 123,
number: 4,
createdAt: new Date('2018-08-12T00:00:00.000Z'),
updatedAt: new Date('2018-08-12T00:00:00.000Z'),
};

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

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

Expand All @@ -3262,33 +3271,27 @@ describe('ParseQuery LocalDatastore', () => {
expect(results[0].get('number')).toEqual(2);
expect(results[1].get('number')).toEqual(3);
expect(results[2].get('number')).toEqual(4);
expect(results[3].get('number')).toEqual(4);

q = new ParseQuery('Item');
q.descending('number');
q.fromLocalDatastore();
results = await q.find();
expect(results[0].get('number')).toEqual(4);
expect(results[1].get('number')).toEqual(3);
expect(results[2].get('number')).toEqual(2);

q = new ParseQuery('Item');
q.descending('number');
q.fromLocalDatastore();
results = await q.find();
expect(results[0].get('number')).toEqual(4);
expect(results[1].get('number')).toEqual(3);
expect(results[2].get('number')).toEqual(2);
expect(results[1].get('number')).toEqual(4);
expect(results[2].get('number')).toEqual(3);
expect(results[3].get('number')).toEqual(2);

q = new ParseQuery('Item');
q.descending('_created_at');
q.ascending('_created_at');
q.fromLocalDatastore();
results = await q.find();
expect(results[0].get('number')).toEqual(2);
expect(results[1].get('number')).toEqual(3);
expect(results[2].get('number')).toEqual(4);

q = new ParseQuery('Item');
q.descending('_updated_at');
q.ascending('_updated_at');
q.fromLocalDatastore();
results = await q.find();
expect(results[0].get('number')).toEqual(2);
Expand Down
2 changes: 1 addition & 1 deletion src/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { opFromJSON } from './ParseOp';
import ParseRelation from './ParseRelation';

export default function decode(value: any): any {
if (value === null || typeof value !== 'object') {
if (value === null || typeof value !== 'object' || value instanceof Date) {
return value;
}
if (Array.isArray(value)) {
Expand Down