-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathCategoriesTree.test.tsx
193 lines (177 loc) · 8.63 KB
/
CategoriesTree.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable import/no-duplicates */
/* eslint-disable @typescript-eslint/no-shadow */
import { join } from "path";
import { useCallback } from "react";
import sinon from "sinon";
import { UiFramework } from "@itwin/appui-react";
import { IModelReadRpcInterface, SnapshotIModelRpcInterface } from "@itwin/core-common";
import { IModelApp, NoRenderApp } from "@itwin/core-frontend";
import { ECSchemaRpcInterface } from "@itwin/ecschema-rpcinterface-common";
import { ECSchemaRpcImpl } from "@itwin/ecschema-rpcinterface-impl";
import { PresentationRpcInterface } from "@itwin/presentation-common";
import { HierarchyCacheMode, initialize as initializePresentationTesting, terminate as terminatePresentationTesting } from "@itwin/presentation-testing";
// __PUBLISH_EXTRACT_START__ Presentation.TreeWidget.CategoriesTreeExampleImports
import { CategoriesTreeComponent } from "@itwin/tree-widget-react";
// __PUBLISH_EXTRACT_END__
// __PUBLISH_EXTRACT_START__ Presentation.TreeWidget.CustomCategoriesTreeExampleImports
import {
TreeWithHeader,
useCategoriesTree,
useCategoriesTreeButtonProps,
VisibilityTree,
VisibilityTreeRenderer,
} from "@itwin/tree-widget-react";
import type { IModelConnection, Viewport } from "@itwin/core-frontend";
import type { SelectionStorage } from "@itwin/unified-selection";
import type { SchemaContext } from "@itwin/ecschema-metadata";
import type { ComponentPropsWithoutRef } from "react";
// __PUBLISH_EXTRACT_END__
import { createStorage } from "@itwin/unified-selection";
import { cleanup, render, waitFor } from "@testing-library/react";
import { buildIModel, insertPhysicalElement, insertPhysicalModelWithPartition, insertSpatialCategory } from "../../utils/IModelUtils";
import { getSchemaContext, getTestViewer, mockGetBoundingClientRect, TestUtils } from "../../utils/TestUtils";
describe("Tree widget", () => {
describe("Learning snippets", () => {
describe("Components", () => {
describe("Categories tree", () => {
before(async function () {
await initializePresentationTesting({
backendProps: {
caching: {
hierarchies: {
mode: HierarchyCacheMode.Memory,
},
},
},
testOutputDir: join(__dirname, "output"),
backendHostProps: {
cacheDir: join(__dirname, "cache"),
},
rpcs: [SnapshotIModelRpcInterface, IModelReadRpcInterface, PresentationRpcInterface, ECSchemaRpcInterface],
});
// eslint-disable-next-line @itwin/no-internal
ECSchemaRpcImpl.register();
});
after(async function () {
await terminatePresentationTesting();
});
beforeEach(async () => {
await NoRenderApp.startup();
await TestUtils.initialize();
});
afterEach(async () => {
TestUtils.terminate();
await IModelApp.shutdown();
sinon.restore();
});
it.only("Renders <CategoriesTreeComponent />", async function () {
const imodel = (
await buildIModel(this, async (builder) => {
const physicalModel = insertPhysicalModelWithPartition({ builder, codeValue: "TestPhysicalModel" });
const category = insertSpatialCategory({ builder, codeValue: "Test SpatialCategory" });
insertPhysicalElement({ builder, modelId: physicalModel.id, categoryId: category.id });
return { category };
})
).imodel;
const testViewport = getTestViewer(imodel);
const unifiedSelectionStorage = createStorage();
sinon.stub(IModelApp.viewManager, "selectedView").get(() => testViewport);
sinon.stub(UiFramework, "getIModelConnection").returns(imodel);
mockGetBoundingClientRect();
// __PUBLISH_EXTRACT_START__ Presentation.TreeWidget.CategoriesTreeExample
function MyWidget() {
return (
<CategoriesTreeComponent
// see "Creating schema context" section for example implementation
getSchemaContext={getSchemaContext}
// see "Creating unified selection storage" section for example implementation
selectionStorage={unifiedSelectionStorage}
headerButtons={[
(props) => <CategoriesTreeComponent.ShowAllButton {...props} />,
(props) => <CategoriesTreeComponent.HideAllButton {...props} />,
]}
/>
);
}
// __PUBLISH_EXTRACT_END__
const { getByText } = render(<MyWidget />);
await waitFor(() => getByText("Test SpatialCategory"));
cleanup();
});
it("Renders custom categories tree", async function () {
const imodelConnection = (
await buildIModel(this, async (builder) => {
const physicalModel = insertPhysicalModelWithPartition({ builder, codeValue: "TestPhysicalModel" });
const category = insertSpatialCategory({ builder, codeValue: "Test SpatialCategory" });
insertPhysicalElement({ builder, modelId: physicalModel.id, categoryId: category.id });
return { category };
})
).imodel;
const testViewport = getTestViewer(imodelConnection);
const unifiedSelectionStorage = createStorage();
sinon.stub(IModelApp.viewManager, "selectedView").get(() => testViewport);
sinon.stub(UiFramework, "getIModelConnection").returns(imodelConnection);
mockGetBoundingClientRect();
// __PUBLISH_EXTRACT_START__ Presentation.TreeWidget.CustomCategoriesTreeExample
type VisibilityTreeRendererProps = ComponentPropsWithoutRef<typeof VisibilityTreeRenderer>;
type CustomCategoriesTreeRendererProps = Parameters<ComponentPropsWithoutRef<typeof VisibilityTree>["treeRenderer"]>[0];
function CustomCategoriesTreeRenderer(props: CustomCategoriesTreeRendererProps) {
const getLabel = props.getLabel;
const getLabelCallback = useCallback<Required<VisibilityTreeRendererProps>["getLabel"]>(
(node) => {
const originalLabel = getLabel(node);
return <>Custom node - {originalLabel}</>;
},
[getLabel],
);
const getSublabel = useCallback<Required<VisibilityTreeRendererProps>["getSublabel"]>(() => {
return <>Custom sub label</>;
}, []);
return <VisibilityTreeRenderer {...props} getLabel={getLabelCallback} getSublabel={getSublabel} />;
}
interface CustomCategoriesTreeProps {
imodel: IModelConnection;
viewport: Viewport;
getSchemaContext: (imodel: IModelConnection) => SchemaContext;
selectionStorage: SelectionStorage;
}
function CustomCategoriesTreeComponent({ imodel, viewport, getSchemaContext, selectionStorage }: CustomCategoriesTreeProps) {
const buttonProps = useCategoriesTreeButtonProps({ viewport });
const { categoriesTreeProps, rendererProps } = useCategoriesTree({ activeView: viewport, filter: "" });
return (
<TreeWithHeader
buttons={[
<CategoriesTreeComponent.ShowAllButton {...buttonProps} key={"ShowAllButton"} />,
<CategoriesTreeComponent.HideAllButton {...buttonProps} key={"HideAllButton"} />,
]}
>
<VisibilityTree
{...categoriesTreeProps}
getSchemaContext={getSchemaContext}
selectionStorage={selectionStorage}
imodel={imodel}
treeRenderer={(props) => <CustomCategoriesTreeRenderer {...props} {...rendererProps} />}
/>
</TreeWithHeader>
);
}
// __PUBLISH_EXTRACT_END__
const { getByText } = render(
<CustomCategoriesTreeComponent
imodel={imodelConnection}
viewport={testViewport}
getSchemaContext={getSchemaContext}
selectionStorage={unifiedSelectionStorage}
/>,
);
await waitFor(() => getByText("Test SpatialCategory"));
cleanup();
});
});
});
});
});