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

Replace factory function with transient dependency

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-01-31 12:24:37 +02:00 committed by Sebastian Malton
parent b1c3a59709
commit 5567b0df30
6 changed files with 16 additions and 11 deletions

View File

@ -20,6 +20,7 @@ import callForLogsInjectable from "../call-for-logs.injectable";
import { LogTabViewModel, LogTabViewModelDependencies } from "../logs-view-model";
import type { TabId } from "../../dock/store";
import userEvent from "@testing-library/user-event";
import { SearchStore } from "../../../../search-store/search-store";
jest.mock("electron", () => ({
app: {
@ -50,7 +51,7 @@ function mockLogTabViewModel(tabId: TabId, deps: Partial<LogTabViewModelDependen
stopLoadingLogs: jest.fn(),
getPodById: jest.fn(),
getPodsByOwnerId: jest.fn(),
createSearchStore: jest.fn(),
searchStore: new SearchStore(),
areLogsPresent: jest.fn(),
...deps,
});

View File

@ -30,7 +30,7 @@ function mockLogTabViewModel(tabId: TabId, deps: Partial<LogTabViewModelDependen
getPodById: jest.fn(),
getPodsByOwnerId: jest.fn(),
areLogsPresent: jest.fn(),
createSearchStore: () => new SearchStore(),
searchStore: new SearchStore(),
...deps,
});
}

View File

@ -14,9 +14,9 @@ import loadLogsInjectable from "./load-logs.injectable";
import setLogTabDataInjectable from "./set-log-tab-data.injectable";
import stopLoadingLogsInjectable from "./stop-loading-logs.injectable";
import { podsStore } from "../../+workloads-pods/pods.store";
import createSearchStoreInjectable from "../../../search-store/create-search-store.injectable";
import renameTabInjectable from "../dock/rename-tab.injectable";
import areLogsPresentInjectable from "./are-logs-present.injectable";
import searchStoreInjectable from "../../../search-store/search-store.injectable";
export interface InstantiateArgs {
tabId: TabId;
@ -36,7 +36,7 @@ const logsViewModelInjectable = getInjectable({
areLogsPresent: di.inject(areLogsPresentInjectable),
getPodById: id => podsStore.getById(id),
getPodsByOwnerId: id => podsStore.getPodsByOwnerId(id),
createSearchStore: di.inject(createSearchStoreInjectable),
searchStore: di.inject(searchStoreInjectable),
}),
lifecycle: lifecycleEnum.transient,
});

View File

@ -21,12 +21,16 @@ export interface LogTabViewModelDependencies {
getPodById: (id: string) => Pod | undefined;
getPodsByOwnerId: (id: string) => Pod[];
areLogsPresent: (tabId: TabId) => boolean;
createSearchStore: () => SearchStore;
searchStore: SearchStore;
}
export class LogTabViewModel {
constructor(protected readonly tabId: TabId, private readonly dependencies: LogTabViewModelDependencies) {}
get searchStore() {
return this.dependencies.searchStore;
}
readonly isLoading = computed(() => this.dependencies.areLogsPresent(this.tabId));
readonly logs = computed(() => this.dependencies.getLogs(this.tabId));
readonly logsWithoutTimestamps = computed(() => this.dependencies.getLogsWithoutTimestamps(this.tabId));
@ -54,7 +58,6 @@ export class LogTabViewModel {
return this.dependencies.getPodById(data.selectedPodId);
});
readonly searchStore = this.dependencies.createSearchStore();
updateLogTabData = (partialData: Partial<LogTabData>) => {
this.dependencies.setLogTabData(this.tabId, { ...this.logTabData.get(), ...partialData });

View File

@ -5,9 +5,9 @@
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { SearchStore } from "./search-store";
const createSearchStoreInjectable = getInjectable({
instantiate: () => () => new SearchStore(),
lifecycle: lifecycleEnum.singleton,
const searchStoreInjectable = getInjectable({
instantiate: () => new SearchStore(),
lifecycle: lifecycleEnum.transient,
});
export default createSearchStoreInjectable;
export default searchStoreInjectable;

View File

@ -9,6 +9,7 @@ import { stdout, stderr } from "process";
import { getDiForUnitTesting } from "../getDiForUnitTesting";
import directoryForUserDataInjectable
from "../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import searchStoreInjectable from "./search-store.injectable";
jest.mock("electron", () => ({
app: {
@ -34,7 +35,7 @@ describe("search store tests", () => {
await di.runSetups();
searchStore = new SearchStore();
searchStore = di.inject(searchStoreInjectable);
});
it("does nothing with empty search query", () => {