This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
useDataSourceInfo.test.tsx
121 lines (112 loc) · 3.27 KB
/
useDataSourceInfo.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
// 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 * as React from "react";
import * as PanelAPI from ".";
import { MockMessagePipelineProvider } from "@foxglove-studio/app/components/MessagePipeline";
describe("useDataSourceInfo", () => {
const topics = [{ name: "/foo", datatype: "Foo" }];
const messages = [
{
topic: "/foo",
receiveTime: { sec: 1, nsec: 2 },
message: {},
},
{
topic: "/foo",
receiveTime: { sec: 5, nsec: 6 },
message: {},
},
];
const datatypes = {
Foo: { fields: [] },
};
// Create a helper component that exposes the results of the hook in a Jest mock function
function createTest() {
function Test() {
return Test.renderFn(PanelAPI.useDataSourceInfo());
}
Test.renderFn = jest.fn().mockImplementation(() => null);
return Test;
}
it("returns data from MessagePipelineContext", () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider
topics={topics}
datatypes={datatypes}
capabilities={["hello"]}
messages={[messages[0]]}
startTime={{ sec: 0, nsec: 1 }}
>
<Test />
</MockMessagePipelineProvider>,
);
expect(Test.renderFn.mock.calls).toEqual([
[
{
topics: [{ name: "/foo", datatype: "Foo" }],
datatypes: { Foo: { fields: [] } },
capabilities: ["hello"],
startTime: { sec: 0, nsec: 1 },
playerId: "1",
},
],
]);
root.unmount();
});
it("doesn't change when messages change", () => {
const Test = createTest();
const root = mount(
<MockMessagePipelineProvider
topics={topics}
datatypes={datatypes}
capabilities={["hello"]}
messages={[messages[0]]}
startTime={{ sec: 0, nsec: 1 }}
>
<Test />
</MockMessagePipelineProvider>,
);
expect(Test.renderFn.mock.calls).toEqual([
[
{
topics: [{ name: "/foo", datatype: "Foo" }],
datatypes: { Foo: { fields: [] } },
capabilities: ["hello"],
startTime: { sec: 0, nsec: 1 },
playerId: "1",
},
],
]);
Test.renderFn.mockClear();
root.setProps({ messages: [messages[1]] });
expect(Test.renderFn).toHaveBeenCalledTimes(0);
root.setProps({ topics: [...topics, { name: "/bar", datatype: "Bar" }] });
expect(Test.renderFn.mock.calls).toEqual([
[
{
topics: [
{ name: "/bar", datatype: "Bar" },
{ name: "/foo", datatype: "Foo" },
],
datatypes: { Foo: { fields: [] } },
capabilities: ["hello"],
startTime: { sec: 0, nsec: 1 },
playerId: "1",
},
],
]);
root.unmount();
});
});