Files
privacy.sexy/tests/unit/presentation/bootstrapping/ClientLoggerFactory.spec.ts
undergroundwires 08dbfead7c Centralize log file and refactor desktop logging
- Migrate to `electron-log` v5.X.X, centralizing log files to adhere to
  best-practices.
- Add critical event logging in the log file.
- Replace `ElectronLog` type with `LogFunctions` for better abstraction.
- Unify log handling in `desktop-runtime-error` by removing
  `renderer.log` due to `electron-log` v5 changes.
- Update and extend logger interfaces, removing 'I' prefix and adding
  common log levels to abstract `electron-log` completely.
- Move logger interfaces to the application layer as it's cross-cutting
  concern, meanwhile keeping the implementations in the infrastructure
  layer.
- Introduce `useLogger` hook for easier logging in Vue components.
- Simplify `WindowVariables` by removing nullable properties.
- Improve documentation to clearly differentiate between desktop and web
  versions, outlining specific features of each.
2023-12-02 11:50:25 +01:00

81 lines
2.8 KiB
TypeScript

import {
describe, it, beforeEach, afterEach,
} from 'vitest';
import { IRuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/IRuntimeEnvironment';
import { ClientLoggerFactory } from '@/presentation/bootstrapping/ClientLoggerFactory';
import { Logger } from '@/application/Common/Log/Logger';
import { WindowInjectedLogger } from '@/infrastructure/Log/WindowInjectedLogger';
import { ConsoleLogger } from '@/infrastructure/Log/ConsoleLogger';
import { NoopLogger } from '@/infrastructure/Log/NoopLogger';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
import { Constructible } from '@/TypeHelpers';
import { itIsSingleton } from '@tests/unit/shared/TestCases/SingletonTests';
import { LoggerStub } from '@tests/unit/shared/Stubs/LoggerStub';
describe('ClientLoggerFactory', () => {
describe('Current', () => {
itIsSingleton({
getter: () => ClientLoggerFactory.Current,
expectedType: ClientLoggerFactory,
});
});
describe('logger instantiation based on environment', () => {
const originalWindow = { ...window };
beforeEach(() => {
Object.assign(window, { log: new LoggerStub() });
});
afterEach(() => {
Object.assign(window, originalWindow);
});
const testCases: Array<{
readonly description: string,
readonly expectedType: Constructible<Logger>,
readonly environment: IRuntimeEnvironment,
}> = [
{
description: 'desktop environment',
expectedType: WindowInjectedLogger,
environment: new RuntimeEnvironmentStub()
.withIsDesktop(true),
},
{
description: 'non-production and desktop environment',
expectedType: WindowInjectedLogger,
environment: new RuntimeEnvironmentStub()
.withIsDesktop(true)
.withIsNonProduction(true),
},
{
description: 'non-production without desktop',
expectedType: ConsoleLogger,
environment: new RuntimeEnvironmentStub()
.withIsDesktop(false)
.withIsNonProduction(true),
},
{
description: 'production without desktop',
expectedType: NoopLogger,
environment: new RuntimeEnvironmentStub()
.withIsDesktop(false)
.withIsNonProduction(false),
},
];
testCases.forEach(({ description, expectedType, environment }) => {
it(`instantiates ${expectedType.name} for ${description}`, () => {
// arrange
const factory = new TestableClientLoggerFactory(environment);
// act
const { logger } = factory;
// assert
expect(logger).to.be.instanceOf(expectedType);
});
});
});
});
class TestableClientLoggerFactory extends ClientLoggerFactory {
public constructor(environment: IRuntimeEnvironment) {
super(environment);
}
}