Skip to content

Commit 47d7290

Browse files
committed
fix tests
1 parent 6800e42 commit 47d7290

File tree

7 files changed

+80
-172
lines changed

7 files changed

+80
-172
lines changed

app/components/Groups.test.tsx

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
import { render } from "@testing-library/react";
22
import Groups from "@/app/components/Groups";
3-
import { IfcGroup } from "@/app/types";
3+
import { tBlockMapping, tGroups } from "@/app/types";
44

55
it("renders groups correctly with hidden and non hidden groups", () => {
6-
const shownGroupWithTwoBlocks: IfcGroup = {
7-
name: "group1",
8-
blocks: [
9-
{
10-
pvaddress: "A:SHOWN:BLOCK",
11-
human_readable_name: "aShownBlock",
12-
value: 2.344,
13-
visible: true,
14-
},
15-
{
16-
pvaddress: "ADIFF:SHOWN:BLOCK",
17-
human_readable_name: "aDifferentShownBlock",
18-
value: 3.14,
19-
visible: true,
20-
},
21-
],
22-
};
23-
const hiddenGroupWithOneBlock: IfcGroup = {
24-
name: "group2",
25-
blocks: [
26-
{
27-
pvaddress: "A:HIDDEN:BLOCK",
28-
human_readable_name: "aHiddenBlock",
29-
visible: false,
30-
},
31-
],
32-
};
6+
let groups: tGroups = new Map();
7+
let blocksForGroup1: tBlockMapping = new Map();
8+
blocksForGroup1.set("A:SHOWN:BLOCK", {
9+
pvaddress: "A:SHOWN:BLOCK",
10+
human_readable_name: "aShownBlock",
11+
value: 2.344,
12+
visible: true,
13+
});
14+
blocksForGroup1.set("ADIFF:SHOWN:BLOCK", {
15+
pvaddress: "ADIFF:SHOWN:BLOCK",
16+
human_readable_name: "aDifferentShownBlock",
17+
value: 3.14,
18+
visible: true,
19+
});
20+
groups.set("group1", blocksForGroup1);
21+
22+
let blocksForGroup2: tBlockMapping = new Map();
23+
blocksForGroup2.set("A:HIDDEN:BLOCK", {
24+
pvaddress: "A:HIDDEN:BLOCK",
25+
human_readable_name: "aHiddenBlock",
26+
visible: false,
27+
});
28+
groups.set("group2", blocksForGroup2);
29+
3330
const { container } = render(
34-
<Groups
35-
instName={"TESTING"}
36-
showHiddenBlocks={false}
37-
groupsMap={[shownGroupWithTwoBlocks, hiddenGroupWithOneBlock]}
38-
/>,
31+
<Groups instName={"TESTING"} showHiddenBlocks={false} groupsMap={groups} />,
3932
);
4033
expect(container).toMatchSnapshot();
4134
});

app/components/Instrument.test.ts

Lines changed: 40 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
CSSB,
3-
findPVInDashboard,
43
findPVInGroups,
54
getGroupsWithBlocksFromConfigOutput,
65
RC_ENABLE,
@@ -14,98 +13,23 @@ import {
1413
} from "@/app/components/Instrument";
1514
import {
1615
ConfigOutput,
17-
DashboardArr,
1816
IfcBlock,
19-
IfcGroup,
20-
IfcPV,
2117
IfcPVWSMessage,
18+
tBlockMapping,
19+
tGroups,
2220
} from "@/app/types";
2321

