Skip to content

Commit fa97df5

Browse files
ananfangdplewis
authored andcommitted
Decode Date JSON value at LiveQuery (#5540)
1 parent 2e2ee06 commit fa97df5

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

spec/QueryTools.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,49 @@ describe('matchesQuery', function() {
587587
q.notContainedIn('profile', ['abc', 'def', 'ghi']);
588588
expect(matchesQuery(message, q)).toBe(false);
589589
});
590+
591+
it('matches on Date', () => {
592+
// given
593+
const now = new Date();
594+
const obj = {
595+
id: new Id('Person', '01'),
596+
dateObject: now,
597+
dateJSON: {
598+
__type: 'Date',
599+
iso: now.toISOString(),
600+
},
601+
};
602+
603+
// when, then: Equal
604+
let q = new Parse.Query('Person');
605+
q.equalTo('dateObject', now);
606+
q.equalTo('dateJSON', now);
607+
expect(matchesQuery(Object.assign({}, obj), q)).toBe(true);
608+
609+
// when, then: lessThan
610+
const future = Date(now.getTime() + 1000);
611+
q = new Parse.Query('Person');
612+
q.lessThan('dateObject', future);
613+
q.lessThan('dateJSON', future);
614+
expect(matchesQuery(Object.assign({}, obj), q)).toBe(true);
615+
616+
// when, then: lessThanOrEqualTo
617+
q = new Parse.Query('Person');
618+
q.lessThanOrEqualTo('dateObject', now);
619+
q.lessThanOrEqualTo('dateJSON', now);
620+
expect(matchesQuery(Object.assign({}, obj), q)).toBe(true);
621+
622+
// when, then: greaterThan
623+
const past = Date(now.getTime() - 1000);
624+
q = new Parse.Query('Person');
625+
q.greaterThan('dateObject', past);
626+
q.greaterThan('dateJSON', past);
627+
expect(matchesQuery(Object.assign({}, obj), q)).toBe(true);
628+
629+
// when, then: greaterThanOrEqualTo
630+
q = new Parse.Query('Person');
631+
q.greaterThanOrEqualTo('dateObject', now);
632+
q.greaterThanOrEqualTo('dateJSON', now);
633+
expect(matchesQuery(Object.assign({}, obj), q)).toBe(true);
634+
});
590635
});

src/LiveQuery/QueryTools.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ function matchesKeyConstraints(object, key, constraints) {
177177
// Bail! We can't handle relational queries locally
178178
return false;
179179
}
180+
// Decode Date JSON value
181+
if (object[key] && object[key].__type == 'Date') {
182+
object[key] = new Date(object[key].iso);
183+
}
180184
// Equality (or Array contains) cases
181185
if (typeof constraints !== 'object') {
182186
if (Array.isArray(object[key])) {

0 commit comments

Comments
 (0)