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
/
useBlocksByTopic.test.tsx
138 lines (120 loc) · 4.7 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
/** @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 { mount } from "enzyme";
import { cloneDeep } from "lodash";
import { parseMessageDefinition } from "rosbag";
import MockMessagePipelineProvider from "@foxglove-studio/app/components/MessagePipeline/MockMessagePipelineProvider";
import * as PanelAPI from ".";
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 ReactNull;
}
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 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: [undefined, undefined], 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: {},
},
];
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(c3.blocks).not.toBe(c4.blocks);
expect(c3.blocks[0]).not.toBe(c4.blocks[0]);
root.unmount();
});
});