This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
useLatestMessageDataItem.test.tsx
119 lines (107 loc) · 3.87 KB
/
useLatestMessageDataItem.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
/** @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 MockMessagePipelineProvider from "@foxglove-studio/app/components/MessagePipeline/MockMessagePipelineProvider";
import { Message } from "@foxglove-studio/app/players/types";
import { useLatestMessageDataItem } from "./useLatestMessageDataItem";
const topics = [{ name: "/topic", datatype: "datatype" }];
const datatypes = {
datatype: { fields: [{ name: "value", type: "uint32", isArray: false, isComplex: false }] },
};
const messages: Message[] = [
{
topic: "/topic",
receiveTime: { sec: 0, nsec: 0 },
message: { value: 0 },
},
{
topic: "/topic",
receiveTime: { sec: 1, nsec: 0 },
message: { value: 1 },
},
{
topic: "/topic",
receiveTime: { sec: 2, nsec: 0 },
message: { value: 2 },
},
];
describe("useLatestMessageDataItem", () => {
// Create a helper component that exposes the results of the hook for mocking.
function createTest() {
function Test({ path }: { path: string }) {
Test.result(useLatestMessageDataItem(path));
return ReactNull;
}
Test.result = jest.fn();
return Test;
}
it("returns undefined by default", async () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider topics={topics} datatypes={datatypes}>
<Test path="/topic.value" />
</MockMessagePipelineProvider>,
);
expect(Test.result.mock.calls).toEqual([[undefined]]);
root.unmount();
});
it("uses the latest message", async () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider messages={[messages[0]!]} topics={topics} datatypes={datatypes}>
<Test path="/topic.value" />
</MockMessagePipelineProvider>,
);
expect(Test.result.mock.calls).toEqual([
[{ message: messages[0], queriedData: [{ path: "/topic.value", value: 0 }] }],
]);
root.setProps({ messages: [messages[1], messages[2]] });
expect(Test.result.mock.calls).toEqual([
[{ message: messages[0], queriedData: [{ path: "/topic.value", value: 0 }] }],
[{ message: messages[2], queriedData: [{ path: "/topic.value", value: 2 }] }],
]);
root.unmount();
});
it("only keeps messages that match the path", async () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider messages={messages} topics={topics} datatypes={datatypes}>
<Test path="/topic{value==1}.value" />
</MockMessagePipelineProvider>,
);
expect(Test.result.mock.calls).toEqual([
[{ message: messages[1], queriedData: [{ path: "/topic{value==1}.value", value: 1 }] }],
]);
root.unmount();
});
it("changing the path gives the new queriedData from the message", async () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider messages={messages} topics={topics} datatypes={datatypes}>
<Test path="/topic{value==1}.value" />
</MockMessagePipelineProvider>,
);
root.setProps({ children: <Test path="/topic{value==1}" /> });
expect(Test.result.mock.calls).toEqual([
[{ message: messages[1], queriedData: [{ path: "/topic{value==1}.value", value: 1 }] }],
[
{
message: messages[1],
queriedData: [{ path: "/topic{value==1}", value: messages[1]?.message }],
},
],
]);
root.unmount();
});
});