Skip to content

Commit d3354c7

Browse files
committed
Add test
1 parent 878a7eb commit d3354c7

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

chartlets.js/packages/lib/src/actions/handleHostStoreChange.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
1313
import type { ContributionState } from "@/types/state/contribution";
1414
import type { HostStore } from "@/types/state/host";
1515
import { store } from "@/store";
16-
import { shallowEqualArrays } from "@/utils/compare";
16+
import { shallowEqualArrays } from "@/utils/shallowEqualArrays";
1717
import type { ContribPoint } from "@/types/model/extension";
1818

1919
/**
@@ -109,8 +109,9 @@ const getCallbackRequest = (
109109
/**
110110
* Get the static list of host state property references
111111
* for given contribution points.
112+
* Note: the export exists only for testing.
112113
*/
113-
const getPropertyRefsForContribPoints = memoize(
114+
export const getPropertyRefsForContribPoints = memoize(
114115
_getPropertyRefsForContribPoints,
115116
);
116117

chartlets.js/packages/lib/src/actions/handleHostStoreChanges.test.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it } from "vitest";
22
import { store } from "@/store";
33
import {
44
getCallbackRequests,
5+
getPropertyRefsForContribPoints,
56
handleHostStoreChange,
67
type PropertyRef,
78
} from "./handleHostStoreChange";
@@ -46,6 +47,76 @@ describe("handleHostStoreChange", () => {
4647
expect(store.getState().themeMode).toEqual("light");
4748
});
4849

50+
it("should memoize computation of property refs", () => {
51+
const contributionsRecord: Record<string, ContributionState[]> = {
52+
panels: [
53+
{
54+
name: "p0",
55+
container: { title: "Panel A" },
56+
extension: "e0",
57+
componentResult: {},
58+
initialState: {},
59+
callbacks: [
60+
{
61+
function: {
62+
name: "callback",
63+
parameters: [],
64+
return: {},
65+
},
66+
inputs: [{ id: "@app", property: "variableName" }],
67+
outputs: [{ id: "select", property: "value" }],
68+
},
69+
{
70+
function: {
71+
name: "callback2",
72+
parameters: [],
73+
return: {},
74+
},
75+
inputs: [
76+
{ id: "@app", property: "datasetId" },
77+
{ id: "@app", property: "variableName" },
78+
],
79+
outputs: [{ id: "plot", property: "value" }],
80+
},
81+
],
82+
},
83+
],
84+
};
85+
const propertyRefs1 = getPropertyRefsForContribPoints(contributionsRecord);
86+
const propertyRefs2 = getPropertyRefsForContribPoints(contributionsRecord);
87+
const propertyRefs3 = getPropertyRefsForContribPoints({
88+
...contributionsRecord,
89+
});
90+
expect(propertyRefs1).toBe(propertyRefs2);
91+
expect(propertyRefs2).not.toBe(propertyRefs3);
92+
expect(propertyRefs1).toEqual([
93+
{
94+
callbackIndex: 0,
95+
contribIndex: 0,
96+
contribPoint: "panels",
97+
inputIndex: 0,
98+
property: "variableName",
99+
},
100+
{
101+
callbackIndex: 1,
102+
contribIndex: 0,
103+
contribPoint: "panels",
104+
inputIndex: 0,
105+
property: "datasetId",
106+
},
107+
{
108+
callbackIndex: 1,
109+
contribIndex: 0,
110+
contribPoint: "panels",
111+
inputIndex: 1,
112+
property: "variableName",
113+
},
114+
]);
115+
expect(propertyRefs1).toEqual(propertyRefs2);
116+
expect(propertyRefs1).toEqual(propertyRefs3);
117+
expect(propertyRefs2).toEqual(propertyRefs3);
118+
});
119+
49120
it("should generate callback requests", () => {
50121
const extensions = [{ name: "e0", version: "0", contributes: ["panels"] }];
51122
store.setState({

0 commit comments

Comments
 (0)