This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
MessagePathInput.test.ts
91 lines (85 loc) · 3.22 KB
/
MessagePathInput.test.ts
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
// 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 { tryToSetDefaultGlobalVar, getFirstInvalidVariableFromRosPath } from "./MessagePathInput";
import { RosPath } from "./constants";
import { getGlobalHooks } from "@foxglove-studio/app/loadWebviz";
const defaultGlobalVars = getGlobalHooks().getDefaultPersistedState().panels.globalVariables;
describe("tryToSetDefaultGlobalVar", () => {
it("correctly returns true/false depending on whether a global variable has a default", () => {
const setGlobalVars = jest.fn();
expect(tryToSetDefaultGlobalVar("some_var_without_default", setGlobalVars)).toEqual(false);
expect(setGlobalVars).not.toHaveBeenCalled();
Object.keys(defaultGlobalVars).forEach((defaultKey) => {
expect(tryToSetDefaultGlobalVar(defaultKey, setGlobalVars)).toEqual(true);
expect(setGlobalVars).toHaveBeenCalledWith({
[defaultKey]: defaultGlobalVars[defaultKey],
});
});
});
});
describe("getFirstInvalidVariableFromRosPath", () => {
it("returns all possible message paths when not passing in `validTypes`", () => {
const setGlobalVars = jest.fn();
const rosPath: RosPath = {
topicName: "/some_topic",
messagePath: [
{ type: "name", name: "fieldName" },
{ type: "slice", start: 0, end: Infinity },
{
type: "filter",
path: ["myId"],
value: { variableName: "not_yet_set_global_var", startLoc: 10 },
nameLoc: 11,
valueLoc: 10,
repr: "myId==$not_yet_set_global_var",
},
],
modifier: null,
};
expect(getFirstInvalidVariableFromRosPath(rosPath, {}, setGlobalVars)).toEqual({
loc: 10,
variableName: "not_yet_set_global_var",
});
expect(setGlobalVars).not.toHaveBeenCalled();
expect(
getFirstInvalidVariableFromRosPath(rosPath, { not_yet_set_global_var: 5 }, setGlobalVars),
).toEqual(undefined);
expect(setGlobalVars).not.toHaveBeenCalled();
const getRosPathWithDefaultGlobalVar = (defaultKey: string): RosPath => ({
topicName: "/some_topic",
messagePath: [
{ type: "name", name: "fieldName" },
{ type: "slice", start: 0, end: Infinity },
{
type: "filter",
path: ["myId"],
value: { variableName: defaultKey, startLoc: 10 },
nameLoc: 11,
valueLoc: 10,
repr: `myId==$${defaultKey}`,
},
],
modifier: null,
});
Object.keys(defaultGlobalVars).forEach((defaultKey) => {
expect(
getFirstInvalidVariableFromRosPath(
getRosPathWithDefaultGlobalVar(defaultKey),
defaultGlobalVars,
setGlobalVars,
),
).toEqual(undefined);
expect(setGlobalVars).not.toHaveBeenCalled();
});
});
});