From bad491063da1363db3cdf61b31b9b0f4f9290d18 Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Wed, 31 Jul 2024 16:58:02 +0200 Subject: [PATCH] fix: getShouldClockUpdate test (#1165) * useFakeTimers * now can never be negative --- .../__tests__/rundownService.utils.test.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/apps/server/src/services/runtime-service/__tests__/rundownService.utils.test.ts b/apps/server/src/services/runtime-service/__tests__/rundownService.utils.test.ts index a6aa79c31..2a8328b8f 100644 --- a/apps/server/src/services/runtime-service/__tests__/rundownService.utils.test.ts +++ b/apps/server/src/services/runtime-service/__tests__/rundownService.utils.test.ts @@ -1,17 +1,26 @@ import { MILLIS_PER_MINUTE } from 'ontime-utils'; import { getShouldClockUpdate, getShouldTimerUpdate } from '../rundownService.utils.js'; +beforeEach(() => { + vi.useFakeTimers(); + vi.setSystemTime(0); +}); + +afterEach(() => { + vi.useRealTimers(); +}); + describe('getShouldClockUpdate()', () => { it('should return true when we slid forwards', () => { - const previousUpdate = Date.now() - 2000; // 2 seconds ago - const now = Date.now(); + const previousUpdate = Date.now(); // 2 seconds ago + const now = Date.now() + 2000; const result = getShouldClockUpdate(previousUpdate, now); expect(result).toBe(true); }); it('should return true when we slid backwards', () => { - const previousUpdate = Date.now(); - const now = Date.now() - 2000; // 2 seconds ago + const previousUpdate = Date.now() + 2000; + const now = Date.now(); // 2 seconds ago const result = getShouldClockUpdate(previousUpdate, now); expect(result).toBe(true); }); @@ -24,8 +33,8 @@ describe('getShouldClockUpdate()', () => { }); it('should return false when clock is not a second ahead and force update is not required', () => { - const previousUpdate = Date.now() - 32; - const now = Date.now(); + const previousUpdate = Date.now(); + const now = Date.now() + 32; const result = getShouldClockUpdate(previousUpdate, now); expect(result).toBe(false); });