Implement a lot of technical requirements for behavioural unit tests

Note: the crux of this was to make routing env-agnostic, and not based on URLs as magic strings, but instead something type-enforced.

Note: extension-based routes comply to same exact interface by "late-registering" their routes when installed. Routes are just injectables.

Note: another chunk of global shared state is no more.

Note: a lot of explicit side effects have been cornered to injectables.

Note: a lot of stuff has become reactive as part if this.

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne SavolainenandMikko Aspiala committed 2022-03-31 15:13:26 +03:00
1 parent 086604630a
commit 5c8ca2c038
523 files changed
+11727 -5541

No files matched your search

+1 -1
View File
@@ -7,8 +7,8 @@ import { getDiForUnitTesting as getMainDi } from "../main/getDiForUnitTesting";
import { overrideIpcBridge } from "./override-ipc-bridge";
export const getDisForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverrides: false }) => {
const rendererDi = getRendererDi({ doGeneralOverrides });
const mainDi = getMainDi({ doGeneralOverrides });
const rendererDi = getRendererDi({ doGeneralOverrides });
overrideIpcBridge({ rendererDi, mainDi });
+65
View File
@@ -0,0 +1,65 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import readFileInjectable from "../common/fs/read-file.injectable";
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
import pathExistsInjectable from "../common/fs/path-exists.injectable";
type Override = (di: DiContainer) => void;
export const overrideFsWithFakes = (di: DiContainer) => {
const state = new Map();
const readFile = readFileFor(state);
const overrides: Override[] = [
(di) => {
di.override(readFileInjectable, () => readFile);
},
(di) => {
di.override(
writeJsonFileInjectable,
() => (filePath: string, contents: object) => {
state.set(filePath, JSON.stringify(contents));
return Promise.resolve();
},
);
},
(di) => {
di.override(readJsonFileInjectable, () => async (filePath: string) => {
const fileContent = await readFile(filePath);
return JSON.parse(fileContent.toString());
});
},
(di) => {
di.override(
pathExistsInjectable,
() => async (filePath: string) => Promise.resolve(state.has(filePath)),
);
},
];
overrides.forEach(callback => callback(di));
};
const readFileFor = (state: Map<string, string>) => (filePath: string) => {
const fileContent = state.get(filePath);
if (!fileContent) {
const existingFilePaths = [...state.keys()].join('", "');
throw new Error(
`Tried to access file ${filePath} which does not exist. Existing file paths are: "${existingFilePaths}"`,
);
}
return Promise.resolve(fileContent);
};
+33
View File
@@ -7,6 +7,10 @@ import type { Channel } from "../common/ipc-channel/channel";
import getValueFromRegisteredChannelInjectable from "../renderer/app-paths/get-value-from-registered-channel/get-value-from-registered-channel.injectable";
import registerChannelInjectable from "../main/app-paths/register-channel/register-channel.injectable";
import asyncFn from "@async-fn/jest";
import registerIpcChannelListenerInjectable from "../renderer/app-paths/get-value-from-registered-channel/register-ipc-channel-listener.injectable";
import windowManagerInjectable from "../main/window-manager.injectable";
import type { SendToViewArgs, WindowManager } from "../main/window-manager";
import { appNavigationIpcChannel } from "../common/front-end-routing/navigation-ipc-channel";
export const overrideIpcBridge = ({
rendererDi,
@@ -63,4 +67,33 @@ export const overrideIpcBridge = ({
mainDi.override(registerChannelInjectable, () => (channel, callback) => {
mainIpcRegistrations.set(channel, callback);
});
const rendererIpcFakeHandles = new Map<string, ((...args: any[]) => void)[]>();
rendererDi.override(
registerIpcChannelListenerInjectable,
() =>
({ channel, handle }) => {
const existingHandles = rendererIpcFakeHandles.get(channel.name) || [];
rendererIpcFakeHandles.set(channel.name, [...existingHandles, handle]);
},
);
mainDi.override(
windowManagerInjectable,
() =>
({
sendToView: ({ channel: channelName, data }: SendToViewArgs) => {
const handles = rendererIpcFakeHandles.get(channelName);
handles.forEach(handle => handle(...data));
},
navigateSync(url: string) {
this.sendToView({ channel: appNavigationIpcChannel.name, data: [url] });
},
} as unknown as WindowManager),
);
};