This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
useBlocksByTopic.test.tsx
213 lines (192 loc) · 7.56 KB
/
useBlocksByTopic.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
// 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 { mount } from "enzyme";
import { cloneDeep } from "lodash";
import * as React from "react";
import { MessageReader, parseMessageDefinition } from "rosbag";
import * as PanelAPI from ".";
import { MockMessagePipelineProvider } from "@foxglove-studio/app/components/MessagePipeline";
describe("useBlocksByTopic", () => {
// Create a helper component that exposes the results of the hook for mocking.
function createTest() {
function Test({ topics }: { topics: string[] }) {
Test.result(PanelAPI.useBlocksByTopic(topics));
return null;
}
Test.result = jest.fn();
return Test;
}
it("returns an empty structure when there are no blocks", async () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider>
<Test topics={["/foo"]} />
</MockMessagePipelineProvider>,
);
expect(Test.result.mock.calls).toEqual([[{ blocks: [], messageReadersByTopic: {} }]]);
root.unmount();
});
it("returns just the blocks for which we're subscribed and have message definitions", async () => {
// Eight cases:
// Subscribed | Defined | Present | Should appear in blocks
// -----------+---------+---------+------------------------
// 0 | 0 | 0 | 0
// 0 | 0 | 1 | 0
// 0 | 1 | 0 | 0
// 0 | 1 | 1 | 0
// 1 | 0 | 0 | 0
// 1 | 0 | 1 | 0
// 1 | 1 | 0 | 0
// 1 | 1 | 1 | 1
const activeData = {
parsedMessageDefinitionsByTopic: {
"/just_defined": parseMessageDefinition("uint32 id"),
"/defined_and_present": parseMessageDefinition("uint32 id"),
"/subscribed_and_defined": parseMessageDefinition("uint32 id"),
"/subscribed_defined_and_present": parseMessageDefinition("uint32 id"),
},
};
const progress = {
messageCache: {
blocks: [
{
sizeInBytes: 0,
messagesByTopic: {
"/just_present": [],
"/defined_and_present": [],
"/subscribed_and_present": [],
"/subscribed_defined_and_present": [],
},
},
undefined,
],
startTime: { sec: 0, nsec: 0 },
},
};
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider activeData={activeData} progress={progress}>
<Test
topics={[
"/just_subscribed",
"/subscribed_and_defined",
"/subscribed_and_present",
"/subscribed_defined_and_present",
]}
/>
</MockMessagePipelineProvider>,
);
expect(Test.result.mock.calls).toEqual([
[
{
blocks: [
{
// Messages from the subscribed and defined topic that is present in the block.
"/subscribed_defined_and_present": [],
}, // Missing block transformed into empty messages-by-topic. Missing/uncached data for
// topics is signaled through missing entries in these objects.
{},
],
// Readers for each subscribed and defined topic, regardless of whether anything has been
// cached for those topics.
messageReadersByTopic: {
"/subscribed_and_defined": expect.any(MessageReader),
"/subscribed_defined_and_present": expect.any(MessageReader),
},
},
],
]);
root.unmount();
});
it("returns no messagesByTopic when the player does not provide blocks", async () => {
const activeData = {
parsedMessageDefinitionsByTopic: { "/topic": parseMessageDefinition("uint32 id") },
};
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider activeData={activeData}>
<Test topics={["/topic1"]} />
</MockMessagePipelineProvider>,
);
// Consumers just need to check in one place to see whether they need a fallback for a topic:
// in messageReadersByTopic. (They don't also need to check the presence of blocks.)
expect(Test.result.mock.calls).toEqual([[{ blocks: [], messageReadersByTopic: {} }]]);
root.unmount();
});
it("handles uninitialized block states", async () => {
// messagesByTopic will not exist.
const activeData = undefined;
// Note: progress.blocks.map() does not iterate over the blocks.
const progress = {
messageCache: {
blocks: new Array(2),
startTime: { sec: 0, nsec: 0 },
},
};
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider activeData={activeData} progress={progress}>
<Test topics={["/topic1"]} />
</MockMessagePipelineProvider>,
);
// No message readers, even though we have a definition and we try to subscribe to the topic.
// This means the data will never be provided.
expect(Test.result.mock.calls).toEqual([[{ blocks: [{}, {}], messageReadersByTopic: {} }]]);
root.unmount();
});
it("maintains block identity across repeated renders", async () => {
const activeData = {
parsedMessageDefinitionsByTopic: { "/topic": parseMessageDefinition("uint32 id") },
};
const progress = {
messageCache: {
blocks: [{ sizeInBytes: 0, messagesByTopic: { "/topic": [] } }],
startTime: { sec: 0, nsec: 0 },
},
};
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider activeData={activeData} progress={progress}>
<Test topics={["/topic"]} />
</MockMessagePipelineProvider>,
);
// Make sure the calls are actual rerenders caused
const expectedCall = [
{
blocks: [{ "/topic": [] }],
messageReadersByTopic: { "/topic": expect.any(MessageReader) },
},
];
expect(Test.result.mock.calls).toEqual([expectedCall]);
// Same identity on everything. useBlocksByTopic does not run again.
root.setProps({ activeData, progress: { messageCache: { ...progress.messageCache } } });
// Block identity is the same, but blocks array identity changes.
root.setProps({
activeData,
progress: {
messageCache: { ...progress.messageCache, blocks: progress.messageCache.blocks.slice() },
},
});
// Both identities change.
root.setProps({ activeData, progress: { messageCache: cloneDeep(progress.messageCache) } });
expect(Test.result.mock.calls).toEqual([expectedCall, expectedCall, expectedCall]);
const [[c1], [c3], [c4]] = Test.result.mock.calls;
expect(c1.blocks).not.toBe(c3.blocks);
expect(c1.blocks[0]).toBe(c3.blocks[0]);
expect(c1.messageReadersByTopic).toBe(c3.messageReadersByTopic);
expect(c3.blocks).not.toBe(c4.blocks);
expect(c3.blocks[0]).not.toBe(c4.blocks[0]);
expect(c3.messageReadersByTopic).toBe(c4.messageReadersByTopic);
root.unmount();
});
});