mirror of
https://github.com/directus/directus.git
synced 2026-02-10 12:14:58 -05:00
* 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>
42 lines
940 B
TypeScript
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);
|
|
});
|