mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-07-03 10:35:21 +01:00
Add more unit tests
This commit is contained in:
parent
00bd339fb6
commit
aac97d2808
@ -1,13 +1,19 @@
|
|||||||
import { FeedResponse, ItemDefinition, Resource } from "@azure/cosmos";
|
import { FeedResponse, ItemDefinition, Resource } from "@azure/cosmos";
|
||||||
import { TableRowId } from "@fluentui/react-components";
|
import { TableRowId } from "@fluentui/react-components";
|
||||||
|
import { deleteDocuments } from "Common/dataAccess/deleteDocument";
|
||||||
import { Platform, updateConfigContext } from "ConfigContext";
|
import { Platform, updateConfigContext } from "ConfigContext";
|
||||||
import { EditorReactProps } from "Explorer/Controls/Editor/EditorReact";
|
import { EditorReactProps } from "Explorer/Controls/Editor/EditorReact";
|
||||||
import { useCommandBar } from "Explorer/Menus/CommandBar/CommandBarComponentAdapter";
|
import { useCommandBar } from "Explorer/Menus/CommandBar/CommandBarComponentAdapter";
|
||||||
import {
|
import {
|
||||||
ButtonsDependencies,
|
ButtonsDependencies,
|
||||||
|
DELETE_BUTTON_ID,
|
||||||
|
DISCARD_BUTTON_ID,
|
||||||
DocumentsTabComponent,
|
DocumentsTabComponent,
|
||||||
IDocumentsTabComponentProps,
|
IDocumentsTabComponentProps,
|
||||||
NEW_DOCUMENT_BUTTON_ID,
|
NEW_DOCUMENT_BUTTON_ID,
|
||||||
|
SAVE_BUTTON_ID,
|
||||||
|
UPDATE_BUTTON_ID,
|
||||||
|
UPLOAD_BUTTON_ID,
|
||||||
buildQuery,
|
buildQuery,
|
||||||
getDeleteExistingDocumentButtonState,
|
getDeleteExistingDocumentButtonState,
|
||||||
getDiscardExistingDocumentChangesButtonState,
|
getDiscardExistingDocumentChangesButtonState,
|
||||||
@ -21,7 +27,7 @@ import { ReactWrapper, ShallowWrapper, mount, shallow } from "enzyme";
|
|||||||
import * as ko from "knockout";
|
import * as ko from "knockout";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { act } from "react-dom/test-utils";
|
import { act } from "react-dom/test-utils";
|
||||||
import { DatabaseAccount } from "../../../Contracts/DataModels";
|
import { DatabaseAccount, DocumentId } from "../../../Contracts/DataModels";
|
||||||
import * as ViewModels from "../../../Contracts/ViewModels";
|
import * as ViewModels from "../../../Contracts/ViewModels";
|
||||||
import { updateUserContext } from "../../../UserContext";
|
import { updateUserContext } from "../../../UserContext";
|
||||||
import Explorer from "../../Explorer";
|
import Explorer from "../../Explorer";
|
||||||
@ -47,12 +53,13 @@ jest.mock("Common/dataAccess/queryDocuments", () => ({
|
|||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
const PROPERTY_VALUE = "__SOME_PROPERTY_VALUE__";
|
||||||
jest.mock("Common/dataAccess/readDocument", () => ({
|
jest.mock("Common/dataAccess/readDocument", () => ({
|
||||||
readDocument: jest.fn(() =>
|
readDocument: jest.fn(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
container: undefined,
|
container: undefined,
|
||||||
id: "id",
|
id: "id",
|
||||||
url: "url",
|
property: PROPERTY_VALUE,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
}));
|
}));
|
||||||
@ -61,7 +68,21 @@ jest.mock("Explorer/Controls/Editor/EditorReact", () => ({
|
|||||||
EditorReact: (props: EditorReactProps) => <>{props.content}</>,
|
EditorReact: (props: EditorReactProps) => <>{props.content}</>,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
async function waitForComponentToPaint<P = unknown>(wrapper: ReactWrapper<P>, amount = 0) {
|
jest.mock("Explorer/Controls/Dialog", () => ({
|
||||||
|
useDialog: {
|
||||||
|
getState: jest.fn(() => ({
|
||||||
|
showOkCancelModalDialog: (title: string, subText: string, okLabel: string, onOk: () => void) => onOk(),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock("Common/dataAccess/deleteDocument", () => ({
|
||||||
|
deleteDocuments: jest.fn((collection: ViewModels.CollectionBase, documentIds: DocumentId[]) =>
|
||||||
|
Promise.resolve(documentIds),
|
||||||
|
),
|
||||||
|
}));
|
||||||
|
|
||||||
|
async function waitForComponentToPaint<P = unknown>(wrapper: ReactWrapper<P> | ShallowWrapper<P>, amount = 0) {
|
||||||
let newWrapper;
|
let newWrapper;
|
||||||
await act(async () => {
|
await act(async () => {
|
||||||
await new Promise((resolve) => setTimeout(resolve, amount));
|
await new Promise((resolve) => setTimeout(resolve, amount));
|
||||||
@ -354,9 +375,8 @@ describe("Documents tab", () => {
|
|||||||
|
|
||||||
let wrapper: ShallowWrapper;
|
let wrapper: ShallowWrapper;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(async () => {
|
||||||
const props: IDocumentsTabComponentProps = createMockProps();
|
const props: IDocumentsTabComponentProps = createMockProps();
|
||||||
|
|
||||||
wrapper = shallow(<DocumentsTabComponent {...props} />);
|
wrapper = shallow(<DocumentsTabComponent {...props} />);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -427,13 +447,51 @@ describe("Documents tab", () => {
|
|||||||
wrapper.unmount();
|
wrapper.unmount();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("clicking on New Document should show editor with new document", async () => {
|
it("renders by default the first document", async () => {
|
||||||
useCommandBar
|
expect(wrapper.findWhere((node) => node.text().includes(PROPERTY_VALUE)).exists()).toBeTruthy();
|
||||||
.getState()
|
});
|
||||||
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
|
|
||||||
.onCommandClick(undefined);
|
it("default buttons", async () => {
|
||||||
wrapper = await waitForComponentToPaint(wrapper);
|
expect(useCommandBar.getState().contextButtons.find((button) => button.id === UPDATE_BUTTON_ID)).toBeDefined();
|
||||||
|
expect(useCommandBar.getState().contextButtons.find((button) => button.id === DISCARD_BUTTON_ID)).toBeDefined();
|
||||||
|
expect(useCommandBar.getState().contextButtons.find((button) => button.id === DELETE_BUTTON_ID)).toBeDefined();
|
||||||
|
expect(useCommandBar.getState().contextButtons.find((button) => button.id === UPLOAD_BUTTON_ID)).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clicking on New Document should show editor with new document", () => {
|
||||||
|
act(() => {
|
||||||
|
useCommandBar
|
||||||
|
.getState()
|
||||||
|
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
|
||||||
|
.onCommandClick(undefined);
|
||||||
|
});
|
||||||
expect(wrapper.findWhere((node) => node.text().includes("replace_with_new_document_id")).exists()).toBeTruthy();
|
expect(wrapper.findWhere((node) => node.text().includes("replace_with_new_document_id")).exists()).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("clicking on New Document should show Save and Discard buttons", () => {
|
||||||
|
act(() => {
|
||||||
|
useCommandBar
|
||||||
|
.getState()
|
||||||
|
.contextButtons.find((button) => button.id === NEW_DOCUMENT_BUTTON_ID)
|
||||||
|
.onCommandClick(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(useCommandBar.getState().contextButtons.find((button) => button.id === SAVE_BUTTON_ID)).toBeDefined();
|
||||||
|
expect(useCommandBar.getState().contextButtons.find((button) => button.id === DISCARD_BUTTON_ID)).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clicking Delete Document asks for confirmation", () => {
|
||||||
|
const mockDeleteDocuments = deleteDocuments as jest.Mock;
|
||||||
|
mockDeleteDocuments.mockClear();
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
useCommandBar
|
||||||
|
.getState()
|
||||||
|
.contextButtons.find((button) => button.id === DELETE_BUTTON_ID)
|
||||||
|
.onCommandClick(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockDeleteDocuments).toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -236,7 +236,7 @@ export type ButtonsDependencies = {
|
|||||||
const createUploadButton = (container: Explorer): CommandButtonComponentProps => {
|
const createUploadButton = (container: Explorer): CommandButtonComponentProps => {
|
||||||
const label = "Upload Item";
|
const label = "Upload Item";
|
||||||
return {
|
return {
|
||||||
id: "uploadItemBtn",
|
id: UPLOAD_BUTTON_ID,
|
||||||
iconSrc: UploadIcon,
|
iconSrc: UploadIcon,
|
||||||
iconAlt: label,
|
iconAlt: label,
|
||||||
onCommandClick: () => {
|
onCommandClick: () => {
|
||||||
@ -254,6 +254,11 @@ const createUploadButton = (container: Explorer): CommandButtonComponentProps =>
|
|||||||
|
|
||||||
// Export to expose to unit tests
|
// Export to expose to unit tests
|
||||||
export const NEW_DOCUMENT_BUTTON_ID = "mongoNewDocumentBtn";
|
export const NEW_DOCUMENT_BUTTON_ID = "mongoNewDocumentBtn";
|
||||||
|
export const SAVE_BUTTON_ID = "saveBtn";
|
||||||
|
export const UPDATE_BUTTON_ID = "updateBtn";
|
||||||
|
export const DISCARD_BUTTON_ID = "discardBtn";
|
||||||
|
export const DELETE_BUTTON_ID = "deleteBtn";
|
||||||
|
export const UPLOAD_BUTTON_ID = "uploadItemBtn";
|
||||||
|
|
||||||
// Export to expose in unit tests
|
// Export to expose in unit tests
|
||||||
export const getTabsButtons = ({
|
export const getTabsButtons = ({
|
||||||
@ -304,6 +309,7 @@ export const getTabsButtons = ({
|
|||||||
disabled:
|
disabled:
|
||||||
!getSaveNewDocumentButtonState(editorState).enabled ||
|
!getSaveNewDocumentButtonState(editorState).enabled ||
|
||||||
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
||||||
|
id: SAVE_BUTTON_ID,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,6 +326,7 @@ export const getTabsButtons = ({
|
|||||||
disabled:
|
disabled:
|
||||||
!getDiscardNewDocumentChangesButtonState(editorState).enabled ||
|
!getDiscardNewDocumentChangesButtonState(editorState).enabled ||
|
||||||
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
||||||
|
id: DISCARD_BUTTON_ID,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,6 +343,7 @@ export const getTabsButtons = ({
|
|||||||
disabled:
|
disabled:
|
||||||
!getSaveExistingDocumentButtonState(editorState).enabled ||
|
!getSaveExistingDocumentButtonState(editorState).enabled ||
|
||||||
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
||||||
|
id: UPDATE_BUTTON_ID,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -352,6 +360,7 @@ export const getTabsButtons = ({
|
|||||||
disabled:
|
disabled:
|
||||||
!getDiscardExistingDocumentChangesButtonState(editorState).enabled ||
|
!getDiscardExistingDocumentChangesButtonState(editorState).enabled ||
|
||||||
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
||||||
|
id: DISCARD_BUTTON_ID,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,6 +377,7 @@ export const getTabsButtons = ({
|
|||||||
disabled:
|
disabled:
|
||||||
!getDeleteExistingDocumentButtonState(editorState, selectedRows).enabled ||
|
!getDeleteExistingDocumentButtonState(editorState, selectedRows).enabled ||
|
||||||
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
useSelectedNode.getState().isQueryCopilotCollectionSelected(),
|
||||||
|
id: DELETE_BUTTON_ID,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user