This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
useMessagesByPath.test.tsx
454 lines (409 loc) · 14 KB
/
useMessagesByPath.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/** @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 { act, renderHook } from "@testing-library/react-hooks";
import { createMemoryHistory } from "history";
import React, { PropsWithChildren } from "react";
import { setGlobalVariables } from "@foxglove-studio/app/actions/panels";
import useMessagesByPath from "@foxglove-studio/app/components/MessagePathSyntax/useMessagesByPath";
import MockMessagePipelineProvider from "@foxglove-studio/app/components/MessagePipeline/MockMessagePipelineProvider";
import createRootReducer from "@foxglove-studio/app/reducers";
import configureStore from "@foxglove-studio/app/store/configureStore.testing";
import * as fixture from "./fixture";
const singleTopic = [{ name: "/some/topic", datatype: "some/datatype" }];
function queriedMessage(index: 0 | 1 | 2) {
return {
message: fixture.messages[index],
queriedData: [{ value: fixture.messages[index].message, path: "/some/topic" }],
};
}
type TestProps = {
paths: string[];
historySize?: number;
} & Partial<
Pick<
React.ComponentProps<typeof MockMessagePipelineProvider>,
"topics" | "datatypes" | "messages" | "activeData" | "store"
>
>;
function makeMessagePipelineWrapper() {
const setSubscriptions = jest.fn();
const wrapper = ({
children,
topics = [],
datatypes = {},
messages = [],
activeData,
store,
}: PropsWithChildren<TestProps>) => (
<MockMessagePipelineProvider
topics={topics}
datatypes={datatypes}
messages={messages}
setSubscriptions={setSubscriptions}
activeData={activeData}
store={store}
>
{children}
</MockMessagePipelineProvider>
);
return { setSubscriptions, wrapper };
}
// eslint-disable-next-line react-hooks/rules-of-hooks
const callback = ({ paths, historySize }: TestProps) => useMessagesByPath(paths, historySize);
describe("useMessagesByPath", () => {
it("(un)subscribes based on `topics`", () => {
const { setSubscriptions, wrapper } = makeMessagePipelineWrapper();
const { unmount, rerender } = renderHook(callback, {
wrapper,
initialProps: {
paths: ["/some/topic", "/some/other/topic"],
topics: [
{ name: "/some/topic", datatype: "dummy" },
{ name: "/some/other/topic", datatype: "dummy" },
],
},
});
rerender({ paths: ["/some/topic"] });
unmount();
expect(setSubscriptions.mock.calls).toEqual([
[
expect.any(String),
[
{ topic: "/some/topic", preloadingFallback: false },
{ topic: "/some/other/topic", preloadingFallback: false },
],
],
[expect.any(String), [{ topic: "/some/topic", preloadingFallback: false }]],
[expect.any(String), []],
]);
});
it("does not filter out non-existing topics", () => {
// Initial mount. Note that we haven't received any topics yet.
const { setSubscriptions, wrapper } = makeMessagePipelineWrapper();
const { unmount, rerender } = renderHook(callback, {
wrapper,
initialProps: {
paths: ["/some/topic"],
},
});
// Updating to change topics
rerender({ paths: ["/some/topic", "/some/other/topic"] });
// And unsubscribes properly, too.
unmount();
expect(setSubscriptions.mock.calls).toEqual([
[expect.any(String), [{ topic: "/some/topic", preloadingFallback: false }]],
[
expect.any(String),
[
{ topic: "/some/topic", preloadingFallback: false },
{ topic: "/some/other/topic", preloadingFallback: false },
],
],
[expect.any(String), []],
]);
});
it("allows changing historySize", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic"],
topics: singleTopic,
historySize: 1,
};
const { result, rerender, unmount } = renderHook(callback, {
wrapper,
initialProps,
});
rerender({ ...initialProps, historySize: 2 });
rerender({ ...initialProps, historySize: 2, messages: [...fixture.messages] });
unmount();
// These extra renders are not ideal, but are necessary because the old MessageHistory wrapped itself in a React.memo.
expect(result.all).toEqual([
{ "/some/topic": [] },
{ "/some/topic": [] },
{ "/some/topic": [] },
{ "/some/topic": [] },
{
"/some/topic": [
{ message: fixture.messages[1], queriedData: [] },
{ message: fixture.messages[2], queriedData: [] },
],
},
]);
});
it("buffers messages (with historySize=2)", () => {
// Start with just the first two messages.
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic"],
topics: singleTopic,
historySize: 2,
datatypes: fixture.datatypes,
messages: [fixture.messages[0], fixture.messages[1]],
};
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps,
});
expect(result.all.length).toEqual(1);
// Then let's send in the last message too, and it should discard the older message
// (since bufferSize=2).
rerender({ ...initialProps, messages: [fixture.messages[2]] });
// These extra renders are not ideal, but are necessary because the old MessageHistory wrapped itself in a React.memo.
expect(result.all).toEqual([
{ "/some/topic": [queriedMessage(0), queriedMessage(1)] },
{ "/some/topic": [queriedMessage(0), queriedMessage(1)] },
{ "/some/topic": [queriedMessage(1), queriedMessage(2)] },
]);
});
it("clears everything on seek", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic"],
topics: singleTopic,
datatypes: fixture.datatypes,
messages: [fixture.messages[0]],
};
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps,
});
expect(result.all.length).toEqual(1);
// Do the seek, and make sure we clear things out.
rerender({ ...initialProps, messages: [], activeData: { lastSeekTime: 1 } });
expect(result.all.length).toEqual(3);
// These extra renders are not ideal, but are necessary because the old MessageHistory wrapped itself in a React.memo.
expect(result.all).toEqual([
{ "/some/topic": [queriedMessage(0)] },
{ "/some/topic": [queriedMessage(0)] },
{ "/some/topic": [] },
]);
});
it("returns the same when passing in a topic twice", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
topics: singleTopic,
datatypes: fixture.datatypes,
messages: [fixture.messages[0], fixture.messages[1]],
};
const { result: result1 } = renderHook(callback, {
wrapper,
initialProps: { ...initialProps, paths: ["/some/topic"] },
});
const { result: result2 } = renderHook(callback, {
wrapper,
initialProps: { ...initialProps, paths: ["/some/topic", "/some/topic"] },
});
expect(result1.all).toEqual(result2.all);
});
it("lets you drill down in a path", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic.index"],
topics: singleTopic,
datatypes: fixture.datatypes,
messages: [fixture.messages[0], fixture.messages[1]],
};
const { result } = renderHook(callback, {
wrapper,
initialProps,
});
expect(result.all).toEqual([
{
"/some/topic.index": [
{
message: fixture.messages[0],
queriedData: [{ path: "/some/topic.index", value: 0 }],
},
{
message: fixture.messages[1],
queriedData: [{ path: "/some/topic.index", value: 1 }],
},
],
},
]);
});
it("remembers data when changing topics", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic"],
topics: [
{ name: "/some/topic", datatype: "some/datatype" },
{ name: "/some/other/topic", datatype: "dummy" },
],
datatypes: fixture.datatypes,
messages: [fixture.messages[0]],
};
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps,
});
// Add a new path, and we should get another call with the same data
rerender({ ...initialProps, paths: ["/some/topic", "/some/other/topic"] });
expect(result.all).toEqual([
{ "/some/topic": [queriedMessage(0)] },
{ "/some/topic": [queriedMessage(0)], "/some/other/topic": [] },
]);
});
it("remembers data when changing paths on an existing topic", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic"],
topics: [
{ name: "/some/topic", datatype: "some/datatype" },
{ name: "/some/other/topic", datatype: "dummy" },
],
datatypes: fixture.datatypes,
messages: [fixture.messages[0]],
};
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps,
});
// Change an existing path, and we should restore the data from the previous path on the same topic
rerender({ ...initialProps, paths: ["/some/topic.index"] });
expect(result.all).toEqual([
{ "/some/topic": [queriedMessage(0)] },
{
"/some/topic.index": [
{
message: fixture.messages[0],
queriedData: [{ path: "/some/topic.index", value: 0 }],
},
],
},
]);
});
describe("global variables in paths", () => {
const exampleDatatypes = {
"dtype/Foo": {
fields: [{ name: "bars", type: "dtype/Bar", isArray: true, isComplex: true }],
},
"dtype/Bar": {
fields: [
{ name: "index", type: "int32" },
{ name: "baz", type: "int32" },
],
},
};
const message = {
topic: "/some/topic",
receiveTime: { sec: 100, nsec: 0 },
message: {
bars: [
{ index: 0, baz: 10 },
{ index: 1, baz: 11 },
{ index: 2, baz: 12 },
],
},
};
it("updates queriedData when a global variable changes", () => {
const store = configureStore(createRootReducer(createMemoryHistory()));
store.dispatch(setGlobalVariables({ foo: 0 }));
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/topic.bars[:]{index==$foo}.baz"],
store,
topics: [{ name: "/some/topic", datatype: "dtype/Foo" }],
datatypes: exampleDatatypes,
messages: [message],
};
const { result, unmount } = renderHook(callback, {
wrapper,
initialProps,
});
// when $foo changes to 1, queriedData.value should change to 11
act(() => {
store.dispatch(setGlobalVariables({ foo: 1 }));
});
expect(result.all).toEqual([
{
"/some/topic.bars[:]{index==$foo}.baz": [
{
message,
queriedData: [{ path: "/some/topic.bars[:]{index==$foo}.baz", value: 10 }],
},
],
},
{
"/some/topic.bars[:]{index==$foo}.baz": [
{
message,
queriedData: [{ path: "/some/topic.bars[:]{index==$foo}.baz", value: 11 }],
},
],
},
]);
unmount();
});
});
it("supports changing a path for a previously-existing topic that no longer exists", () => {
const { wrapper } = makeMessagePipelineWrapper();
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps: {
paths: ["/some/topic"],
topics: singleTopic,
datatypes: fixture.datatypes,
messages: [fixture.messages[0]],
},
});
rerender({ topics: [], datatypes: {}, messages: [], paths: ["/some/topic.index"] });
expect(result.all).toEqual([
{ "/some/topic": [queriedMessage(0)] },
{
// These extra renders are not ideal, but are necessary because the old MessageHistory wrapped itself in a React.memo.
"/some/topic.index": [
{ message: fixture.messages[0], queriedData: [{ path: "/some/topic.index", value: 0 }] },
],
},
{ "/some/topic.index": [] },
]);
});
it("ignores messages from non-subscribed topics", () => {
const { wrapper } = makeMessagePipelineWrapper();
const initialProps = {
paths: ["/some/other/topic"],
topics: [
{ name: "/some/topic", datatype: "dummy" },
{ name: "/some/other/topic", datatype: "dummy" },
],
datatypes: fixture.datatypes,
messages: [fixture.messages[0]],
};
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps,
});
rerender({ ...initialProps, messages: [fixture.messages[1], fixture.messages[2]] });
expect(result.all).toEqual([{ "/some/other/topic": [] }, { "/some/other/topic": [] }]);
});
it("return the same itemsByPath (identity) if the MessageHistory props did not change but children changed", () => {
const { wrapper } = makeMessagePipelineWrapper();
const { result, rerender } = renderHook(callback, {
wrapper,
initialProps: {
paths: ["/some/topic"],
topics: singleTopic,
datatypes: fixture.datatypes,
messages: [fixture.messages[0]],
},
});
rerender();
expect(result.all).toEqual([
{ "/some/topic": [queriedMessage(0)] },
{ "/some/topic": [queriedMessage(0)] },
]);
expect(result.all[0]).toBe(result.all[1]);
});
});