From 482e245b4ef92348c973845774a3e64a5de5a474 Mon Sep 17 00:00:00 2001 From: Laurent Nguyen Date: Tue, 7 May 2024 11:52:35 +0200 Subject: [PATCH] Move unit tests to correct file --- .../DocumentsTabV2/DocumentsTabV2.test.tsx | 190 ++++++++++++++++++ .../DocumentsTableComponent.test.tsx | 124 ------------ 2 files changed, 190 insertions(+), 124 deletions(-) diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.test.tsx b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.test.tsx index 2755c207c..f3b05606e 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.test.tsx +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.test.tsx @@ -1,7 +1,16 @@ +import { TableRowId } from "@fluentui/react-components"; +import { Platform, updateConfigContext } from "ConfigContext"; import { + ButtonsDependencies, DocumentsTabComponent, IDocumentsTabComponentProps, buildQuery, + getDeleteExistingDocumentButtonState, + getDiscardExistingDocumentChangesButtonState, + getDiscardNewDocumentChangesButtonState, + getSaveExistingDocumentButtonState, + getSaveNewDocumentButtonState, + getTabsButtons, showPartitionKey, } from "Explorer/Tabs/DocumentsTabV2/DocumentsTabV2"; import { ShallowWrapper, shallow } from "enzyme"; @@ -96,6 +105,187 @@ describe("Documents tab", () => { }); }); + describe("when getting command bar button state", () => { + describe("should set Save New Document state", () => { + const testCases = new Set<{ state: ViewModels.DocumentExplorerState; enabled: boolean; visible: boolean }>(); + testCases.add({ state: ViewModels.DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentValid, enabled: true, visible: true }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentInvalid, enabled: false, visible: true }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentNoEdits, + enabled: false, + visible: false, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyValid, + enabled: false, + visible: false, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyInvalid, + enabled: false, + visible: false, + }); + + testCases.forEach((testCase) => { + const state = getSaveNewDocumentButtonState(testCase.state); + it(`enable for ${testCase.state}`, () => { + expect(state.enabled).toBe(testCase.enabled); + }); + it(`visible for ${testCase.state}`, () => { + expect(state.visible).toBe(testCase.visible); + }); + }); + }); + + describe("should set Discard New Document state", () => { + const testCases = new Set<{ state: ViewModels.DocumentExplorerState; enabled: boolean; visible: boolean }>(); + testCases.add({ state: ViewModels.DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentValid, enabled: true, visible: true }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentInvalid, enabled: true, visible: true }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentNoEdits, + enabled: false, + visible: false, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyValid, + enabled: false, + visible: false, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyInvalid, + enabled: false, + visible: false, + }); + + testCases.forEach((testCase) => { + const state = getDiscardNewDocumentChangesButtonState(testCase.state); + it(`enable for ${testCase.state}`, () => { + expect(state.enabled).toBe(testCase.enabled); + }); + it(`visible for ${testCase.state}`, () => { + expect(state.visible).toBe(testCase.visible); + }); + }); + }); + + describe("should set Save Existing Document state", () => { + const testCases = new Set<{ state: ViewModels.DocumentExplorerState; enabled: boolean; visible: boolean }>(); + testCases.add({ state: ViewModels.DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentValid, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentInvalid, enabled: false, visible: false }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentNoEdits, + enabled: false, + visible: true, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyValid, + enabled: true, + visible: true, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyInvalid, + enabled: false, + visible: true, + }); + + testCases.forEach((testCase) => { + const state = getSaveExistingDocumentButtonState(testCase.state); + it(`enable for ${testCase.state}`, () => { + expect(state.enabled).toBe(testCase.enabled); + }); + it(`visible for ${testCase.state}`, () => { + expect(state.visible).toBe(testCase.visible); + }); + }); + }); + + describe("should set Discard Existing Document state", () => { + const testCases = new Set<{ state: ViewModels.DocumentExplorerState; enabled: boolean; visible: boolean }>(); + testCases.add({ state: ViewModels.DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentValid, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentInvalid, enabled: false, visible: false }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentNoEdits, + enabled: false, + visible: true, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyValid, + enabled: true, + visible: true, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyInvalid, + enabled: true, + visible: true, + }); + + testCases.forEach((testCase) => { + const state = getDiscardExistingDocumentChangesButtonState(testCase.state); + it(`enable for ${testCase.state}`, () => { + expect(state.enabled).toBe(testCase.enabled); + }); + it(`visible for ${testCase.state}`, () => { + expect(state.visible).toBe(testCase.visible); + }); + }); + }); + + describe("should set Delete Existing Document state", () => { + const testCases = new Set<{ state: ViewModels.DocumentExplorerState; enabled: boolean; visible: boolean }>(); + testCases.add({ state: ViewModels.DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentValid, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.newDocumentInvalid, enabled: false, visible: false }); + testCases.add({ state: ViewModels.DocumentExplorerState.exisitingDocumentNoEdits, enabled: true, visible: true }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyValid, + enabled: true, + visible: true, + }); + testCases.add({ + state: ViewModels.DocumentExplorerState.exisitingDocumentDirtyInvalid, + enabled: true, + visible: true, + }); + + testCases.forEach((testCase) => { + const state = getDeleteExistingDocumentButtonState(testCase.state, new Set()); + it(`enable for ${testCase.state} (no selected rows)`, () => { + expect(state.enabled).toBe(testCase.enabled); + }); + it(`visible for ${testCase.state} (no selected rows)`, () => { + expect(state.visible).toBe(false); + }); + + // state = getDeleteExistingDocumentButtonState(testCase.state, new Set([2, 1])); + // it(`enable for ${testCase.state} (2 selected rows)`, () => { + // expect(state.enabled).toBe(testCase.enabled); + // }); + // it(`visible for ${testCase.state} (2 selected rows)`, () => { + // expect(state.visible).toBe(testCase.visible); + // }); + }); + }); + }); + + it("Do not get tabs button for Fabric readonly", () => { + updateConfigContext({ platform: Platform.Fabric }); + updateUserContext({ + fabricContext: { + connectionId: "test", + databaseConnectionInfo: undefined, + isReadOnly: true, + isVisible: true, + }, + }); + + const buttons = getTabsButtons({} as ButtonsDependencies); + expect(buttons.length).toBe(0); + }); + describe("when rendered", () => { const createMockProps = (): IDocumentsTabComponentProps => ({ isPreferredApiMongoDB: false, diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.test.tsx b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.test.tsx index 040fb3fce..3505fd78b 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.test.tsx +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.test.tsx @@ -1,14 +1,4 @@ import { TableRowId } from "@fluentui/react-components"; -import { DocumentExplorerState } from "Contracts/ViewModels"; -import { - ButtonsDependencies, - getDeleteExistingDocumentButtonState, - getDiscardExistingDocumentChangesButtonState, - getDiscardNewDocumentChangesButtonState, - getSaveExistingDocumentButtonState, - getSaveNewDocumentButtonState, - getTabsButtons, -} from "Explorer/Tabs/DocumentsTabV2/DocumentsTabV2"; import { ReactWrapper, mount } from "enzyme"; import React from "react"; import { DocumentsTableComponent, IDocumentsTableComponentProps } from "./DocumentsTableComponent"; @@ -40,120 +30,6 @@ describe("DocumentsTableComponent", () => { }, }); - describe("when getting command bar button state", () => { - describe("should set Save New Document state", () => { - const testCases = new Set<{ state: DocumentExplorerState; enabled: boolean; visible: boolean }>(); - testCases.add({ state: DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentValid, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.newDocumentInvalid, enabled: false, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentNoEdits, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyValid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyInvalid, enabled: false, visible: false }); - - testCases.forEach((testCase) => { - const state = getSaveNewDocumentButtonState(testCase.state); - it(`enable for ${testCase.state}`, () => { - expect(state.enabled).toBe(testCase.enabled); - }); - it(`visible for ${testCase.state}`, () => { - expect(state.visible).toBe(testCase.visible); - }); - }); - }); - - describe("should set Discard New Document state", () => { - const testCases = new Set<{ state: DocumentExplorerState; enabled: boolean; visible: boolean }>(); - testCases.add({ state: DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentValid, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.newDocumentInvalid, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentNoEdits, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyValid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyInvalid, enabled: false, visible: false }); - - testCases.forEach((testCase) => { - const state = getDiscardNewDocumentChangesButtonState(testCase.state); - it(`enable for ${testCase.state}`, () => { - expect(state.enabled).toBe(testCase.enabled); - }); - it(`visible for ${testCase.state}`, () => { - expect(state.visible).toBe(testCase.visible); - }); - }); - }); - - describe("should set Save Existing Document state", () => { - const testCases = new Set<{ state: DocumentExplorerState; enabled: boolean; visible: boolean }>(); - testCases.add({ state: DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentValid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentInvalid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentNoEdits, enabled: false, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyValid, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyInvalid, enabled: false, visible: true }); - - testCases.forEach((testCase) => { - const state = getSaveExistingDocumentButtonState(testCase.state); - it(`enable for ${testCase.state}`, () => { - expect(state.enabled).toBe(testCase.enabled); - }); - it(`visible for ${testCase.state}`, () => { - expect(state.visible).toBe(testCase.visible); - }); - }); - }); - - describe("should set Discard Existing Document state", () => { - const testCases = new Set<{ state: DocumentExplorerState; enabled: boolean; visible: boolean }>(); - testCases.add({ state: DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentValid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentInvalid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentNoEdits, enabled: false, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyValid, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyInvalid, enabled: true, visible: true }); - - testCases.forEach((testCase) => { - const state = getDiscardExistingDocumentChangesButtonState(testCase.state); - it(`enable for ${testCase.state}`, () => { - expect(state.enabled).toBe(testCase.enabled); - }); - it(`visible for ${testCase.state}`, () => { - expect(state.visible).toBe(testCase.visible); - }); - }); - }); - - describe("should set Delete Existing Document state", () => { - const testCases = new Set<{ state: DocumentExplorerState; enabled: boolean; visible: boolean }>(); - testCases.add({ state: DocumentExplorerState.noDocumentSelected, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentValid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.newDocumentInvalid, enabled: false, visible: false }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentNoEdits, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyValid, enabled: true, visible: true }); - testCases.add({ state: DocumentExplorerState.exisitingDocumentDirtyInvalid, enabled: true, visible: true }); - - testCases.forEach((testCase) => { - const state = getDeleteExistingDocumentButtonState(testCase.state, new Set()); - it(`enable for ${testCase.state} (no selected rows)`, () => { - expect(state.enabled).toBe(testCase.enabled); - }); - it(`visible for ${testCase.state} (no selected rows)`, () => { - expect(state.visible).toBe(false); - }); - - // state = getDeleteExistingDocumentButtonState(testCase.state, new Set([2, 1])); - // it(`enable for ${testCase.state} (2 selected rows)`, () => { - // expect(state.enabled).toBe(testCase.enabled); - // }); - // it(`visible for ${testCase.state} (2 selected rows)`, () => { - // expect(state.visible).toBe(testCase.visible); - // }); - }); - }); - }); - - describe("Do not get tabs button for Fabric readonly", () => { - const buttons = getTabsButtons({} as ButtonsDependencies); - }); - describe("when rendered", () => { let wrapper: ReactWrapper; beforeEach(() => {