Skip to content

Commit 6578f7c

Browse files
authored
chore(LDS): Improve $relativeTime tests (#1410)
* chore(LDS): Improve $relativeTime tests * test shorthand notation
1 parent 58bd796 commit 6578f7c

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

src/__tests__/OfflineQuery-test.js

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,51 @@ describe('OfflineQuery', () => {
6262
});
6363

6464
it('matches queries relativeTime date field', () => {
65-
const date = new Date('1995-12-17T03:24:00');
66-
const obj = new ParseObject('Item');
67-
obj.set('field', date);
68-
const q = new ParseQuery('Item');
69-
q.lessThanOrEqualTo('field', { $relativeTime: 'in 0 day' });
65+
const now = Date.now();
66+
const obj = new ParseObject('Item', {
67+
name: 'obj1',
68+
ttl: new Date(now + 2 * 24 * 60 * 60 * 1000), // 2 days from now
69+
});
70+
71+
let q = new ParseQuery('Item');
72+
q.greaterThan('ttl', { $relativeTime: 'in 1 day' });
73+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
74+
75+
q = new ParseQuery('Item');
76+
q.greaterThan('ttl', { $relativeTime: '1 day ago' });
77+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
78+
79+
q = new ParseQuery('Item');
80+
q.lessThan('ttl', { $relativeTime: '5 days ago' });
81+
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
82+
83+
q = new ParseQuery('Item');
84+
q.lessThan('ttl', { $relativeTime: '0 yr 0 wk 5 d 0 hr 0 min 0 sec ago' });
85+
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
86+
87+
q = new ParseQuery('Item');
88+
q.greaterThan('ttl', { $relativeTime: 'now' });
89+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
90+
91+
q = new ParseQuery('Item');
92+
q.greaterThan('ttl', { $relativeTime: 'now' });
93+
q.lessThan('ttl', { $relativeTime: 'in 3 day' });
94+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
95+
96+
q = new ParseQuery('Item');
97+
q.greaterThan('ttl', { $relativeTime: '1 years 3 weeks 1 hours 3 minutes 2 seconds ago' });
98+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
99+
100+
q = new ParseQuery('Item');
101+
q.greaterThan('ttl', { $relativeTime: '2 year 2 week 1 hour 4 day 3 minute 2 second ago' });
102+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
103+
104+
q = new ParseQuery('Item');
105+
q.greaterThan('ttl', { $relativeTime: '2 yrs 2 wks 1 hrs 3 mins 2 secs ago' });
106+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
107+
108+
q = new ParseQuery('Item');
109+
q.greaterThan('ttl', { $relativeTime: '0 yr 0 wk 0 d 24 hr 3 min 2 sec ago' });
70110
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
71111
});
72112

@@ -79,6 +119,37 @@ describe('OfflineQuery', () => {
79119
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
80120
});
81121

122+
it('handles invalid queries relativeTime date errors', () => {
123+
const obj = new ParseObject('Item');
124+
obj.set('field', new Date('1995-12-17T03:24:00'));
125+
126+
const q = new ParseQuery('Item');
127+
q.greaterThan('field', { $relativeTime: '1 unknown ago' });
128+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
129+
"bad $relativeTime (field) value. Invalid interval: 'unknown'"
130+
);
131+
132+
q.greaterThan('field', { $relativeTime: 'in 1 ago' });
133+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
134+
"bad $relativeTime (field) value. Time cannot have both 'in' and 'ago'"
135+
);
136+
137+
q.greaterThan('field', { $relativeTime: 'ago 1 in' });
138+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
139+
"bad $relativeTime (field) value. Time should either start with 'in' or end with 'ago'"
140+
);
141+
142+
q.greaterThan('field', { $relativeTime: '1 ago' });
143+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
144+
'bad $relativeTime (field) value. Invalid time string. Dangling unit or number.'
145+
);
146+
147+
q.greaterThan('field', { $relativeTime: 'in N/A days' });
148+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
149+
"bad $relativeTime (field) value. 'n/a' is not an integer."
150+
);
151+
});
152+
82153
it('matches queries relation', () => {
83154
const obj = new ParseObject('Item');
84155
const relation = obj.relation('author');

0 commit comments

Comments
 (0)