mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Replace jest.mock's with overriding dependencies (#6014)
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
03d2389f01
commit
81f7bd3c2c
50 files changed
+788
-605
No files matched your search
@@ -5,7 +5,6 @@
|
||||
|
||||
import * as uuid from "uuid";
|
||||
|
||||
import { broadcastMessage } from "../../../common/ipc";
|
||||
import { ProtocolHandlerExtension, ProtocolHandlerInternal, ProtocolHandlerInvalid } from "../../../common/protocol-handler";
|
||||
import { delay, noop } from "../../../common/utils";
|
||||
import type { ExtensionsStore, IsEnabledExtensionDescriptor } from "../../../extensions/extensions-store/extensions-store";
|
||||
@@ -19,8 +18,7 @@ import type { LensExtensionId } from "../../../extensions/lens-extension";
|
||||
import type { ObservableMap } from "mobx";
|
||||
import extensionInstancesInjectable from "../../../extensions/extension-loader/extension-instances.injectable";
|
||||
import directoryForUserDataInjectable from "../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
|
||||
jest.mock("../../../common/ipc");
|
||||
import broadcastMessageInjectable from "../../../common/ipc/broadcast-message.injectable";
|
||||
|
||||
function throwIfDefined(val: any): void {
|
||||
if (val != null) {
|
||||
@@ -32,6 +30,7 @@ describe("protocol router tests", () => {
|
||||
let extensionInstances: ObservableMap<LensExtensionId, LensExtension>;
|
||||
let lpr: LensProtocolRouterMain;
|
||||
let enabledExtensions: Set<string>;
|
||||
let broadcastMessageMock: jest.Mock;
|
||||
|
||||
beforeEach(async () => {
|
||||
const di = getDiForUnitTesting({ doGeneralOverrides: true });
|
||||
@@ -46,6 +45,9 @@ describe("protocol router tests", () => {
|
||||
|
||||
di.override(directoryForUserDataInjectable, () => "some-directory-for-user-data");
|
||||
|
||||
broadcastMessageMock = jest.fn();
|
||||
di.override(broadcastMessageInjectable, () => broadcastMessageMock);
|
||||
|
||||
extensionInstances = di.inject(extensionInstancesInjectable);
|
||||
lpr = di.inject(lensProtocolRouterMainInjectable);
|
||||
|
||||
@@ -54,12 +56,12 @@ describe("protocol router tests", () => {
|
||||
|
||||
it("should broadcast invalid protocol on non-lens URLs", async () => {
|
||||
await lpr.route("https://google.ca");
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerInvalid, "invalid protocol", "https://google.ca");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerInvalid, "invalid protocol", "https://google.ca");
|
||||
});
|
||||
|
||||
it("should broadcast invalid host on non internal or non extension URLs", async () => {
|
||||
await lpr.route("lens://foobar");
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerInvalid, "invalid host", "lens://foobar");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerInvalid, "invalid host", "lens://foobar");
|
||||
});
|
||||
|
||||
it("should not throw when has valid host", async () => {
|
||||
@@ -101,8 +103,8 @@ describe("protocol router tests", () => {
|
||||
}
|
||||
|
||||
await delay(50);
|
||||
expect(broadcastMessage).toHaveBeenCalledWith(ProtocolHandlerInternal, "lens://app", "matched");
|
||||
expect(broadcastMessage).toHaveBeenCalledWith(ProtocolHandlerExtension, "lens://extension/@mirantis/minikube", "matched");
|
||||
expect(broadcastMessageMock).toHaveBeenCalledWith(ProtocolHandlerInternal, "lens://app", "matched");
|
||||
expect(broadcastMessageMock).toHaveBeenCalledWith(ProtocolHandlerExtension, "lens://extension/@mirantis/minikube", "matched");
|
||||
});
|
||||
|
||||
it("should call handler if matches", async () => {
|
||||
@@ -117,7 +119,7 @@ describe("protocol router tests", () => {
|
||||
}
|
||||
|
||||
expect(called).toBe(true);
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page", "matched");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page", "matched");
|
||||
});
|
||||
|
||||
it("should call most exact handler", async () => {
|
||||
@@ -133,7 +135,7 @@ describe("protocol router tests", () => {
|
||||
}
|
||||
|
||||
expect(called).toBe("foo");
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page/foo", "matched");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page/foo", "matched");
|
||||
});
|
||||
|
||||
it("should call most exact handler for an extension", async () => {
|
||||
@@ -174,7 +176,7 @@ describe("protocol router tests", () => {
|
||||
|
||||
await delay(50);
|
||||
expect(called).toBe("foob");
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerExtension, "lens://extension/@foobar/icecream/page/foob", "matched");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerExtension, "lens://extension/@foobar/icecream/page/foob", "matched");
|
||||
});
|
||||
|
||||
it("should work with non-org extensions", async () => {
|
||||
@@ -244,7 +246,7 @@ describe("protocol router tests", () => {
|
||||
await delay(50);
|
||||
|
||||
expect(called).toBe(1);
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerExtension, "lens://extension/icecream/page", "matched");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerExtension, "lens://extension/icecream/page", "matched");
|
||||
});
|
||||
|
||||
it("should throw if urlSchema is invalid", () => {
|
||||
@@ -266,7 +268,7 @@ describe("protocol router tests", () => {
|
||||
}
|
||||
|
||||
expect(called).toBe(3);
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page/foo/bar/bat", "matched");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page/foo/bar/bat", "matched");
|
||||
});
|
||||
|
||||
it("should call most exact handler with 2 found handlers", async () => {
|
||||
@@ -283,6 +285,6 @@ describe("protocol router tests", () => {
|
||||
}
|
||||
|
||||
expect(called).toBe(1);
|
||||
expect(broadcastMessage).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page/foo/bar/bat", "matched");
|
||||
expect(broadcastMessageMock).toBeCalledWith(ProtocolHandlerInternal, "lens://app/page/foo/bar/bat", "matched");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user