24-
test("findPVinDashboard finds a pv in the dashboard and returns it", () => {
25-
const prefix = "UNITTESTING";
26-
const pvToTestFor: IfcPV = { pvaddress: `${prefix}1:1:LABEL` };
27-
let dashboard: DashboardArr = [
28-
//column 0
29-
[
30-
[{ pvaddress: "", value: "Title:" }, { pvaddress: `${prefix}DAE:TITLE` }],
31-
[
32-
{ pvaddress: "", value: "Users:" },
33-
{ pvaddress: `${prefix}DAE:_USERNAME` },
34-
],
35-
],
36-
//column 1
37-
[
38-
[pvToTestFor, { pvaddress: `${prefix}1:1:VALUE` }],
39-
[
40-
{ pvaddress: `${prefix}2:1:LABEL` },
41-
{ pvaddress: `${prefix}2:1:VALUE` },
42-
],
43-
[
44-
{ pvaddress: `${prefix}3:1:LABEL` },
45-
{ pvaddress: `${prefix}3:1:VALUE` },
46-
],
47-
],
48-
//column 2
49-
[
50-
[
51-
{ pvaddress: `${prefix}1:2:LABEL` },
52-
{ pvaddress: `${prefix}1:2:VALUE` },
53-
],
54-
[
55-
{ pvaddress: `${prefix}2:2:LABEL` },
56-
{ pvaddress: `${prefix}2:2:VALUE` },
57-
],
58-
[
59-
{ pvaddress: `${prefix}3:2:LABEL` },
60-
{ pvaddress: `${prefix}3:2:VALUE` },
61-
],
62-
],
63-
];
64-
65-
const result = findPVInDashboard(dashboard, pvToTestFor.pvaddress);
66-
expect(result).toBe(pvToTestFor);
67-
});
68-
69-
test("findPVinDashboard does not find a PV in the dashboard and returns undefined", () => {
70-
const prefix = "UNITTESTING";
71-
const pvToTestFor: IfcPV = { pvaddress: `${prefix}1:4:LABEL` };
72-
let dashboard: DashboardArr = [
73-
[
74-
[
75-
{ pvaddress: `${prefix}1:2:LABEL` },
76-
{ pvaddress: `${prefix}1:2:VALUE` },
77-
],
78-
[
79-
{ pvaddress: `${prefix}2:2:LABEL` },
80-
{ pvaddress: `${prefix}2:2:VALUE` },
81-
],
82-
[
83-
{ pvaddress: `${prefix}3:2:LABEL` },
84-
{ pvaddress: `${prefix}3:2:VALUE` },
85-
],
86-
],
87-
];
88-
89-
const result = findPVInDashboard(dashboard, pvToTestFor.pvaddress);
90-
expect(result).toBe(undefined);
91-
});
92-
9322
test("findPVInGroups returns a block when it finds one", () => {
9423
const blockName = "blockName";
9524
const prefix = "IN:INSTRUMENT";
96-
const groups: Array<IfcGroup> = [
97-
{
98-
name: "aGroup",
99-
blocks: [
100-
{
101-
human_readable_name: blockName,
102-
pvaddress: "some:underlying:pv:name",
103-
},
104-
],
105-
},
106-
];
107-
108-
findPVInGroups(groups, prefix, prefix + CSSB + blockName);
25+
let groups: tGroups = new Map();
26+
let group1Blocks: tBlockMapping = new Map();
27+
group1Blocks.set(blockName, {
28+
human_readable_name: blockName,
29+
pvaddress: "some:underlying:pv:name",
30+
});
31+
groups.set("aGroup", group1Blocks);
32+
findPVInGroups(groups, prefix + CSSB + blockName);
10933
});
11034

