Skip to content

Commit

Permalink
More stable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gorandalum committed Nov 28, 2024
1 parent d42b05a commit 95da80c
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -36,3 +48,5 @@ describe('sanitizeRealtimeQuery', () => {
expect(sanitized).toBeUndefined();
});
});

const diff = (v1: number, v2: number) => Math.abs(v1 - v2);

0 comments on commit 95da80c

Please sign in to comment.