Remove method Explorer.openSettingPane (#880)

This commit is contained in:
Hardikkumar Nai
2021-06-10 18:39:38 +05:30
committed by GitHub
parent c632342a43
commit c980af9a5c
6 changed files with 83 additions and 88 deletions

View File

@@ -0,0 +1,477 @@
import * as ko from "knockout";
import { ActionContracts } from "../../Contracts/ExplorerContracts";
import * as ViewModels from "../../Contracts/ViewModels";
import Explorer from "../Explorer";
import { handleOpenAction } from "./OpenActions";
describe("OpenActions", () => {
describe("handleOpenAction", () => {
let explorer: Explorer;
let database: ViewModels.Database;
let collection: ViewModels.Collection;
beforeEach(() => {
explorer = {} as Explorer;
explorer.onNewCollectionClicked = jest.fn();
database = {
id: ko.observable("db"),
collections: ko.observableArray<ViewModels.Collection>([]),
} as ViewModels.Database;
collection = {
id: ko.observable("coll"),
} as ViewModels.Collection;
collection.expandCollection = jest.fn();
collection.onDocumentDBDocumentsClick = jest.fn();
collection.onMongoDBDocumentsClick = jest.fn();
collection.onSchemaAnalyzerClick = jest.fn();
collection.onTableEntitiesClick = jest.fn();
collection.onGraphDocumentsClick = jest.fn();
collection.onNewQueryClick = jest.fn();
collection.onSettingsClick = jest.fn();
});
describe("unknown action type", () => {
it("should not be handled", () => {
const action = {
actionType: "foo",
};
const actionHandled = handleOpenAction(action, [], explorer);
expect(actionHandled).toBe(false);
});
});
describe("OpenPane action type", () => {
it("should handle enum value", () => {
const action = {
actionType: ActionContracts.ActionType.OpenPane,
};
const actionHandled = handleOpenAction(action, [], explorer);
expect(actionHandled).toBe(true);
});
it("should handle string value", () => {
const action = {
actionType: "OpenPane",
};
const actionHandled = handleOpenAction(action, [], explorer);
expect(actionHandled).toBe(true);
});
describe("AddCollection pane kind", () => {
it("string value should call explorer.onNewCollectionClicked", () => {
const action = {
actionType: "OpenPane",
paneKind: "AddCollection",
};
handleOpenAction(action, [], explorer);
expect(explorer.onNewCollectionClicked).toHaveBeenCalled();
});
it("enum value should call explorer.onNewCollectionClicked", () => {
const action = {
actionType: "OpenPane",
paneKind: ActionContracts.PaneKind.AddCollection,
};
handleOpenAction(action, [], explorer);
expect(explorer.onNewCollectionClicked).toHaveBeenCalled();
});
});
});
describe("OpenCollectionTab action type", () => {
it("should handle string value", () => {
const action = {
actionType: "OpenCollectionTab",
};
const actionHandled = handleOpenAction(action, [], explorer);
expect(actionHandled).toBe(true);
});
it("should handle enum value", () => {
const action = {
actionType: ActionContracts.ActionType.OpenCollectionTab,
};
const actionHandled = handleOpenAction(action, [], explorer);
expect(actionHandled).toBe(true);
});
it("should expand collection node when handleOpenAction is called before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.expandCollection).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.expandCollection).toHaveBeenCalled();
});
it("should expand collection node when handleOpenAction is called", () => {
const action = {
actionType: "OpenCollectionTab",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.expandCollection).toHaveBeenCalled();
});
describe("SQLDocuments tab kind", () => {
it("string value should call onDocumentDBDocumentsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "SQLDocuments",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onDocumentDBDocumentsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onDocumentDBDocumentsClick).toHaveBeenCalled();
});
it("string value should call onDocumentDBDocumentsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "SQLDocuments",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onDocumentDBDocumentsClick).toHaveBeenCalled();
});
it("enum value should call onDocumentDBDocumentsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.SQLDocuments,
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onDocumentDBDocumentsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onDocumentDBDocumentsClick).toHaveBeenCalled();
});
it("enum value should call onDocumentDBDocumentsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.SQLDocuments,
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onDocumentDBDocumentsClick).toHaveBeenCalled();
});
});
describe("MongoDocuments tab kind", () => {
it("string value should call onMongoDBDocumentsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "MongoDocuments",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onMongoDBDocumentsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onMongoDBDocumentsClick).toHaveBeenCalled();
});
it("string value should call onMongoDBDocumentsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "MongoDocuments",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onMongoDBDocumentsClick).toHaveBeenCalled();
});
it("enum value should call onMongoDBDocumentsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.MongoDocuments,
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onMongoDBDocumentsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onMongoDBDocumentsClick).toHaveBeenCalled();
});
it("enum value should call onMongoDBDocumentsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.MongoDocuments,
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onMongoDBDocumentsClick).toHaveBeenCalled();
});
});
describe("TableEntities tab kind", () => {
it("string value should call onTableEntitiesClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "TableEntities",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onTableEntitiesClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onTableEntitiesClick).toHaveBeenCalled();
});
it("string value should call onTableEntitiesClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "TableEntities",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onTableEntitiesClick).toHaveBeenCalled();
});
it("enum value should call onTableEntitiesClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.TableEntities,
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onTableEntitiesClick).toHaveBeenCalled();
});
it("enum value should call onTableEntitiesClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.TableEntities,
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onTableEntitiesClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onTableEntitiesClick).toHaveBeenCalled();
});
});
describe("Graph tab kind", () => {
it("string value should call onGraphDocumentsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "Graph",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onGraphDocumentsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onGraphDocumentsClick).toHaveBeenCalled();
});
it("string value should call onGraphDocumentsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "Graph",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onGraphDocumentsClick).toHaveBeenCalled();
});
it("enum value should call onGraphDocumentsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.Graph,
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onGraphDocumentsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onGraphDocumentsClick).toHaveBeenCalled();
});
it("enum value should call onGraphDocumentsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.Graph,
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onGraphDocumentsClick).toHaveBeenCalled();
});
});
describe("SQLQuery tab kind", () => {
it("string value should call onNewQueryClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "SQLQuery",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onNewQueryClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onNewQueryClick).toHaveBeenCalled();
});
it("string value should call onNewQueryClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "SQLQuery",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onNewQueryClick).toHaveBeenCalled();
});
it("enum value should call onNewQueryClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.SQLQuery,
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onNewQueryClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onNewQueryClick).toHaveBeenCalled();
});
it("enum value should call onNewQueryClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.SQLQuery,
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onNewQueryClick).toHaveBeenCalled();
});
});
describe("ScaleSettings tab kind", () => {
it("string value should call onSettingsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "ScaleSettings",
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onSettingsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onSettingsClick).toHaveBeenCalled();
});
it("string value should call onSettingsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: "ScaleSettings",
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onSettingsClick).toHaveBeenCalled();
});
it("enum value should call onSettingsClick before collections are fetched", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.ScaleSettings,
databaseResourceId: "db",
collectionResourceId: "coll",
};
handleOpenAction(action, [database], explorer);
expect(collection.onSettingsClick).not.toHaveBeenCalled();
database.collections([collection]);
expect(collection.onSettingsClick).toHaveBeenCalled();
});
it("enum value should call onSettingsClick", () => {
const action = {
actionType: "OpenCollectionTab",
tabKind: ActionContracts.TabKind.ScaleSettings,
databaseResourceId: "db",
collectionResourceId: "coll",
};
database.collections([collection]);
handleOpenAction(action, [database], explorer);
expect(collection.onSettingsClick).toHaveBeenCalled();
});
});
});
});
});

