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

fix unit test

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-03-29 16:52:29 -04:00
parent ad9767a4c6
commit 39efaabebc
2 changed files with 61 additions and 58 deletions

View File

@ -1,9 +1,11 @@
import mockFs from "mock-fs";
import { watch } from "chokidar"; import { watch } from "chokidar";
import { join, normalize } from "path"; import path from "path";
import { ExtensionDiscovery, InstalledExtension } from "../extension-discovery"; import { ExtensionDiscovery } from "../extension-discovery";
import os from "os";
import { Console } from "console";
jest.mock("../../common/ipc"); jest.mock("../../common/ipc");
jest.mock("fs-extra");
jest.mock("chokidar", () => ({ jest.mock("chokidar", () => ({
watch: jest.fn() watch: jest.fn()
})); }));
@ -14,13 +16,24 @@ jest.mock("../extension-installer", () => ({
} }
})); }));
console = new Console(process.stdout, process.stderr); // fix mockFS
const mockedWatch = watch as jest.MockedFunction<typeof watch>; const mockedWatch = watch as jest.MockedFunction<typeof watch>;
describe("ExtensionDiscovery", () => { describe("ExtensionDiscovery", () => {
it("emits add for added extension", async done => { describe("with mockFs", () => {
globalThis.__non_webpack_require__.mockImplementation(() => ({ beforeEach(() => {
mockFs({
[`${os.homedir()}/.k8slens/extensions/my-extension/package.json`]: JSON.stringify({
name: "my-extension" name: "my-extension"
})); }),
});
});
afterEach(() => {
mockFs.restore();
});
it("emits add for added extension", async (done) => {
let addHandler: (filePath: string) => void; let addHandler: (filePath: string) => void;
const mockWatchInstance: any = { const mockWatchInstance: any = {
@ -43,21 +56,22 @@ describe("ExtensionDiscovery", () => {
await extensionDiscovery.watchExtensions(); await extensionDiscovery.watchExtensions();
extensionDiscovery.events.on("add", (extension: InstalledExtension) => { extensionDiscovery.events.on("add", extension => {
expect(extension).toEqual({ expect(extension).toEqual({
absolutePath: expect.any(String), absolutePath: expect.any(String),
id: normalize("node_modules/my-extension/package.json"), id: path.normalize("node_modules/my-extension/package.json"),
isBundled: false, isBundled: false,
isEnabled: false, isEnabled: false,
manifest: { manifest: {
name: "my-extension", name: "my-extension",
}, },
manifestPath: normalize("node_modules/my-extension/package.json"), manifestPath: path.normalize("node_modules/my-extension/package.json"),
}); });
done(); done();
}); });
addHandler(join(extensionDiscovery.localFolderPath, "/my-extension/package.json")); addHandler(path.join(extensionDiscovery.localFolderPath, "/my-extension/package.json"));
});
}); });
it("doesn't emit add for added file under extension", async done => { it("doesn't emit add for added file under extension", async done => {
@ -87,7 +101,7 @@ describe("ExtensionDiscovery", () => {
extensionDiscovery.events.on("add", onAdd); extensionDiscovery.events.on("add", onAdd);
addHandler(join(extensionDiscovery.localFolderPath, "/my-extension/node_modules/dep/package.json")); addHandler(path.join(extensionDiscovery.localFolderPath, "/my-extension/node_modules/dep/package.json"));
setTimeout(() => { setTimeout(() => {
expect(onAdd).not.toHaveBeenCalled(); expect(onAdd).not.toHaveBeenCalled();

View File

@ -66,11 +66,7 @@ export class ExtensionDiscovery {
// IPC channel to broadcast changes to extension-discovery from main // IPC channel to broadcast changes to extension-discovery from main
protected static readonly extensionDiscoveryChannel = "extension-discovery:main"; protected static readonly extensionDiscoveryChannel = "extension-discovery:main";
public events: EventEmitter; public events = new EventEmitter();
constructor() {
this.events = new EventEmitter();
}
get localFolderPath(): string { get localFolderPath(): string {
return path.join(os.homedir(), ".k8slens", "extensions"); return path.join(os.homedir(), ".k8slens", "extensions");
@ -172,7 +168,7 @@ export class ExtensionDiscovery {
if (extension) { if (extension) {
// Remove a broken symlink left by a previous installation if it exists. // Remove a broken symlink left by a previous installation if it exists.
await this.removeSymlinkByManifestPath(manifestPath); await fse.remove(extension.manifestPath);
// Install dependencies for the new extension // Install dependencies for the new extension
await this.installPackage(extension.absolutePath); await this.installPackage(extension.absolutePath);
@ -231,16 +227,6 @@ export class ExtensionDiscovery {
return fse.remove(this.getInstalledPath(name)); return fse.remove(this.getInstalledPath(name));
} }
/**
* Remove the symlink under node_modules if it exists.
* @param manifestPath Path to package.json
*/
removeSymlinkByManifestPath(manifestPath: string) {
const manifestJson = __non_webpack_require__(manifestPath);
return this.removeSymlinkByPackageName(manifestJson.name);
}
/** /**
* Uninstalls extension. * Uninstalls extension.
* The application will detect the folder unlink and remove the extension from the UI automatically. * The application will detect the folder unlink and remove the extension from the UI automatically.
@ -325,7 +311,10 @@ export class ExtensionDiscovery {
*/ */
protected async getByManifest(manifestPath: string, { isBundled = false } = {}): Promise<InstalledExtension | null> { protected async getByManifest(manifestPath: string, { isBundled = false } = {}): Promise<InstalledExtension | null> {
try { try {
console.log(manifestPath);
const manifest = await fse.readJson(manifestPath); const manifest = await fse.readJson(manifestPath);
console.log(manifest);
const installedManifestPath = this.getInstalledManifestPath(manifest.name); const installedManifestPath = this.getInstalledManifestPath(manifest.name);
const isEnabled = isBundled || extensionsStore.isEnabled(installedManifestPath); const isEnabled = isBundled || extensionsStore.isEnabled(installedManifestPath);