11135
test("toPrecision does nothing to string value ", () => {
@@ -150,6 +74,7 @@ test("yesToBoolean works with NO as value", () => {
15074
test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups", () => {
15175
const blockNameToTest = "aBlock";
15276
const groupNameToTest = "aGroup";
77+
const prefix = "TESTING:";
15378

15479
const configOutput: ConfigOutput = {
15580
groups: [{ blocks: [blockNameToTest], name: groupNameToTest }],
@@ -158,7 +83,7 @@ test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups",
15883
name: blockNameToTest,
15984
component: "",
16085
local: true,
161-
pv: "A:BLOCK",
86+
pv: prefix + "A:BLOCK",
16287
set_block: false,
16388
highlimit: 0,
16489
lowlimit: 0,
@@ -181,9 +106,11 @@ test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups",
181106
component_iocs: [],
182107
history: [],
183108
};
184-
const groups = getGroupsWithBlocksFromConfigOutput(configOutput);
185-
expect(groups[0].name).toBe(groupNameToTest);
186-
expect(groups[0].blocks[0].human_readable_name).toBe(blockNameToTest);
109+
const groups = getGroupsWithBlocksFromConfigOutput(prefix, configOutput);
110+
expect(Array.from(groups.keys())[0]).toBe(groupNameToTest);
111+
expect(
112+
Array.from(Array.from(groups.values())[0].values())[0].human_readable_name,
113+
).toBe(blockNameToTest);
187114
});
188115

189116
test("subscribeToBlockPVs subscribes to blocks, their run control and their SP:RBV PVs", () => {
@@ -211,47 +138,40 @@ test("storePrecision adds precision to a block if it is the first update", () =>
211138
});
212139

213140
test("getAllBlockPVs returns flat list of blocks, their RC and SPRBV pvs", () => {
214-
const block1Name = "blockName";
215-
const block2Name = "block2Name";
216141
const prefix = "IN:TEST:";
142+
const block1Name = prefix + CSSB + "blockName";
143+
const block2Name = prefix + CSSB + "block2Name";
217144

218145
let inst = new Instrument(prefix);
219146

220-
inst.groups = [
221-
{
222-
name: "aGroup",
223-
blocks: [
224-
{
225-
human_readable_name: block1Name,
226-
pvaddress: "some:underlying:pv:name",
227-
},
228-
],
229-
},
230-
{
231-
name: "aDifferentGroup",
232-
blocks: [
233-
{
234-
human_readable_name: block2Name,
235-
pvaddress: "someother:underlying:pv:name",
236-
},
237-
],
238-
},
239-
];
147+
let group1Blocks: tBlockMapping = new Map();
148+
group1Blocks.set(block1Name, {
149+
human_readable_name: block1Name,
150+
pvaddress: "some:underlying:pv:name",
151+
});
152+
inst.groups.set("aGroup", group1Blocks);
153+
154+
let group2Blocks: tBlockMapping = new Map();
155+
group2Blocks.set(block2Name, {
156+
human_readable_name: block2Name,
157+
pvaddress: "someother:underlying:pv:name",
158+
});
159+
inst.groups.set("aDifferentGroup", group2Blocks);
240160

241161
const res = inst.getAllBlockPVs();
242162

243163
expect(res.length).toBe(2 * 4); // 2 blocks, which means 4 PVs to subscribe to
244164

245165
expect(res).toEqual(
246166
expect.arrayContaining([
247-
prefix + CSSB + block1Name,
248-
prefix + CSSB + block1Name + RC_ENABLE,
249-
prefix + CSSB + block1Name + RC_INRANGE,
250-
prefix + CSSB + block1Name + SP_RBV,
251-
prefix + CSSB + block2Name,
252-
prefix + CSSB + block2Name + RC_ENABLE,
253-
prefix + CSSB + block2Name + RC_INRANGE,
254-
prefix + CSSB + block2Name + SP_RBV,
167+
block1Name,
168+
block1Name + RC_ENABLE,
169+
block1Name + RC_INRANGE,
170+
block1Name + SP_RBV,
171+
block2Name,
172+
block2Name + RC_ENABLE,
173+
block2Name + RC_INRANGE,
174+
block2Name + SP_RBV,
255175
]),
256176
);
257177
});

app/components/TopBar.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import TopBar, { getRunstate, runStateStr } from "@/app/components/TopBar";
1+
import { exportedForTesting, getRunstate } from "@/app/components/TopBar";
22
import { tBlockMapping } from "@/app/types";
33
import { render } from "@testing-library/react";
44
import { Instrument } from "@/app/components/Instrument";
@@ -11,7 +11,7 @@ test("GetRunstate returns the runstate when it exists and is of string type", ()
1111
blocks.set(prefix + "DAE:RUNSTATE_STR", {
1212
pvaddress: prefix + "DAE:RUNSTATE_STR",
1313
value: expected,
14-
human_readable_name: runStateStr,
14+
human_readable_name: "Run state",
1515
});
1616
expect(getRunstate(prefix, blocks)).toBe(expected);
1717
});
@@ -26,7 +26,7 @@ it("renders topbar unchanged", () => {
2626
let instrument = new Instrument(prefix);
2727
const instName = "Instrument";
2828
const { container } = render(
29-
TopBar({
29+
exportedForTesting({
3030
dashboard: instrument.dashboard,
3131
instName: instName,
3232
runInfoPVs: instrument.runInfoPVs,
@@ -42,7 +42,7 @@ it("draws instName expectedly", () => {
4242
let instrument = new Instrument(prefix);
4343
const instName = "Instrument";
4444
const { container } = render(
45-
TopBar({
45+
exportedForTesting({
4646
dashboard: instrument.dashboard,
4747
instName: instName,
4848
runInfoPVs: instrument.runInfoPVs,
@@ -63,7 +63,7 @@ it("draws configName expectedly", () => {
6363
const expectedConfigName = "Aconfig";
6464
configNamePV!.value = expectedConfigName;
6565
const { container } = render(
66-
TopBar({
66+
exportedForTesting({
6767
dashboard: instrument.dashboard,
6868
instName: "Instrument",
6969
runInfoPVs: instrument.runInfoPVs,

app/components/TopBar.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import { tBlockMapping } from "@/app/types";
88
import { DASHBOARD } from "@/app/components/Instrument";
99
import { memo } from "react";
1010

11-
export const runStateStr = "Run state";
12-
export const configName = "Config name";
13-
1411
export function getRunstate(prefix: string, runInfoPVs: tBlockMapping): string {
1512
const runStatePV = runInfoPVs.get(`${prefix}DAE:RUNSTATE_STR`);
1613
if (runStatePV && runStatePV.value && typeof runStatePV.value === "string") {
@@ -19,7 +16,7 @@ export function getRunstate(prefix: string, runInfoPVs: tBlockMapping): string {
1916
return UNREACHABLE;
2017
}
2118

22-
const TopBar = memo(function TopBar({
19+
const TopBar = function TopBar({
2320
dashboard,
2421
instName,
2522
runInfoPVs,
@@ -152,6 +149,7 @@ const TopBar = memo(function TopBar({
152149
</div>
153150
</div>
154151
);
155-
});
152+
};
156153

157-
export default TopBar;
154+
export default memo(TopBar);
155+
export const exportedForTesting = TopBar;

app/components/__snapshots__/Groups.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ exports[`renders groups correctly with hidden and non hidden groups 1`] = `
7171
7272
</span>
7373
<svg
74-
class="min-w-2 min-h-2 max-w-2 max-h-2 transition-all text-transparent"
74+
class="min-w-2 min-h-2 max-w-2 max-h-2 transition-opacity text-green-500 opacity-100"
7575
fill="currentColor"
7676
id="aShownBlock_CIRCLE"
7777
viewBox="0 0 24 24"
@@ -132,7 +132,7 @@ exports[`renders groups correctly with hidden and non hidden groups 1`] = `
132132
133133
</span>
134134
<svg
135-
class="min-w-2 min-h-2 max-w-2 max-h-2 transition-all text-transparent"
135+
class="min-w-2 min-h-2 max-w-2 max-h-2 transition-opacity text-green-500 opacity-100"
136136
fill="currentColor"
137137
id="aDifferentShownBlock_CIRCLE"
138138
viewBox="0 0 24 24"

0 commit comments

Comments
 (0)