Files
privacy.sexy/tests/unit/application/Common/Timing/PlatformTimer.spec.ts
undergroundwires e17744faf0 Bump to TypeScript 5.5 and enable noImplicitAny
This commit upgrades TypeScript from 5.4 to 5.5 and enables the
`noImplicitAny` option for stricter type checking. It refactors code to
comply with `noImplicitAny` and adapts to new TypeScript features and
limitations.

Key changes:

- Migrate from TypeScript 5.4 to 5.5
- Enable `noImplicitAny` for stricter type checking
- Refactor code to comply with new TypeScript features and limitations

Other supporting changes:

- Refactor progress bar handling for type safety
- Drop 'I' prefix from interfaces to align with new code convention
- Update TypeScript target from `ES2017` and `ES2018`.
  This allows named capturing groups. Otherwise, new TypeScript compiler
  does not compile the project and shows the following error:
  ```
  ...
  TimestampedFilenameGenerator.spec.ts:105:23 - error TS1503: Named capturing groups are only available when targeting 'ES2018' or later
  const pattern = /^(?<timestamp>\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-(?<scriptName>[^.]+?)(?:\.(?<extension>[^.]+))?$/;// timestamp-scriptName.extension
  ...
  ```
- Refactor usage of `electron-progressbar` for type safety and
  less complexity.
2024-09-26 16:07:37 +02:00

82 lines
2.5 KiB
TypeScript

import {
describe, it, expect, beforeEach,
afterEach,
} from 'vitest';
import { PlatformTimer } from '@/application/Common/Timing/PlatformTimer';
describe('PlatformTimer', () => {
let originalSetTimeout: typeof global.setTimeout;
let originalClearTimeout: typeof global.clearTimeout;
let originalDateNow: typeof global.Date.now;
beforeEach(() => {
originalSetTimeout = global.setTimeout;
originalClearTimeout = global.clearTimeout;
originalDateNow = Date.now;
Date.now = () => originalDateNow();
});
afterEach(() => {
global.setTimeout = originalSetTimeout;
global.clearTimeout = originalClearTimeout;
Date.now = originalDateNow;
});
describe('setTimeout', () => {
it('calls the global setTimeout with the provided delay', () => {
// arrange
type Delay = Parameters<typeof setTimeout>[1];
const expectedDelay = 55;
let actualDelay: Delay | undefined;
global.setTimeout = ((_: never, delay: Delay) => {
actualDelay = delay;
}) as typeof global.setTimeout;
// act
PlatformTimer.setTimeout(() => { /* NOOP */ }, expectedDelay);
// assert
expect(actualDelay).to.equal(expectedDelay);
});
it('calls the global setTimeout with the provided callback', () => {
// arrange
type Callback = Parameters<typeof setTimeout>[0];
const expectedCallback = () => { /* NOOP */ };
let actualCallback: Callback | undefined;
global.setTimeout = ((callback: Callback) => {
actualCallback = callback;
}) as typeof global.setTimeout;
// act
PlatformTimer.setTimeout(expectedCallback, 33);
// assert
expect(actualCallback).to.equal(expectedCallback);
});
});
describe('clearTimeout', () => {
it('should clear timeout', () => {
// arrange
type Timer = ReturnType<typeof PlatformTimer.setTimeout>;
let actualTimer: Timer | undefined;
global.clearTimeout = ((timer: Timer) => {
actualTimer = timer;
}) as typeof global.clearTimeout;
const expectedTimer = PlatformTimer.setTimeout(() => { /* NOOP */ }, 1);
// act
PlatformTimer.clearTimeout(expectedTimer);
// assert
expect(actualTimer).to.equal(expectedTimer);
});
});
describe('dateNow', () => {
it('should get current date', () => {
// arrange
const expected = Date.now();
Date.now = () => expected;
// act
const actual = PlatformTimer.dateNow();
// assert
expect(expected).to.equal(actual);
});
});
});