Fix memory leak in unit tests (#5597)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-06-13 11:00:07 -04:00
1 parent 2eb585e88e
commit 3058bea88f
37 files changed
+1502 -1777

No files matched your search

@@ -11,6 +11,7 @@ import { runInAction } from "mobx";
import updateExtensionsStateInjectable from "../extension-loader/update-extensions-state/update-extensions-state.injectable";
import mockFs from "mock-fs";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import { delay } from "../../renderer/utils";
console = new Console(stdout, stderr);
@@ -125,42 +126,39 @@ describe("ExtensionLoader", () => {
mockFs.restore();
});
it("renderer updates extension after ipc broadcast", async done => {
it("renderer updates extension after ipc broadcast", async () => {
expect(extensionLoader.userExtensions).toMatchInlineSnapshot(`Map {}`);
await extensionLoader.init();
await delay(10);
setTimeout(() => {
// Assert the extensions after the extension broadcast event
expect(extensionLoader.userExtensions).toMatchInlineSnapshot(`
Map {
"manifest/path" => Object {
"absolutePath": "/test/1",
"id": "manifest/path",
"isBundled": false,
"isEnabled": true,
"manifest": Object {
"name": "TestExtension",
"version": "1.0.0",
},
"manifestPath": "manifest/path",
// Assert the extensions after the extension broadcast event
expect(extensionLoader.userExtensions).toMatchInlineSnapshot(`
Map {
"manifest/path" => Object {
"absolutePath": "/test/1",
"id": "manifest/path",
"isBundled": false,
"isEnabled": true,
"manifest": Object {
"name": "TestExtension",
"version": "1.0.0",
},
"manifest/path3" => Object {
"absolutePath": "/test/3",
"id": "manifest/path3",
"isBundled": false,
"isEnabled": true,
"manifest": Object {
"name": "TestExtension3",
"version": "3.0.0",
},
"manifestPath": "manifest/path3",
"manifestPath": "manifest/path",
},
"manifest/path3" => Object {
"absolutePath": "/test/3",
"id": "manifest/path3",
"isBundled": false,
"isEnabled": true,
"manifest": Object {
"name": "TestExtension3",
"version": "3.0.0",
},
}
`);
done();
}, 10);
"manifestPath": "manifest/path3",
},
}
`);
});
it("updates ExtensionsStore after isEnabled is changed", async () => {
@@ -17,6 +17,8 @@ import installExtensionInjectable
import directoryForUserDataInjectable
from "../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import mockFs from "mock-fs";
import { delay } from "../../renderer/utils";
import { observable, when } from "mobx";
jest.setTimeout(60_000);
@@ -64,7 +66,8 @@ describe("ExtensionDiscovery", () => {
mockFs.restore();
});
it("emits add for added extension", async (done) => {
it("emits add for added extension", async () => {
const letTestFinish = observable.box(false);
let addHandler!: (filePath: string) => void;
mockedFse.readJson.mockImplementation((p) => {
@@ -114,13 +117,14 @@ describe("ExtensionDiscovery", () => {
},
manifestPath: path.normalize("some-directory-for-user-data/node_modules/my-extension/package.json"),
});
done();
letTestFinish.set(true);
});
addHandler(path.join(extensionDiscovery.localFolderPath, "/my-extension/package.json"));
await when(() => letTestFinish.get());
});
it("doesn't emit add for added file under extension", async done => {
it("doesn't emit add for added file under extension", async () => {
let addHandler!: (filePath: string) => void;
const mockWatchInstance = {
@@ -146,10 +150,8 @@ describe("ExtensionDiscovery", () => {
addHandler(path.join(extensionDiscovery.localFolderPath, "/my-extension/node_modules/dep/package.json"));
setTimeout(() => {
expect(onAdd).not.toHaveBeenCalled();
done();
}, 10);
await delay(10);
expect(onAdd).not.toHaveBeenCalled();
});
});
@@ -17,6 +17,11 @@ export interface LensExtensionState {
name: string;
}
export interface IsEnabledExtensionDescriptor {
id: string;
isBundled: boolean;
}
export class ExtensionsStore extends BaseStore<LensExtensionsStoreModel> {
readonly displayName = "ExtensionsStore";
constructor() {
@@ -36,7 +41,7 @@ export class ExtensionsStore extends BaseStore<LensExtensionsStoreModel> {
protected state = observable.map<LensExtensionId, LensExtensionState>();
isEnabled({ id, isBundled }: { id: string; isBundled: boolean }): boolean {
isEnabled({ id, isBundled }: IsEnabledExtensionDescriptor): boolean {
// By default false, so that copied extensions are disabled by default.
// If user installs the extension from the UI, the Extensions component will specifically enable it.
return isBundled || Boolean(this.state.get(id)?.enabled);