1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add unit testing requirement to have data serializable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-06-13 15:36:00 -04:00
parent 2c48be3ec0
commit 91d336518a
5 changed files with 36 additions and 20 deletions

View File

@ -16,6 +16,7 @@ import type { UpdateChannel, UpdateChannelId } from "../../common/application-up
import { updateChannels } from "../../common/application-update/update-channels";
import type { DownloadPlatformUpdate } from "../../main/application-update/download-platform-update/download-platform-update.injectable";
import downloadPlatformUpdateInjectable from "../../main/application-update/download-platform-update/download-platform-update.injectable";
import type { SelectedUpdateChannel } from "../../common/application-update/selected-update-channel/selected-update-channel.injectable";
import selectedUpdateChannelInjectable from "../../common/application-update/selected-update-channel/selected-update-channel.injectable";
import type { IComputedValue } from "mobx";
import setUpdateOnQuitInjectable from "../../main/electron-app/features/set-update-on-quit.injectable";
@ -80,10 +81,7 @@ describe("selection of update stability", () => {
});
describe('given update channel "alpha" is selected, when checking for updates', () => {
let selectedUpdateChannel: {
value: IComputedValue<UpdateChannel>;
setValue: (channelId: UpdateChannelId) => void;
};
let selectedUpdateChannel: SelectedUpdateChannel;
beforeEach(() => {
selectedUpdateChannel = applicationBuilder.dis.mainDi.inject(

View File

@ -4,6 +4,7 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { BrowserWindow } from "electron";
import { toJS } from "../../../../common/utils";
import type { SendToViewArgs } from "./lens-window-injection-token";
const sendToChannelInElectronBrowserWindowInjectable = getInjectable({
@ -19,10 +20,10 @@ const sendToChannelInElectronBrowserWindowInjectable = getInjectable({
browserWindow.webContents.sendToFrame(
[frameInfo.processId, frameInfo.frameId],
channel,
data,
toJS(data),
);
} else {
browserWindow.webContents.send(channel, data);
browserWindow.webContents.send(channel, toJS(data));
}
},

View File

@ -9,7 +9,8 @@ import type { SendToViewArgs } from "../../main/start-main-application/lens-wind
import enlistMessageChannelListenerInjectableInRenderer from "../../renderer/utils/channel/channel-listeners/enlist-message-channel-listener.injectable";
import type { DiContainer } from "@ogre-tools/injectable";
import { serialize } from "v8";
import { getOrInsertSet } from "../../renderer/utils";
import { getOrInsertSet, toJS } from "../../common/utils";
import { inspect } from "util";
export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => {
const messageChannelListenerFakesForRenderer = new Map<
@ -23,7 +24,7 @@ export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => {
() =>
(
browserWindow,
{ channel: channelId, frameInfo, data }: SendToViewArgs,
{ channel: channelId, frameInfo, data: rawData }: SendToViewArgs,
) => {
const listeners =
messageChannelListenerFakesForRenderer.get(channelId) || new Set();
@ -42,10 +43,15 @@ export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => {
);
}
const data = toJS(rawData);
try {
serialize(data);
} catch {
throw new Error(`Tried to send message to channel "${channelId}" but the value cannot be serialized.`);
} catch (error) {
throw new Error(`Tried to send message to channel "${channelId}" but the value cannot be serialized: ${inspect(data, {
colors: true,
depth: Infinity,
})}`);
}
listeners.forEach((listener) => listener.handler(data));

View File

@ -3,11 +3,12 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import { inspect } from "util";
import { serialize } from "v8";
import type { MessageChannel } from "../../common/utils/channel/message-channel-injection-token";
import type { MessageChannelListener } from "../../common/utils/channel/message-channel-listener-injection-token";
import enlistMessageChannelListenerInjectableInMain from "../../main/utils/channel/channel-listeners/enlist-message-channel-listener.injectable";
import { getOrInsertSet } from "../../renderer/utils";
import { getOrInsertSet, toJS } from "../../common/utils";
import sendToMainInjectable from "../../renderer/utils/channel/send-to-main.injectable";
export const overrideMessagingFromWindowToMain = (mainDi: DiContainer) => {
@ -32,11 +33,10 @@ export const overrideMessagingFromWindowToMain = (mainDi: DiContainer) => {
);
return (windowDi: DiContainer) => {
windowDi.override(sendToMainInjectable, () => (channelId, message) => {
const listeners =
messageChannelListenerFakesForMain.get(channelId) || new Set();
windowDi.override(sendToMainInjectable, () => (channelId, rawMessage) => {
const listeners = messageChannelListenerFakesForMain.get(channelId);
if (listeners.size === 0) {
if (!listeners || listeners.size === 0) {
throw new Error(
`Tried to send message to channel "${channelId}" but there where no listeners. Current channels with listeners: "${[
...messageChannelListenerFakesForMain.keys(),
@ -44,10 +44,15 @@ export const overrideMessagingFromWindowToMain = (mainDi: DiContainer) => {
);
}
const message = toJS(rawMessage);
try {
serialize(message);
} catch {
throw new Error(`Tried to send message to main channel "${channelId}" but the value cannot be serialized.`);
} catch (error) {
throw new Error(`Tried to send message to main channel "${channelId}" but the value cannot be serialized: ${inspect(message, {
colors: true,
depth: Infinity,
})}`);
}
listeners.forEach((listener) => listener.handler(message));

View File

@ -3,7 +3,9 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import { inspect } from "util";
import { serialize } from "v8";
import { toJS } from "../../common/utils";
import type { RequestChannel } from "../../common/utils/channel/request-channel-injection-token";
import type { RequestChannelHandler } from "../../common/utils/channel/request-channel-listener-injection-token";
import enlistRequestChannelListenerInjectableInMain from "../../main/utils/channel/channel-listeners/enlist-request-channel-listener.injectable";
@ -47,12 +49,16 @@ export const overrideRequestingFromWindowToMain = (mainDi: DiContainer) => {
);
}
const result = requestListener.handler(request);
const rawResult = await requestListener.handler(request);
const result = toJS(rawResult);
try {
serialize(result);
} catch {
throw new Error(`Tried to request value from channel "${id}" but the value cannot be serialized.`);
} catch (error) {
throw new Error(`Tried to request value from channel "${id}" but the value cannot be serialized: ${inspect(result, {
colors: true,
depth: Infinity,
})}`);
}
return result;