Files
directus/api/src/utils/stall.test.ts
Azri Kahar e899244ef3 Add unit tests to several API utility functions (#16662)
* add unit tests to several API util functions

* fix timezone tests to account for daylight saving

* add a note for future reference

* Update api/src/utils/get-date-formatted.test.ts

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>

* remove unnecessary note as it is not an issue

* fix getEnv mock in validate-env test

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
2022-12-23 16:51:49 +00:00

42 lines
940 B
TypeScript

import { afterAll, beforeAll, expect, SpyInstance, test, vi } from 'vitest';
import { stall } from './stall';
let performanceNowSpy: SpyInstance;
beforeAll(() => {
vi.useFakeTimers();
// fake timers doesn't fake performance.now(), so this is used to mock it
performanceNowSpy = vi.spyOn(performance, 'now').mockReturnValue(0);
});
afterAll(() => {
vi.useRealTimers();
});
const STALL_TIME = 100;
test('does not stall if elapsed time has already past the stall time', () => {
const startTime = performance.now();
// intentionally advance past the stall time first
performanceNowSpy.mockReturnValueOnce(1000);
stall(STALL_TIME, startTime);
expect(vi.getTimerCount()).toBe(0);
});
test('should stall for a set amount of time', () => {
const startTime = performance.now();
stall(STALL_TIME, startTime);
expect(vi.getTimerCount()).toBe(1);
vi.advanceTimersByTime(STALL_TIME);
expect(vi.getTimerCount()).toBe(0);
});