View File

@@ -0,0 +1,188 @@
// TODO convert this file to an action registry in order to have actions and their handlers be more tightly coupled.
import React from "react";
import { ActionContracts } from "../../Contracts/ExplorerContracts";
import * as ViewModels from "../../Contracts/ViewModels";
import { useSidePanel } from "../../hooks/useSidePanel";
import Explorer from "../Explorer";
import { SettingsPane } from "../Panes/SettingsPane/SettingsPane";
function generateQueryText(action: ActionContracts.OpenQueryTab, partitionKeyProperty: string): string {
if (!action.query) {
return "SELECT * FROM c";
} else if (action.query.text) {
return action.query.text;
} else if (!!action.query.partitionKeys && action.query.partitionKeys.length > 0) {
let query = "SELECT * FROM c WHERE";
for (let i = 0; i < action.query.partitionKeys.length; i++) {
const partitionKey = action.query.partitionKeys[i];
if (!partitionKey) {
// null partition key case
query = query.concat(` c.${partitionKeyProperty} = ${action.query.partitionKeys[i]}`);
} else if (typeof partitionKey !== "string") {
// Undefined partition key case
query = query.concat(` NOT IS_DEFINED(c.${partitionKeyProperty})`);
} else {
query = query.concat(` c.${partitionKeyProperty} = "${action.query.partitionKeys[i]}"`);
}
if (i !== action.query.partitionKeys.length - 1) {
query = query.concat(" OR");
}
}
return query;
}
return "SELECT * FROM c";
}
function openCollectionTab(
action: ActionContracts.OpenCollectionTab,
databases: ViewModels.Database[],
initialDatabaseIndex = 0
) {
for (let i = initialDatabaseIndex; i < databases.length; i++) {
const database: ViewModels.Database = databases[i];
if (!!action.databaseResourceId && database.id() !== action.databaseResourceId) {
continue;
}
const collectionActionHandler = (collections: ViewModels.Collection[]) => {
if (!action.collectionResourceId && collections.length === 0) {
subscription.dispose();
openCollectionTab(action, databases, ++i);
return;
}
for (let j = 0; j < collections.length; j++) {
const collection: ViewModels.Collection = collections[j];
if (!!action.collectionResourceId && collection.id() !== action.collectionResourceId) {
continue;
}
// select the collection
collection.expandCollection();
if (
action.tabKind === ActionContracts.TabKind.SQLDocuments ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.SQLDocuments]
) {
collection.onDocumentDBDocumentsClick();
break;
}
if (
action.tabKind === ActionContracts.TabKind.MongoDocuments ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.MongoDocuments]
) {
collection.onMongoDBDocumentsClick();
break;
}
if (
action.tabKind === ActionContracts.TabKind.SchemaAnalyzer ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.SchemaAnalyzer]
) {
collection.onSchemaAnalyzerClick();
break;
}
if (
action.tabKind === ActionContracts.TabKind.TableEntities ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.TableEntities]
) {
collection.onTableEntitiesClick();
break;
}
if (
action.tabKind === ActionContracts.TabKind.Graph ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.Graph]
) {
collection.onGraphDocumentsClick();
break;
}
if (
action.tabKind === ActionContracts.TabKind.SQLQuery ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.SQLQuery]
) {
collection.onNewQueryClick(
collection,
undefined,
generateQueryText(action as ActionContracts.OpenQueryTab, collection.partitionKeyProperty)
);
break;
}
if (
action.tabKind === ActionContracts.TabKind.ScaleSettings ||
action.tabKind === ActionContracts.TabKind[ActionContracts.TabKind.ScaleSettings]
) {
collection.onSettingsClick();
break;
}
}
subscription.dispose();
};
const subscription = database.collections.subscribe((collections) => collectionActionHandler(collections));
if (database.collections && database.collections() && database.collections().length) {
collectionActionHandler(database.collections());
}
break;
}
}
function openPane(action: ActionContracts.OpenPane, explorer: Explorer) {
if (
action.paneKind === ActionContracts.PaneKind.AddCollection ||
action.paneKind === ActionContracts.PaneKind[ActionContracts.PaneKind.AddCollection]
) {
explorer.onNewCollectionClicked();
} else if (
action.paneKind === ActionContracts.PaneKind.CassandraAddCollection ||
action.paneKind === ActionContracts.PaneKind[ActionContracts.PaneKind.CassandraAddCollection]
) {
explorer.openCassandraAddCollectionPane();
} else if (
action.paneKind === ActionContracts.PaneKind.GlobalSettings ||
action.paneKind === ActionContracts.PaneKind[ActionContracts.PaneKind.GlobalSettings]
) {
useSidePanel.getState().openSidePanel("Settings", <SettingsPane />);
}
}
export function handleOpenAction(
action: ActionContracts.DataExplorerAction,
databases: ViewModels.Database[],
explorer: Explorer
): boolean {
if (
action.actionType === ActionContracts.ActionType.OpenCollectionTab ||
action.actionType === ActionContracts.ActionType[ActionContracts.ActionType.OpenCollectionTab]
) {
openCollectionTab(action as ActionContracts.OpenCollectionTab, databases);
return true;
}
if (
action.actionType === ActionContracts.ActionType.OpenPane ||
action.actionType === ActionContracts.ActionType[ActionContracts.ActionType.OpenPane]
) {
openPane(action as ActionContracts.OpenPane, explorer);
return true;
}
if (
action.actionType === ActionContracts.ActionType.OpenSampleNotebook ||
action.actionType === ActionContracts.ActionType[ActionContracts.ActionType.OpenSampleNotebook]
) {
openFile(action as ActionContracts.OpenSampleNotebook, explorer);
return true;
}
return false;
}
function openFile(action: ActionContracts.OpenSampleNotebook, explorer: Explorer) {
explorer.handleOpenFileAction(decodeURIComponent(action.path));
}