Files
privacy.sexy/tests/unit/presentation/electron/preload/ContextBridging/RendererApiProvider.spec.ts
undergroundwires a721e82a4f Bump TypeScript to 5.3 with verbatimModuleSyntax
This commit upgrades TypeScript to the latest version 5.3 and introduces
`verbatimModuleSyntax` in line with the official Vue guide
recommendatinos (vuejs/docs#2592).

By enforcing `import type` for type-only imports, this commit improves
code clarity and supports tooling optimization, ensuring imports are
only bundled when necessary for runtime.

Changes:

- Bump TypeScript to 5.3.3 across the project.
- Adjust import statements to utilize `import type` where applicable,
  promoting cleaner and more efficient code.
2024-02-27 04:20:22 +01:00

124 lines
4.3 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { type ApiFacadeFactory, type IpcConsumerProxyCreator, provideWindowVariables } from '@/presentation/electron/preload/ContextBridging/RendererApiProvider';
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { Logger } from '@/application/Common/Log/Logger';
import { LoggerStub } from '@tests/unit/shared/Stubs/LoggerStub';
import type { PropertyKeys } from '@/TypeHelpers';
import type { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
import { IpcChannelDefinitions } from '@/presentation/electron/shared/IpcBridging/IpcChannelDefinitions';
import type { IpcChannel } from '@/presentation/electron/shared/IpcBridging/IpcChannel';
describe('RendererApiProvider', () => {
describe('provideWindowVariables', () => {
interface WindowVariableTestCase {
readonly description: string;
setupContext(context: RendererApiProviderTestContext): RendererApiProviderTestContext;
readonly expectedValue: unknown;
}
const testScenarios: Record<
PropertyKeys<Required<WindowVariables>>,
WindowVariableTestCase> = {
isRunningAsDesktopApplication: {
description: 'returns true',
setupContext: (context) => context,
expectedValue: true,
},
codeRunner: expectIpcConsumer(IpcChannelDefinitions.CodeRunner),
os: (() => {
const operatingSystem = OperatingSystem.WindowsPhone;
return {
description: 'returns expected',
setupContext: (context) => context.withOs(operatingSystem),
expectedValue: operatingSystem,
};
})(),
log: expectFacade({
instance: new LoggerStub(),
setupContext: (c, instance) => c.withLogger(instance),
}),
dialog: expectIpcConsumer(IpcChannelDefinitions.Dialog),
scriptDiagnosticsCollector: expectIpcConsumer(
IpcChannelDefinitions.ScriptDiagnosticsCollector,
),
};
Object.entries(testScenarios).forEach((
[property, { description, setupContext, expectedValue }],
) => {
it(`${property}: ${description}`, () => {
// arrange
const testContext = setupContext(new RendererApiProviderTestContext());
// act
const variables = testContext.provideWindowVariables();
// assert
const actualValue = variables[property];
expect(actualValue).to.equal(expectedValue);
});
});
function expectIpcConsumer<T>(expectedDefinition: IpcChannel<T>): WindowVariableTestCase {
const ipcConsumerCreator: IpcConsumerProxyCreator = (definition) => definition as never;
return {
description: 'creates correct IPC consumer',
setupContext: (context) => context
.withIpcConsumerCreator(ipcConsumerCreator),
expectedValue: expectedDefinition,
};
}
function expectFacade<T>(options: {
readonly instance: T;
setupContext: (
context: RendererApiProviderTestContext,
instance: T,
) => RendererApiProviderTestContext;
}): WindowVariableTestCase {
const createFacadeMock: ApiFacadeFactory = (obj) => obj;
return {
description: 'creates correct facade',
setupContext: (context) => options.setupContext(
context.withApiFacadeCreator(createFacadeMock),
options.instance,
),
expectedValue: options.instance,
};
}
});
});
class RendererApiProviderTestContext {
private os: OperatingSystem = OperatingSystem.Android;
private log: Logger = new LoggerStub();
private apiFacadeCreator: ApiFacadeFactory = (obj) => obj;
private ipcConsumerCreator: IpcConsumerProxyCreator = () => { return {} as never; };
public withOs(os: OperatingSystem): this {
this.os = os;
return this;
}
public withLogger(log: Logger): this {
this.log = log;
return this;
}
public withApiFacadeCreator(apiFacadeCreator: ApiFacadeFactory): this {
this.apiFacadeCreator = apiFacadeCreator;
return this;
}
public withIpcConsumerCreator(ipcConsumerCreator: IpcConsumerProxyCreator): this {
this.ipcConsumerCreator = ipcConsumerCreator;
return this;
}
public provideWindowVariables() {
return provideWindowVariables(
this.apiFacadeCreator,
this.ipcConsumerCreator,
() => this.os,
() => this.log,
);
}
}