From 75719b3cf07cd90ae851cb19b5b6b77d574f7732 Mon Sep 17 00:00:00 2001 From: Nishtha Ahuja <45535788+nishthaAhujaa@users.noreply.github.com> Date: Thu, 13 Mar 2025 11:01:34 +0530 Subject: [PATCH] test files (#2074) Co-authored-by: nishthaAhujaa --- .../MaterializedViewComponent.test.tsx | 45 +++++++++++++++++++ .../MaterializedViewSourceComponent.test.tsx | 39 ++++++++++++++++ .../MaterializedViewTargetComponent.test.tsx | 31 +++++++++++++ src/Explorer/Controls/Settings/TestUtils.tsx | 9 ++++ 4 files changed, 124 insertions(+) create mode 100644 src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewComponent.test.tsx create mode 100644 src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.test.tsx create mode 100644 src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewTargetComponent.test.tsx diff --git a/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewComponent.test.tsx b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewComponent.test.tsx new file mode 100644 index 000000000..861761d21 --- /dev/null +++ b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewComponent.test.tsx @@ -0,0 +1,45 @@ +import { shallow } from "enzyme"; +import React from "react"; +import { collection } from "../TestUtils"; +import { MaterializedViewComponent } from "./MaterializedViewComponent"; +import { MaterializedViewSourceComponent } from "./MaterializedViewSourceComponent"; +import { MaterializedViewTargetComponent } from "./MaterializedViewTargetComponent"; + +describe("MaterializedViewComponent", () => { + let testCollection: typeof collection; + + beforeEach(() => { + testCollection = { ...collection }; + }); + + it("renders only the source component when materializedViewDefinition is missing", () => { + testCollection.materializedViews([ + { id: "view1", _rid: "rid1" }, + { id: "view2", _rid: "rid2" }, + ]); + testCollection.materializedViewDefinition(null); + const wrapper = shallow(); + expect(wrapper.find(MaterializedViewSourceComponent).exists()).toBe(true); + expect(wrapper.find(MaterializedViewTargetComponent).exists()).toBe(false); + }); + + it("renders only the target component when materializedViews is missing", () => { + testCollection.materializedViews(null); + testCollection.materializedViewDefinition({ + definition: "SELECT * FROM c WHERE c.id = 1", + sourceCollectionId: "source1", + sourceCollectionRid: "rid123", + }); + const wrapper = shallow(); + expect(wrapper.find(MaterializedViewSourceComponent).exists()).toBe(false); + expect(wrapper.find(MaterializedViewTargetComponent).exists()).toBe(true); + }); + + it("renders neither component when both are missing", () => { + testCollection.materializedViews(null); + testCollection.materializedViewDefinition(null); + const wrapper = shallow(); + expect(wrapper.find(MaterializedViewSourceComponent).exists()).toBe(false); + expect(wrapper.find(MaterializedViewTargetComponent).exists()).toBe(false); + }); +}); diff --git a/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.test.tsx b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.test.tsx new file mode 100644 index 000000000..ac8dda2c2 --- /dev/null +++ b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.test.tsx @@ -0,0 +1,39 @@ +import { PrimaryButton } from "@fluentui/react"; +import { shallow } from "enzyme"; +import React from "react"; +import { collection } from "../TestUtils"; +import { + MaterializedViewSourceComponent, + MaterializedViewSourceComponentProps, +} from "./MaterializedViewSourceComponent"; + +describe("MaterializedViewSourceComponent", () => { + const baseProps: MaterializedViewSourceComponentProps = { + collection: { + ...collection, + materializedViews: jest.fn(() => collection.materializedViews()), + }, + }; + + it("renders without crashing", () => { + const wrapper = shallow(); + expect(wrapper.exists()).toBe(true); + }); + + it("renders the PrimaryButton", () => { + const wrapper = shallow(); + expect(wrapper.find(PrimaryButton).exists()).toBe(true); + }); + + it("updates when new materialized views are provided", () => { + const wrapper = shallow(); + + // Simulating an update by modifying the observable directly + collection.materializedViews([{ id: "view3", _rid: "rid3" }]); + + wrapper.setProps({ collection: { ...collection } }); + wrapper.update(); + + expect(wrapper.find(PrimaryButton).exists()).toBe(true); + }); +}); diff --git a/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewTargetComponent.test.tsx b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewTargetComponent.test.tsx new file mode 100644 index 000000000..1a6e3cab8 --- /dev/null +++ b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewTargetComponent.test.tsx @@ -0,0 +1,31 @@ +import { Text } from "@fluentui/react"; +import { shallow } from "enzyme"; +import React from "react"; +import { collection } from "../TestUtils"; +import { MaterializedViewTargetComponent } from "./MaterializedViewTargetComponent"; + +describe("MaterializedViewTargetComponent", () => { + let testCollection: any; + + beforeEach(() => { + testCollection = { + ...collection, + materializedViewDefinition: collection.materializedViewDefinition, + }; + }); + + it("renders without crashing", () => { + const wrapper = shallow(); + expect(wrapper.exists()).toBe(true); + }); + + it("displays the source container ID", () => { + const wrapper = shallow(); + expect(wrapper.find(Text).at(2).dive().text()).toBe("source1"); + }); + + it("displays the materialized view definition", () => { + const wrapper = shallow(); + expect(wrapper.find(Text).at(4).dive().text()).toBe("SELECT * FROM c WHERE c.id = 1"); + }); +}); diff --git a/src/Explorer/Controls/Settings/TestUtils.tsx b/src/Explorer/Controls/Settings/TestUtils.tsx index c158e5cba..71d939584 100644 --- a/src/Explorer/Controls/Settings/TestUtils.tsx +++ b/src/Explorer/Controls/Settings/TestUtils.tsx @@ -48,6 +48,15 @@ export const collection = { ]), vectorEmbeddingPolicy: ko.observable({} as DataModels.VectorEmbeddingPolicy), fullTextPolicy: ko.observable({} as DataModels.FullTextPolicy), + materializedViews: ko.observable([ + { id: "view1", _rid: "rid1" }, + { id: "view2", _rid: "rid2" }, + ]), + materializedViewDefinition: ko.observable({ + definition: "SELECT * FROM c WHERE c.id = 1", + sourceCollectionId: "source1", + sourceCollectionRid: "rid123", + }), readSettings: () => { return; },