This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 429
/
useSceneBuilderAndTransformsData.test.tsx
205 lines (187 loc) · 7.03 KB
/
useSceneBuilderAndTransformsData.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
194
195
196
197
198
199
200
201
202
203
204
205
/** @jest-environment jsdom */
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2018-2021 Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found at http://www.apache.org/licenses/LICENSE-2.0
// You may not use this file except in compliance with the License.
import { mount } from "enzyme";
import { omit } from "lodash";
import MockMessagePipelineProvider from "@foxglove-studio/app/components/MessagePipeline/MockMessagePipelineProvider";
import { Namespace } from "@foxglove-studio/app/types/Messages";
import { TRANSFORM_TOPIC } from "@foxglove-studio/app/util/globalConstants";
import { UseSceneBuilderAndTransformsDataInput } from "./types";
import useSceneBuilderAndTransformsData from "./useSceneBuilderAndTransformsData";
type ErrorsByTopic = {
[topicName: string]: string[];
};
class MockTransform {
_values: { id: string }[];
constructor({ tfs }: { tfs: { id: string }[] }) {
this._values = tfs || [];
}
values() {
return this._values;
}
}
const DEFAULT_ERRORS = {
"/topic_a": ["missing transforms to root transform: some_root_tf"],
};
class MockSceneBuilder {
allNamespaces: Namespace[] = [];
errorsByTopic: ErrorsByTopic = DEFAULT_ERRORS;
constructor({
namespaces,
errorsByTopic,
}: {
namespaces?: Namespace[];
errorsByTopic?: ErrorsByTopic;
}) {
if (namespaces) {
this.allNamespaces = namespaces;
}
if (errorsByTopic) {
this.errorsByTopic = errorsByTopic;
}
}
}
function getMockProps({
showNamespaces,
showTransforms,
showErrors,
mockTfIds,
}: {
showNamespaces?: boolean;
showTransforms?: boolean;
showErrors?: boolean;
mockTfIds?: string[];
}): UseSceneBuilderAndTransformsDataInput {
let tfIds: string[] = [];
if (showTransforms) {
tfIds = ["some_tf1", "some_tf2", ""];
} else if (mockTfIds) {
tfIds = mockTfIds;
}
return {
sceneBuilder: new MockSceneBuilder({
namespaces: showNamespaces
? [
{ topic: "/foo", name: "ns1" },
{ topic: "/foo", name: "ns2" },
]
: [],
errorsByTopic: showErrors ? DEFAULT_ERRORS : undefined,
}),
transforms: new MockTransform({ tfs: tfIds.map((id) => ({ id })) }),
staticallyAvailableNamespacesByTopic: {},
};
}
describe("useSceneBuilderAndTransformsData", () => {
// Create a helper component that exposes the results of the hook for mocking.
function createTest() {
function Test(
props: UseSceneBuilderAndTransformsDataInput & {
messagePipelineProps?: React.ComponentProps<typeof MockMessagePipelineProvider>;
},
) {
return (
<MockMessagePipelineProvider {...props.messagePipelineProps}>
<TestInner {...omit(props, "messagePipelineProps")} />
</MockMessagePipelineProvider>
);
}
function TestInner(props: UseSceneBuilderAndTransformsDataInput) {
Test.result(useSceneBuilderAndTransformsData(props));
return ReactNull;
}
Test.result = jest.fn();
return Test;
}
describe("availableNamespacesByTopic", () => {
it("collects namespaces from transforms and sceneBuilder namespaces", () => {
const Test = createTest();
const staticallyAvailableNamespacesByTopic = { "/bar": ["ns3", "ns4"] };
const root = mount(
<Test
{...getMockProps({})}
staticallyAvailableNamespacesByTopic={staticallyAvailableNamespacesByTopic}
/>,
);
root.setProps({
...getMockProps({ showNamespaces: true, showTransforms: true }),
staticallyAvailableNamespacesByTopic,
});
expect(Test.result.mock.calls.map((args) => args[0].availableNamespacesByTopic)).toEqual([
staticallyAvailableNamespacesByTopic,
{
"/foo": ["ns1", "ns2"],
[TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"],
...staticallyAvailableNamespacesByTopic,
},
{
"/foo": ["ns1", "ns2"],
[TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"],
...staticallyAvailableNamespacesByTopic,
},
]);
});
it("shows all transform namespaces collected over time", () => {
const Test = createTest();
const root = mount(<Test {...getMockProps({ showTransforms: true })} />);
expect(Test.result).toHaveBeenCalledTimes(1);
// TFs were removed, but we still report them in the available namespaces.
root.setProps(getMockProps({}));
expect(Test.result).toHaveBeenCalledTimes(3);
root.setProps(getMockProps({ mockTfIds: ["some_tf3"] }));
expect(Test.result).toHaveBeenCalledTimes(5);
// Technically we would only want the Test component to re-render once per props change.
// However, this test is set up such that the MockTransforms object is replaced on each change.
// In real-world use, the Transforms object is mutated and only changes when data is cleared out.
expect(Test.result.mock.calls.map((args) => args[0].availableNamespacesByTopic)).toEqual([
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"] },
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"] },
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"] },
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2", "some_tf3"] },
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2", "some_tf3"] },
]);
});
it("resets transforms collected when the player changes", () => {
const Test = createTest();
const root = mount(<Test {...getMockProps({ showTransforms: true })} />);
root.setProps({ ...getMockProps({}), messagePipelineProps: { playerId: "somePlayerId" } });
expect(Test.result.mock.calls.map((args) => args[0].availableNamespacesByTopic)).toEqual([
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"] },
{ [TRANSFORM_TOPIC]: ["some_tf1", "some_tf2"] },
{},
]);
});
});
describe("getSceneErrorsByKey", () => {
it("collects scene builder errors and group them by key", () => {
const Test = createTest();
const mockSceneBuilder = new MockSceneBuilder({
namespaces: [],
errorsByTopic: {
"/topic_a": ["error msg foo", "missing transforms to root transform: some_root_tf"],
"/webviz_bag_2/topic_a": ["error msg bar", "missing frame id"],
},
});
const root = mount(<Test {...getMockProps({})} sceneBuilder={mockSceneBuilder} />);
expect(Test.result.mock.calls[0][0].sceneErrorsByKey).toEqual({
"t:/topic_a": ["error msg foo", "missing transforms to root transform: some_root_tf"],
"t:/webviz_bag_2/topic_a": ["error msg bar", "missing frame id"],
});
// Update scene errors.
root.setProps(getMockProps({ showErrors: true }));
expect(Test.result.mock.calls[1][0].sceneErrorsByKey).toEqual({
"t:/topic_a": ["missing transforms to root transform: some_root_tf"],
});
});
});
});