diff --git a/src/service/impl/realtime/__tests__/sanitize-realtime-query.test.ts b/src/service/impl/realtime/__tests__/sanitize-realtime-query.test.ts index fcc297f..75d670e 100644 --- a/src/service/impl/realtime/__tests__/sanitize-realtime-query.test.ts +++ b/src/service/impl/realtime/__tests__/sanitize-realtime-query.test.ts @@ -12,22 +12,34 @@ describe('sanitizeRealtimeQuery', () => { it('When start time is now the sanitized start time should be unchanged and time range should be 30 minutes', () => { const startTime = new Date(); const sanitized = sanitizeRealtimeQuery(createQuery(startTime)); - expect(sanitized?.startTime.getTime()).toBeCloseTo(startTime.getTime(), 5); - expect(sanitized?.timeRange).toBeCloseTo(1800, 1); + + expect(sanitized?.timeRange).toBeDefined(); + expect( + diff(sanitized!.startTime.getTime(), startTime.getTime()), + ).toBeLessThan(5); + expect(diff(sanitized!.timeRange, 1800)).toBeLessThanOrEqual(1); }); it('When start time is in the past the sanitized start time should be now and time range should be 30 minutes', () => { const startTime = addHours(new Date(), -2); const sanitized = sanitizeRealtimeQuery(createQuery(startTime)); - expect(sanitized?.startTime.getTime()).toBeCloseTo(new Date().getTime(), 5); - expect(sanitized?.timeRange).toBeCloseTo(1800, 1); + + expect(sanitized?.timeRange).toBeDefined(); + expect( + diff(sanitized!.startTime.getTime(), new Date().getTime()), + ).toBeLessThan(5); + expect(diff(sanitized!.timeRange, 1800)).toBeLessThanOrEqual(1); }); it('When start time is 15 minutes in the future the sanitized start time should be unchanged and time range should be 15 minutes', () => { const startTime = addMinutes(new Date(), 15); const sanitized = sanitizeRealtimeQuery(createQuery(startTime)); - expect(sanitized?.startTime.getTime()).toBeCloseTo(startTime.getTime(), 5); - expect(sanitized?.timeRange).toBeCloseTo(900, 1); + + expect(sanitized?.timeRange).toBeDefined(); + expect( + diff(sanitized!.startTime.getTime(), startTime.getTime()), + ).toBeLessThan(5); + expect(diff(sanitized!.timeRange, 900)).toBeLessThanOrEqual(1); }); it('When start time is 45 minutes in the future the sanitized query should be undefined', () => { @@ -36,3 +48,5 @@ describe('sanitizeRealtimeQuery', () => { expect(sanitized).toBeUndefined(); }); }); + +const diff = (v1: number, v2: number) => Math.abs(v1 - v2);