|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. |
| 3 | + * See LICENSE.md in the project root for license terms and full copyright notice. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +/* eslint-disable import/no-duplicates */ |
| 6 | +/* eslint-disable @typescript-eslint/no-shadow */ |
| 7 | + |
| 8 | +import { expect } from "chai"; |
| 9 | +import { useCallback } from "react"; |
| 10 | +import sinon from "sinon"; |
| 11 | +import { UiFramework } from "@itwin/appui-react"; |
| 12 | +import { IModelApp } from "@itwin/core-frontend"; |
| 13 | +import { useModelsTree, VisibilityTree, VisibilityTreeRenderer } from "@itwin/tree-widget-react"; |
| 14 | +import { createStorage } from "@itwin/unified-selection"; |
| 15 | +import { cleanup, render, waitFor } from "@testing-library/react"; |
| 16 | +import { buildIModel, insertPhysicalElement, insertPhysicalModelWithPartition, insertSpatialCategory } from "../../utils/IModelUtils.js"; |
| 17 | +import { initializeLearningSnippetsTests, terminateLearningSnippetsTests } from "../../utils/InitializationUtils.js"; |
| 18 | +import { getSchemaContext, getTestViewer, mockGetBoundingClientRect, TreeWidgetTestUtils } from "../../utils/TreeWidgetTestUtils.js"; |
| 19 | + |
| 20 | +import type { SelectionStorage } from "@itwin/unified-selection"; |
| 21 | +import type { IModelConnection, Viewport } from "@itwin/core-frontend"; |
| 22 | +import type { InstanceKey } from "@itwin/presentation-common"; |
| 23 | +import type { Props } from "@itwin/presentation-shared"; |
| 24 | + |
| 25 | +// __PUBLISH_EXTRACT_START__ TreeWidget.GetSubTreePathsComponentWithTargetItemsExample |
| 26 | +type UseModelsTreeProps = Props<typeof useModelsTree>; |
| 27 | +type GetSubTreePathsType = NonNullable<UseModelsTreeProps["getSubTreePaths"]>; |
| 28 | + |
| 29 | +function CustomModelsTreeComponentWithTargetItems({ |
| 30 | + viewport, |
| 31 | + selectionStorage, |
| 32 | + imodel, |
| 33 | + targetItems, |
| 34 | +}: { |
| 35 | + viewport: Viewport; |
| 36 | + selectionStorage: SelectionStorage; |
| 37 | + imodel: IModelConnection; |
| 38 | + targetItems: InstanceKey[]; |
| 39 | +}) { |
| 40 | + const getSubTreePaths = useCallback<GetSubTreePathsType>( |
| 41 | + async ({ createInstanceKeyPaths }) => { |
| 42 | + return createInstanceKeyPaths({ |
| 43 | + // List of instance keys representing nodes that should be part of the hierarchy. |
| 44 | + // Only these nodes, their ancestors and children will be part of that hierarchy. |
| 45 | + targetItems, |
| 46 | + }); |
| 47 | + }, |
| 48 | + [targetItems], |
| 49 | + ); |
| 50 | + |
| 51 | + const { modelsTreeProps, rendererProps } = useModelsTree({ activeView: viewport, getSubTreePaths }); |
| 52 | + |
| 53 | + return ( |
| 54 | + <VisibilityTree |
| 55 | + {...modelsTreeProps} |
| 56 | + getSchemaContext={getSchemaContext} |
| 57 | + selectionStorage={selectionStorage} |
| 58 | + imodel={imodel} |
| 59 | + treeRenderer={(props) => <VisibilityTreeRenderer {...props} {...rendererProps} />} |
| 60 | + /> |
| 61 | + ); |
| 62 | +} |
| 63 | +// __PUBLISH_EXTRACT_END__ |
| 64 | + |
| 65 | +describe("Tree widget", () => { |
| 66 | + mockGetBoundingClientRect(); |
| 67 | + describe("Learning snippets", () => { |
| 68 | + describe("Components", () => { |
| 69 | + describe("SubTree paths", () => { |
| 70 | + before(async function () { |
| 71 | + await initializeLearningSnippetsTests(); |
| 72 | + await TreeWidgetTestUtils.initialize(); |
| 73 | + }); |
| 74 | + |
| 75 | + after(async function () { |
| 76 | + await terminateLearningSnippetsTests(); |
| 77 | + TreeWidgetTestUtils.terminate(); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach(async () => { |
| 81 | + sinon.restore(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("renders custom models tree component with filtered paths using targetItems", async function () { |
| 85 | + const imodel = await buildIModel(this, async (builder) => { |
| 86 | + const physicalModel = insertPhysicalModelWithPartition({ builder, codeValue: "TestPhysicalModel" }); |
| 87 | + const physicalModel2 = insertPhysicalModelWithPartition({ builder, codeValue: "TestPhysicalModel 2" }); |
| 88 | + const category = insertSpatialCategory({ builder, codeValue: "Test SpatialCategory" }); |
| 89 | + insertPhysicalElement({ builder, modelId: physicalModel.id, categoryId: category.id }); |
| 90 | + const category2 = insertSpatialCategory({ builder, codeValue: "Test SpatialCategory 2" }); |
| 91 | + insertPhysicalElement({ builder, modelId: physicalModel2.id, categoryId: category2.id }); |
| 92 | + return { physicalModel, physicalModel2 }; |
| 93 | + }); |
| 94 | + const testViewport = getTestViewer(imodel.imodel, true); |
| 95 | + const unifiedSelectionStorage = createStorage(); |
| 96 | + sinon.stub(IModelApp.viewManager, "selectedView").get(() => testViewport); |
| 97 | + sinon.stub(UiFramework, "getIModelConnection").returns(imodel.imodel); |
| 98 | + |
| 99 | + using _ = { [Symbol.dispose]: cleanup }; |
| 100 | + const { getByText, queryByText } = render( |
| 101 | + <CustomModelsTreeComponentWithTargetItems |
| 102 | + selectionStorage={unifiedSelectionStorage} |
| 103 | + imodel={imodel.imodel} |
| 104 | + viewport={testViewport} |
| 105 | + targetItems={[imodel.physicalModel]} |
| 106 | + />, |
| 107 | + ); |
| 108 | + |
| 109 | + await waitFor(() => { |
| 110 | + getByText("TestPhysicalModel"); |
| 111 | + expect(queryByText("TestPhysicalModel 2")).to.be.null; |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments