Skip to content

Commit 7bc63f1

Browse files
committed
add tests
1 parent 0a75265 commit 7bc63f1

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

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

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { describe, it, expect, beforeEach } from "vitest";
1+
import { beforeEach, describe, expect, it } from "vitest";
22
import { store } from "@/store";
3-
import { handleHostStoreChange } from "./handleHostStoreChange";
3+
import {
4+
getCallbackRequests,
5+
handleHostStoreChange,
6+
type PropertyRef,
7+
} from "./handleHostStoreChange";
8+
import type { ContribPoint } from "@/types/model/extension";
9+
import type { ContributionState } from "@/types/state/contribution";
410

511
describe("handleHostStoreChange", () => {
612
let listeners: (() => void)[] = [];
@@ -15,10 +21,12 @@ describe("handleHostStoreChange", () => {
1521
listeners.push(_l);
1622
},
1723
};
24+
let lastInputValues: Record<string, unknown[]> = {};
1825

1926
beforeEach(() => {
2027
listeners = [];
2128
hostState = {};
29+
lastInputValues = {};
2230
});
2331

2432
it("should do nothing without host store", () => {
@@ -82,4 +90,89 @@ describe("handleHostStoreChange", () => {
8290
hostStore.set("variableName", "CHL");
8391
handleHostStoreChange();
8492
});
93+
94+
it("should memoize second call with same arguments", () => {
95+
const extensions = [{ name: "e0", version: "0", contributes: ["panels"] }];
96+
store.setState({
97+
configuration: { hostStore, logging: { enabled: false } },
98+
extensions,
99+
contributionsResult: {
100+
status: "ok",
101+
data: {
102+
extensions,
103+
contributions: {
104+
panels: [
105+
{
106+
name: "ext.p1",
107+
extension: "ext",
108+
layout: {
109+
function: {
110+
name: "layout",
111+
parameters: [],
112+
return: {},
113+
},
114+
inputs: [],
115+
outputs: [],
116+
},
117+
callbacks: [
118+
{
119+
function: {
120+
name: "callback",
121+
parameters: [],
122+
return: {},
123+
},
124+
inputs: [{ id: "@app", property: "variableName" }],
125+
outputs: [{ id: "select", property: "value" }],
126+
},
127+
],
128+
initialState: {},
129+
},
130+
],
131+
},
132+
},
133+
},
134+
lastInputValues: lastInputValues,
135+
});
136+
hostStore.set("variableName", "CHL");
137+
const propertyRefs: PropertyRef[] = [
138+
{
139+
id: "panel-0-0-0",
140+
contribPoint: "panel",
141+
contribIndex: 0,
142+
callbackIndex: 0,
143+
property: "value",
144+
inputIndex: 0,
145+
},
146+
];
147+
const contributionsRecord: Record<ContribPoint, ContributionState[]> = {
148+
panel: [
149+
{
150+
name: "ext.p1",
151+
container: { title: "Panel A" },
152+
extension: "ext",
153+
componentResult: {},
154+
initialState: { title: "Panel A" },
155+
callbacks: [
156+
{
157+
function: {
158+
name: "callback",
159+
parameters: [{ name: "param1" }],
160+
return: {},
161+
},
162+
inputs: [{ id: "@app", property: "variableName" }],
163+
},
164+
],
165+
},
166+
],
167+
};
168+
const result = getCallbackRequests(
169+
propertyRefs,
170+
contributionsRecord,
171+
hostStore,
172+
);
173+
expect(result[0]).toEqual({
174+
...propertyRefs[0],
175+
inputValues: ["CHL"],
176+
});
177+
});
85178
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from "vitest";
2+
import { shallowEqualArrays } from "@/utils/compare";
3+
4+
describe("Test shallowEqualArrays()", () => {
5+
const arr_a: string[] = ["a", "b", "c"];
6+
const arr_b: string[] = ["a", "b", "c"];
7+
const arr_c: string[] = ["a", "b", "d"];
8+
const arr_d: string[] = [];
9+
const arr_e: string[] = ["a", "b", "c", "d"];
10+
it("works", () => {
11+
expect(shallowEqualArrays(arr_a, arr_b)).toBe(true);
12+
expect(shallowEqualArrays(arr_a, arr_c)).toBe(false);
13+
expect(shallowEqualArrays(arr_a, arr_d)).toBe(false);
14+
expect(shallowEqualArrays(arr_a, arr_e)).toBe(false);
15+
});
16+
});

0 commit comments

Comments
 (0)