mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix unit tests
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
746c0a73a2
commit
ba45bfea1e
13
src/common/fs/read-dir.injectable.ts
Normal file
13
src/common/fs/read-dir.injectable.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||||
|
import fsInjectable from "./fs.injectable";
|
||||||
|
|
||||||
|
const readDirInjectable = getInjectable({
|
||||||
|
instantiate: (di) => di.inject(fsInjectable).readdir,
|
||||||
|
lifecycle: lifecycleEnum.singleton,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default readDirInjectable;
|
||||||
13
src/common/fs/read-file.injectable.ts
Normal file
13
src/common/fs/read-file.injectable.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||||
|
import fsInjectable from "./fs.injectable";
|
||||||
|
|
||||||
|
const readFileInjectable = getInjectable({
|
||||||
|
instantiate: (di) => di.inject(fsInjectable).readFile,
|
||||||
|
lifecycle: lifecycleEnum.singleton,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default readFileInjectable;
|
||||||
@ -2,15 +2,11 @@
|
|||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { readJsonFile } from "./read-json-file";
|
|
||||||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||||
import fsInjectable from "../fs.injectable";
|
import fsInjectable from "./fs.injectable";
|
||||||
|
|
||||||
const readJsonFileInjectable = getInjectable({
|
const readJsonFileInjectable = getInjectable({
|
||||||
instantiate: (di) => readJsonFile({
|
instantiate: (di) => di.inject(fsInjectable).readJson,
|
||||||
fs: di.inject(fsInjectable),
|
|
||||||
}),
|
|
||||||
|
|
||||||
lifecycle: lifecycleEnum.singleton,
|
lifecycle: lifecycleEnum.singleton,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
import type { JsonObject } from "type-fest";
|
|
||||||
|
|
||||||
interface Dependencies {
|
|
||||||
fs: {
|
|
||||||
readJson: (filePath: string) => Promise<JsonObject>;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export const readJsonFile =
|
|
||||||
({ fs }: Dependencies) =>
|
|
||||||
(filePath: string) =>
|
|
||||||
fs.readJson(filePath);
|
|
||||||
@ -3,11 +3,10 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||||
import { writeJsonFile } from "./write-json-file";
|
import fsInjectable from "./fs.injectable";
|
||||||
import fsInjectable from "../fs.injectable";
|
|
||||||
|
|
||||||
const writeJsonFileInjectable = getInjectable({
|
const writeJsonFileInjectable = getInjectable({
|
||||||
instantiate: (di) => writeJsonFile({ fs: di.inject(fsInjectable) }),
|
instantiate: (di) => di.inject(fsInjectable).writeJson,
|
||||||
lifecycle: lifecycleEnum.singleton,
|
lifecycle: lifecycleEnum.singleton,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
import path from "path";
|
|
||||||
import type { JsonObject } from "type-fest";
|
|
||||||
|
|
||||||
interface Dependencies {
|
|
||||||
fs: {
|
|
||||||
ensureDir: (
|
|
||||||
directoryName: string,
|
|
||||||
options: { mode: number }
|
|
||||||
) => Promise<void>;
|
|
||||||
|
|
||||||
writeJson: (
|
|
||||||
filePath: string,
|
|
||||||
contentObject: JsonObject,
|
|
||||||
options: { spaces: number }
|
|
||||||
) => Promise<void>;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export const writeJsonFile =
|
|
||||||
({ fs }: Dependencies) =>
|
|
||||||
async (filePath: string, contentObject: JsonObject) => {
|
|
||||||
const directoryName = path.dirname(filePath);
|
|
||||||
|
|
||||||
await fs.ensureDir(directoryName, { mode: 0o755 });
|
|
||||||
|
|
||||||
await fs.writeJson(filePath, contentObject, { spaces: 2 });
|
|
||||||
};
|
|
||||||
@ -12,8 +12,8 @@ import getElectronAppPathInjectable from "./app-paths/get-electron-app-path/get-
|
|||||||
import setElectronAppPathInjectable from "./app-paths/set-electron-app-path/set-electron-app-path.injectable";
|
import setElectronAppPathInjectable from "./app-paths/set-electron-app-path/set-electron-app-path.injectable";
|
||||||
import appNameInjectable from "./app-paths/app-name/app-name.injectable";
|
import appNameInjectable from "./app-paths/app-name/app-name.injectable";
|
||||||
import registerChannelInjectable from "./app-paths/register-channel/register-channel.injectable";
|
import registerChannelInjectable from "./app-paths/register-channel/register-channel.injectable";
|
||||||
import writeJsonFileInjectable from "../common/fs/write-json-file/write-json-file.injectable";
|
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
|
||||||
import readJsonFileInjectable from "../common/fs/read-json-file/read-json-file.injectable";
|
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
|
||||||
|
|
||||||
export const getDiForUnitTesting = (
|
export const getDiForUnitTesting = (
|
||||||
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
||||||
|
|||||||
@ -4,19 +4,26 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { readdir, readFile } from "fs/promises";
|
|
||||||
import { hasCorrectExtension } from "./has-correct-extension";
|
import { hasCorrectExtension } from "./has-correct-extension";
|
||||||
import type { RawTemplates } from "./create-resource-templates.injectable";
|
import type { RawTemplates } from "./create-resource-templates.injectable";
|
||||||
|
import "../../../../common/vars";
|
||||||
|
import readFileInjectable from "../../../../common/fs/read-file.injectable";
|
||||||
|
import readDirInjectable from "../../../../common/fs/read-dir.injectable";
|
||||||
|
|
||||||
const templatesFolder = path.resolve(__static, "../templates/create-resource");
|
interface Dependencies {
|
||||||
|
readDir: (dirPath: string) => Promise<string[]>;
|
||||||
|
readFile: (filePath: string, encoding: "utf-8") => Promise<string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getTemplates({ readDir, readFile }: Dependencies) {
|
||||||
|
const templatesFolder = path.resolve(__static, "../templates/create-resource");
|
||||||
|
|
||||||
async function getTemplates() {
|
|
||||||
/**
|
/**
|
||||||
* Mapping between file names and their contents
|
* Mapping between file names and their contents
|
||||||
*/
|
*/
|
||||||
const templates: [file: string, contents: string][] = [];
|
const templates: [file: string, contents: string][] = [];
|
||||||
|
|
||||||
for (const dirEntry of await readdir(templatesFolder)) {
|
for (const dirEntry of await readDir(templatesFolder)) {
|
||||||
if (hasCorrectExtension(dirEntry)) {
|
if (hasCorrectExtension(dirEntry)) {
|
||||||
templates.push([path.parse(dirEntry).name, await readFile(path.join(templatesFolder, dirEntry), "utf-8")]);
|
templates.push([path.parse(dirEntry).name, await readFile(path.join(templatesFolder, dirEntry), "utf-8")]);
|
||||||
}
|
}
|
||||||
@ -28,8 +35,11 @@ async function getTemplates() {
|
|||||||
let lensTemplatePaths: RawTemplates;
|
let lensTemplatePaths: RawTemplates;
|
||||||
|
|
||||||
const lensCreateResourceTemplatesInjectable = getInjectable({
|
const lensCreateResourceTemplatesInjectable = getInjectable({
|
||||||
setup: async () => {
|
setup: async (di) => {
|
||||||
lensTemplatePaths = ["lens", await getTemplates()];
|
lensTemplatePaths = ["lens", await getTemplates({
|
||||||
|
readFile: di.inject(readFileInjectable),
|
||||||
|
readDir: di.inject(readDirInjectable),
|
||||||
|
})];
|
||||||
},
|
},
|
||||||
instantiate: () => lensTemplatePaths,
|
instantiate: () => lensTemplatePaths,
|
||||||
lifecycle: lifecycleEnum.singleton,
|
lifecycle: lifecycleEnum.singleton,
|
||||||
|
|||||||
@ -21,8 +21,8 @@ function groupTemplates(templates: Map<string, string>): RawTemplates[] {
|
|||||||
for (const [filePath, contents] of templates) {
|
for (const [filePath, contents] of templates) {
|
||||||
const rawRelative = path.dirname(path.relative(userTemplatesFolder, filePath));
|
const rawRelative = path.dirname(path.relative(userTemplatesFolder, filePath));
|
||||||
const title = rawRelative === "."
|
const title = rawRelative === "."
|
||||||
? "ungrouped"
|
? "ungrouped"
|
||||||
: rawRelative;
|
: rawRelative;
|
||||||
|
|
||||||
getOrInsert(res, title, []).push([path.parse(filePath).name, contents]);
|
getOrInsert(res, title, []).push([path.parse(filePath).name, contents]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -83,7 +83,11 @@ const getFewPodsTabData = (tabId: TabId, deps: Partial<LogTabViewModelDependenci
|
|||||||
|
|
||||||
return mockLogTabViewModel(tabId, {
|
return mockLogTabViewModel(tabId, {
|
||||||
getLogTabData: () => ({
|
getLogTabData: () => ({
|
||||||
ownerId: "uuid",
|
owner: {
|
||||||
|
uid: "uuid",
|
||||||
|
kind: "Deployment",
|
||||||
|
name: "super-deployment",
|
||||||
|
},
|
||||||
selectedPodId: selectedPod.getId(),
|
selectedPodId: selectedPod.getId(),
|
||||||
selectedContainer: selectedPod.getContainers()[0].name,
|
selectedContainer: selectedPod.getContainers()[0].name,
|
||||||
namespace: selectedPod.getNs(),
|
namespace: selectedPod.getNs(),
|
||||||
@ -181,12 +185,13 @@ describe("<LogResourceSelector />", () => {
|
|||||||
expect(await findByText("init-node-exporter-1")).toBeInTheDocument();
|
expect(await findByText("init-node-exporter-1")).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders pod owner as dropdown title", async () => {
|
it("renders pod owner as badge", async () => {
|
||||||
const model = getFewPodsTabData("foobar");
|
const model = getFewPodsTabData("foobar");
|
||||||
const { findByText, container } = render(<LogResourceSelector model={model} />);
|
const { findByText } = render(<LogResourceSelector model={model} />);
|
||||||
|
|
||||||
selectEvent.openMenu(container.querySelector(".pod-selector"));
|
expect(await findByText("super-deployment", {
|
||||||
expect(await findByText("super-deployment")).toBeInTheDocument();
|
exact: false,
|
||||||
|
})).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("updates tab name if selected pod changes", async () => {
|
it("updates tab name if selected pod changes", async () => {
|
||||||
@ -197,6 +202,6 @@ describe("<LogResourceSelector />", () => {
|
|||||||
selectEvent.openMenu(container.querySelector(".pod-selector"));
|
selectEvent.openMenu(container.querySelector(".pod-selector"));
|
||||||
|
|
||||||
userEvent.click(await findByText("deploymentPod2", { selector: ".pod-selector-menu .Select__option" }));
|
userEvent.click(await findByText("deploymentPod2", { selector: ".pod-selector-menu .Select__option" }));
|
||||||
expect(renameTab).toBeCalledWith("foobar", "Pod deploymentPod1");
|
expect(renameTab).toBeCalledWith("foobar", "Pod deploymentPod2");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -20,15 +20,14 @@ jest.mock("../../../../common/ipc");
|
|||||||
jest.mock("../../../ipc");
|
jest.mock("../../../ipc");
|
||||||
|
|
||||||
jest.mock("../../../../common/vars", () => {
|
jest.mock("../../../../common/vars", () => {
|
||||||
const SemVer = require("semver").SemVer;
|
const { SemVer } = require("semver");
|
||||||
|
|
||||||
const versionStub = new SemVer("1.0.0");
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...jest.requireActual<{}>("../../../../common/vars"),
|
||||||
__esModule: true,
|
__esModule: true,
|
||||||
isWindows: null,
|
isWindows: null,
|
||||||
isLinux: null,
|
isLinux: null,
|
||||||
appSemVer: versionStub,
|
appSemVer: new SemVer("1.0.0"),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -16,13 +16,12 @@ import directoryForUserDataInjectable from "../../../../common/app-paths/directo
|
|||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
|
||||||
jest.mock("../../../../common/vars", () => {
|
jest.mock("../../../../common/vars", () => {
|
||||||
const SemVer = require("semver").SemVer;
|
const { SemVer } = require("semver");
|
||||||
|
|
||||||
const versionStub = new SemVer("1.0.0");
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...jest.requireActual<{}>("../../../../common/vars"),
|
||||||
isMac: true,
|
isMac: true,
|
||||||
appSemVer: versionStub,
|
appSemVer: new SemVer("1.0.0"),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -8,8 +8,10 @@ import { memoize } from "lodash/fp";
|
|||||||
import { createContainer } from "@ogre-tools/injectable";
|
import { createContainer } from "@ogre-tools/injectable";
|
||||||
import { setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
import { setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
||||||
import getValueFromRegisteredChannelInjectable from "./app-paths/get-value-from-registered-channel/get-value-from-registered-channel.injectable";
|
import getValueFromRegisteredChannelInjectable from "./app-paths/get-value-from-registered-channel/get-value-from-registered-channel.injectable";
|
||||||
import writeJsonFileInjectable from "../common/fs/write-json-file/write-json-file.injectable";
|
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
|
||||||
import readJsonFileInjectable from "../common/fs/read-json-file/read-json-file.injectable";
|
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
|
||||||
|
import readDirInjectable from "../common/fs/read-dir.injectable";
|
||||||
|
import readFileInjectable from "../common/fs/read-file.injectable";
|
||||||
|
|
||||||
export const getDiForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverrides: false }) => {
|
export const getDiForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverrides: false }) => {
|
||||||
const di = createContainer();
|
const di = createContainer();
|
||||||
@ -31,6 +33,12 @@ export const getDiForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverride
|
|||||||
if (doGeneralOverrides) {
|
if (doGeneralOverrides) {
|
||||||
di.override(getValueFromRegisteredChannelInjectable, () => () => undefined);
|
di.override(getValueFromRegisteredChannelInjectable, () => () => undefined);
|
||||||
|
|
||||||
|
di.override(readDirInjectable, () => () => Promise.resolve([]));
|
||||||
|
|
||||||
|
di.override(readFileInjectable, () => () => {
|
||||||
|
throw new Error("Tried to read a file from file system without specifying explicit overrride");
|
||||||
|
});
|
||||||
|
|
||||||
di.override(writeJsonFileInjectable, () => () => {
|
di.override(writeJsonFileInjectable, () => () => {
|
||||||
throw new Error("Tried to write JSON file to file system without specifying explicit override.");
|
throw new Error("Tried to write JSON file to file system without specifying explicit override.");
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,8 +5,8 @@
|
|||||||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||||
import directoryForLensLocalStorageInjectable from "../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
|
import directoryForLensLocalStorageInjectable from "../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
|
||||||
import { createStorage } from "./create-storage";
|
import { createStorage } from "./create-storage";
|
||||||
import readJsonFileInjectable from "../../../common/fs/read-json-file/read-json-file.injectable";
|
import readJsonFileInjectable from "../../../common/fs/read-json-file.injectable";
|
||||||
import writeJsonFileInjectable from "../../../common/fs/write-json-file/write-json-file.injectable";
|
import writeJsonFileInjectable from "../../../common/fs/write-json-file.injectable";
|
||||||
|
|
||||||
const createStorageInjectable = getInjectable({
|
const createStorageInjectable = getInjectable({
|
||||||
instantiate: (di) =>
|
instantiate: (di) =>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user