Skip to content

Commit a4e6152

Browse files
committed
chore(LDS): Improve $relativeTime tests
1 parent 58bd796 commit a4e6152

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

src/__tests__/OfflineQuery-test.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,35 @@ 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.greaterThan('ttl', { $relativeTime: 'now' });
85+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
86+
87+
q = new ParseQuery('Item');
88+
q.greaterThan('ttl', { $relativeTime: 'now' });
89+
q.lessThan('ttl', { $relativeTime: 'in 3 day' });
90+
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
91+
92+
q = new ParseQuery('Item');
93+
q.greaterThan('ttl', { $relativeTime: '1 year 3 weeks 1 hour 3 minutes 2 seconds ago' });
7094
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
7195
});
7296

@@ -79,6 +103,37 @@ describe('OfflineQuery', () => {
79103
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
80104
});
81105

106+
it('handles invalid queries relativeTime date errors', () => {
107+
const obj = new ParseObject('Item');
108+
obj.set('field', new Date('1995-12-17T03:24:00'));
109+
110+
const q = new ParseQuery('Item');
111+
q.greaterThan('field', { $relativeTime: '1 unknown ago' });
112+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
113+
"bad $relativeTime (field) value. Invalid interval: 'unknown'"
114+
);
115+
116+
q.greaterThan('field', { $relativeTime: 'in 1 ago' });
117+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
118+
"bad $relativeTime (field) value. Time cannot have both 'in' and 'ago'"
119+
);
120+
121+
q.greaterThan('field', { $relativeTime: 'ago 1 in' });
122+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
123+
"bad $relativeTime (field) value. Time should either start with 'in' or end with 'ago'"
124+
);
125+
126+
q.greaterThan('field', { $relativeTime: '1 ago' });
127+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
128+
'bad $relativeTime (field) value. Invalid time string. Dangling unit or number.'
129+
);
130+
131+
q.greaterThan('field', { $relativeTime: 'in N/A days' });
132+
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
133+
"bad $relativeTime (field) value. 'n/a' is not an integer."
134+
);
135+
});
136+
82137
it('matches queries relation', () => {
83138
const obj = new ParseObject('Item');
84139
const relation = obj.relation('author');

0 commit comments

Comments
 (0)