mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix tray bugs (#5666)
* Make unit tests for tray behave correctly when item is disabled Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Make multiple separators in tray originating from same extension not throw up Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
10ba9ef853
commit
d0ed4e46b1
@ -0,0 +1,54 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { LensMainExtension } from "../../extensions/lens-main-extension";
|
||||||
|
import type { TrayMenuRegistration } from "../../main/tray/tray-menu-registration";
|
||||||
|
import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
|
||||||
|
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
|
||||||
|
import getRandomIdInjectable from "../../common/utils/get-random-id.injectable";
|
||||||
|
|
||||||
|
describe("multiple separators originating from extension", () => {
|
||||||
|
let applicationBuilder: ApplicationBuilder;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
applicationBuilder = getApplicationBuilder();
|
||||||
|
|
||||||
|
applicationBuilder.beforeApplicationStart(({ mainDi }) => {
|
||||||
|
mainDi.unoverride(getRandomIdInjectable);
|
||||||
|
mainDi.permitSideEffects(getRandomIdInjectable);
|
||||||
|
});
|
||||||
|
|
||||||
|
await applicationBuilder.render();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("given extension with multiple separators, when extension is enabled, does not throw", () => {
|
||||||
|
const someExtension = new SomeTestExtension({
|
||||||
|
id: "some-extension-id",
|
||||||
|
trayMenus: [{ type: "separator" }, { type: "separator" } ],
|
||||||
|
});
|
||||||
|
|
||||||
|
return expect(
|
||||||
|
applicationBuilder.extensions.main.enable(someExtension),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
class SomeTestExtension extends LensMainExtension {
|
||||||
|
constructor({ id, trayMenus }: {
|
||||||
|
id: string;
|
||||||
|
trayMenus: TrayMenuRegistration[];
|
||||||
|
}) {
|
||||||
|
super({
|
||||||
|
id,
|
||||||
|
absolutePath: "irrelevant",
|
||||||
|
isBundled: false,
|
||||||
|
isCompatible: false,
|
||||||
|
isEnabled: false,
|
||||||
|
manifest: { name: id, version: "some-version", engines: { lens: "^5.5.0" }},
|
||||||
|
manifestPath: "irrelevant",
|
||||||
|
});
|
||||||
|
|
||||||
|
this.trayMenus = trayMenus;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,6 +15,7 @@ import type { TrayMenuRegistration } from "../tray-menu-registration";
|
|||||||
import { withErrorSuppression } from "../../../common/utils/with-error-suppression/with-error-suppression";
|
import { withErrorSuppression } from "../../../common/utils/with-error-suppression/with-error-suppression";
|
||||||
import type { WithErrorLoggingFor } from "../../../common/utils/with-error-logging/with-error-logging.injectable";
|
import type { WithErrorLoggingFor } from "../../../common/utils/with-error-logging/with-error-logging.injectable";
|
||||||
import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable";
|
import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable";
|
||||||
|
import getRandomIdInjectable from "../../../common/utils/get-random-id.injectable";
|
||||||
|
|
||||||
const trayMenuItemRegistratorInjectable = getInjectable({
|
const trayMenuItemRegistratorInjectable = getInjectable({
|
||||||
id: "tray-menu-item-registrator",
|
id: "tray-menu-item-registrator",
|
||||||
@ -22,11 +23,12 @@ const trayMenuItemRegistratorInjectable = getInjectable({
|
|||||||
instantiate: (di) => (extension, installationCounter) => {
|
instantiate: (di) => (extension, installationCounter) => {
|
||||||
const mainExtension = extension as LensMainExtension;
|
const mainExtension = extension as LensMainExtension;
|
||||||
const withErrorLoggingFor = di.inject(withErrorLoggingInjectable);
|
const withErrorLoggingFor = di.inject(withErrorLoggingInjectable);
|
||||||
|
const getRandomId = di.inject(getRandomIdInjectable);
|
||||||
|
|
||||||
pipeline(
|
pipeline(
|
||||||
mainExtension.trayMenus,
|
mainExtension.trayMenus,
|
||||||
|
|
||||||
flatMap(toItemInjectablesFor(mainExtension, installationCounter, withErrorLoggingFor)),
|
flatMap(toItemInjectablesFor(mainExtension, installationCounter, withErrorLoggingFor, getRandomId)),
|
||||||
|
|
||||||
(injectables) => di.register(...injectables),
|
(injectables) => di.register(...injectables),
|
||||||
);
|
);
|
||||||
@ -37,9 +39,9 @@ const trayMenuItemRegistratorInjectable = getInjectable({
|
|||||||
|
|
||||||
export default trayMenuItemRegistratorInjectable;
|
export default trayMenuItemRegistratorInjectable;
|
||||||
|
|
||||||
const toItemInjectablesFor = (extension: LensMainExtension, installationCounter: number, withErrorLoggingFor: WithErrorLoggingFor) => {
|
const toItemInjectablesFor = (extension: LensMainExtension, installationCounter: number, withErrorLoggingFor: WithErrorLoggingFor, getRandomId: () => string) => {
|
||||||
const _toItemInjectables = (parentId: string | null) => (registration: TrayMenuRegistration): Injectable<TrayMenuItem, TrayMenuItem, void>[] => {
|
const _toItemInjectables = (parentId: string | null) => (registration: TrayMenuRegistration): Injectable<TrayMenuItem, TrayMenuItem, void>[] => {
|
||||||
const trayItemId = registration.id || kebabCase(registration.label || "");
|
const trayItemId = registration.id || kebabCase(registration.label || getRandomId());
|
||||||
const id = `${trayItemId}-tray-menu-item-for-extension-${extension.sanitizedExtensionId}-instance-${installationCounter}`;
|
const id = `${trayItemId}-tray-menu-item-for-extension-${extension.sanitizedExtensionId}-instance-${installationCounter}`;
|
||||||
|
|
||||||
const parentInjectable = getInjectable({
|
const parentInjectable = getInjectable({
|
||||||
@ -72,7 +74,7 @@ const toItemInjectablesFor = (extension: LensMainExtension, installationCounter:
|
|||||||
return decorated(registration);
|
return decorated(registration);
|
||||||
},
|
},
|
||||||
|
|
||||||
enabled: computed(() => !!registration.enabled),
|
enabled: computed(() => registration.enabled ?? true),
|
||||||
visible: computed(() => true),
|
visible: computed(() => true),
|
||||||
|
|
||||||
extension,
|
extension,
|
||||||
|
|||||||
@ -317,6 +317,10 @@ export const getApplicationBuilder = () => {
|
|||||||
throw new Error(`Tried to click tray menu item with ID ${id} which does not exist. Available IDs are: "${availableIds}"`);
|
throw new Error(`Tried to click tray menu item with ID ${id} which does not exist. Available IDs are: "${availableIds}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!menuItem.enabled.get()) {
|
||||||
|
throw new Error(`Tried to click tray menu item with ID ${id} which is disabled.`);
|
||||||
|
}
|
||||||
|
|
||||||
await menuItem.click?.();
|
await menuItem.click?.();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user