This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 423
/
SearchText.test.tsx
289 lines (277 loc) · 9.99 KB
/
SearchText.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/** @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 2019-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 { renderHook, act } from "@testing-library/react-hooks";
import { mount } from "enzyme";
import { CameraState, DEFAULT_CAMERA_STATE } from "regl-worldview";
import { Interactive } from "@foxglove-studio/app/panels/ThreeDimensionalViz/Interactions/types";
import {
GLTextMarker,
useGLText,
ORANGE,
getHighlightedIndices,
useSearchMatches,
} from "@foxglove-studio/app/panels/ThreeDimensionalViz/SearchText";
import Transforms from "@foxglove-studio/app/panels/ThreeDimensionalViz/Transforms";
import { TextMarker } from "@foxglove-studio/app/types/Messages";
import { MARKER_MSG_TYPES } from "@foxglove-studio/app/util/globalConstants";
export const ROOT_FRAME_ID = "root_frame";
export const CHILD_FRAME_ID = "child_frame";
export const header = {
frame_id: ROOT_FRAME_ID,
stamp: { sec: 0, nsec: 0 },
};
function makeInteractive<T>(message: T): Interactive<T> {
return { ...message, interactionData: { topic: "/topic", originalMessage: message } } as any;
}
const createMarker = (text: string): Interactive<TextMarker> | Interactive<GLTextMarker> => {
return makeInteractive({
header,
ns: "base",
action: 0,
scale: { x: 1, y: 1, z: 1 },
color: { r: 1, g: 1, b: 1, a: 1 },
id: "id",
pose: {
orientation: { x: 0, y: 0, z: 0, w: 1 },
position: { x: 1, y: 1, z: 1 },
},
type: MARKER_MSG_TYPES.TEXT_VIEW_FACING,
text,
}) as any;
};
const runUseGLTextTest = (
text: Interactive<TextMarker>[],
searchText: string,
searchTextMatches: Interactive<GLTextMarker>[],
setSearchTextMatches: (marker: Interactive<GLTextMarker>[]) => void = jest.fn(),
selectedMatchIndex: number = 0,
) => {
const originalMarkers: Interactive<TextMarker>[] = text;
let glTextMarkers: any = [];
const Wrapper = () => {
glTextMarkers = useGLText({
text: originalMarkers,
searchText,
searchTextOpen: true,
selectedMatchIndex,
setSearchTextMatches,
searchTextMatches,
} as any);
return ReactNull;
};
const root = mount(<Wrapper />);
root.update();
root.unmount();
return glTextMarkers;
};
describe("<SearchText />", () => {
describe("getHighlightedIndices", () => {
expect(getHighlightedIndices("hello webviz", "e")).toEqual([1, 7]);
expect(getHighlightedIndices("hello webviz", "hello ")).toEqual([0, 1, 2, 3, 4, 5]);
it("is case insensitive", () => {
expect(getHighlightedIndices("Car", "car")).toEqual([0, 1, 2]);
expect(getHighlightedIndices("car", "Car")).toEqual([0, 1, 2]);
});
});
describe("useGLText", () => {
it("updates the text markers to include highlighted indices", async () => {
const setSearchTextMatches = jest.fn();
const glTextMarkers = runUseGLTextTest(
[createMarker("hello")],
"hello",
[],
setSearchTextMatches,
);
expect(glTextMarkers.length).toEqual(1);
expect(glTextMarkers[0].highlightedIndices).toEqual([0, 1, 2, 3, 4]);
expect(setSearchTextMatches).toHaveBeenCalledWith(glTextMarkers);
});
it("works with empy text markers", async () => {
const setSearchTextMatches = jest.fn();
const glTextMarkers = runUseGLTextTest([createMarker("")], "hello", [], setSearchTextMatches);
expect(glTextMarkers.length).toEqual(1);
expect(glTextMarkers[0].highlightedIndices).toEqual(undefined);
expect(setSearchTextMatches).not.toHaveBeenCalled();
});
it("updates matches to empty", async () => {
const setSearchTextMatches = jest.fn();
const marker = createMarker("hello");
const glTextMarkers = runUseGLTextTest(
[marker],
"bye",
[marker as any],
setSearchTextMatches,
);
expect(glTextMarkers.length).toEqual(1);
expect(glTextMarkers[0].highlightedIndices).toEqual(undefined);
expect(setSearchTextMatches).toHaveBeenCalledWith([]);
});
it("sets a custom highlight color to the correct index", async () => {
const setSearchTextMatches = jest.fn();
const markers = [
createMarker("hello webviz"),
createMarker("hello cruies"),
createMarker("hello future"),
];
const glTextMarkers = runUseGLTextTest(markers, "hello", [], setSearchTextMatches, 2);
expect(glTextMarkers.length).toEqual(3);
expect(glTextMarkers[2].highlightColor).toEqual(ORANGE);
});
it("does update the markers if search text has changed", async () => {
const marker = createMarker("hello");
const originalMarkers: Interactive<TextMarker>[] = [marker];
let glTextMarkers: any = [];
const Wrapper = ({ searchText }: { searchText: string }) => {
glTextMarkers = useGLText({
text: originalMarkers,
searchText,
searchTextOpen: true,
selectedMatchIndex: 0,
setSearchTextMatches: jest.fn(),
searchTextMatches: [],
});
return <span>{searchText}</span>;
};
const root = mount(<Wrapper searchText={"hello"} />);
root.update();
const originalGlText = glTextMarkers;
root.setProps({ searchText: "bye" });
expect(glTextMarkers).not.toEqual(originalGlText);
root.unmount();
});
it("does not update the markers if nothing has changed", async () => {
const marker = createMarker("hello");
const originalMarkers: Interactive<TextMarker>[] = [marker];
let glTextMarkers: any = [];
const Wrapper = ({ randomNum }: { randomNum: number }) => {
glTextMarkers = useGLText({
text: originalMarkers,
searchText: "hello",
searchTextOpen: true,
selectedMatchIndex: 0,
setSearchTextMatches: jest.fn(),
searchTextMatches: [],
});
return <span>{randomNum}</span>;
};
const root = mount(<Wrapper randomNum={1} />);
root.update();
const originalGlText = glTextMarkers;
root.setProps({ randomNum: 2 });
expect(glTextMarkers).toEqual(originalGlText);
root.unmount();
});
});
describe("useCurrentMatchPosition", () => {
const p = (x: number = 0, y = x, z = x) => ({ x, y, z });
const q = (x = 0, y = 0, z = 0, w = 0) => ({ x, y, z, w });
const getTf = () => {
const tf = new Transforms();
const message = {
header,
pose: {
position: p(30, 60, 90),
orientation: q(0, 0, 0, 1),
},
};
const rootTf = tf.storage.get(CHILD_FRAME_ID);
rootTf.set(message.pose.position, message.pose.orientation);
rootTf.parent = tf.storage.get(ROOT_FRAME_ID);
return tf;
};
const baseCameraState: CameraState = {
...DEFAULT_CAMERA_STATE,
target: [0, 0, 0],
targetOffset: [0, 0, 0],
targetOrientation: [0, 0, 0, 1],
};
const useWrapper = (
initialCameraState = baseCameraState,
initialMatch: GLTextMarker = createMarker("text"),
) => {
const transforms = getTf();
const [cameraState, updateCameraState] = React.useState(initialCameraState);
const [currentMatch, updateCurrentMatch] = React.useState<GLTextMarker>(initialMatch);
// eslint-disable-next-line react-hooks/exhaustive-deps
const onCameraStateChange = React.useCallback(jest.fn(updateCameraState), []);
useSearchMatches({
cameraState,
onCameraStateChange,
currentMatch,
rootTf: ROOT_FRAME_ID,
searchTextOpen: true,
transforms,
});
return { cameraState, updateCurrentMatch, onCameraStateChange };
};
it("updates camera state with a new target offset based on the root transform", () => {
const { result } = renderHook(() => useWrapper(baseCameraState));
expect(result.current.cameraState).toEqual({
...baseCameraState,
targetOffset: [1, 1, 1],
});
});
it("should take into account target heading in target offset", () => {
const initialCameraState: CameraState = {
...baseCameraState,
targetOrientation: [0.0, 0.0, -0.8, 0.6],
};
const { result } = renderHook(() => useWrapper(initialCameraState));
const [x, y, z] = result.current.cameraState.targetOffset;
expect(x).toBeCloseTo(-1.24);
expect(y).toBeCloseTo(0.68);
expect(z).toEqual(1);
});
it("should update camera state if there are new matches", () => {
const { result } = renderHook(() => useWrapper(baseCameraState));
act(() => {
result.current.updateCurrentMatch({
...createMarker("text"),
pose: {
position: p(2, 2, 2),
orientation: q(0, 0, 0, 1),
},
});
});
expect(result.current.cameraState).toEqual({
...baseCameraState,
targetOffset: [2, 2, 2],
});
act(() => {
result.current.updateCurrentMatch({
...createMarker("text"),
pose: {
position: p(3, 3, 3),
orientation: q(0, 0, 0, 1),
},
});
});
expect(result.current.cameraState).toEqual({
...baseCameraState,
targetOffset: [3, 3, 3],
});
});
it("should not fire onCameraStateChange if cameraState is updated independently", () => {
const { result } = renderHook(() => useWrapper(baseCameraState));
expect(result.current.onCameraStateChange).toHaveBeenCalledTimes(1);
act(() => {
result.current.onCameraStateChange({
...DEFAULT_CAMERA_STATE,
targetOffset: [5, 5, 5],
});
});
expect(result.current.onCameraStateChange).toHaveBeenCalledTimes(2);
});
});
});