Skip to content

Commit 3ba54dc

Browse files
committed
testing handleHostStoreChange()
1 parent 18f17fb commit 3ba54dc

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { formatObjPath } from "@/utils/objPath";
1111
import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
1212
import type { ContributionState } from "@/types/state/contribution";
1313
import type { HostStore } from "@/types/state/options";
14+
import type { store } from "@/store";
1415

1516
/**
1617
* A reference to a property of an input of a callback of a contribution.
@@ -23,12 +24,16 @@ export interface PropertyRef extends ContribRef, CallbackRef, InputRef {
2324
export function handleHostStoreChange() {
2425
const { extensions, configuration, contributionsRecord } = store.getState();
2526
const { hostStore } = configuration;
26-
if (!hostStore || extensions.length === 0) {
27-
// Exit if no host store configured or
28-
// there are no extensions (yet)
27+
if (!hostStore) {
28+
// Exit if no host store configured.
29+
// Actually, we should not come here.
2930
return;
3031
}
3132
synchronizeThemeMode(hostStore);
33+
if (extensions.length === 0) {
34+
// Exit if there are no extensions (yet)
35+
return;
36+
}
3237
const propertyRefs = getHostStorePropertyRefs();
3338
if (!propertyRefs || propertyRefs.length === 0) {
3439
// Exit if there are is nothing to be changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, it, expect, beforeEach } from "vitest";
2+
import { store } from "@/store";
3+
import { handleHostStoreChange } from "./handleHostStoreChange";
4+
5+
describe("handleHostStoreChange", () => {
6+
let listeners: (() => void)[] = [];
7+
let hostState: Record<string, unknown> = {};
8+
const hostStore = {
9+
get: (key: string) => hostState[key],
10+
set: (key: string, value: unknown) => {
11+
hostState = { ...hostState, [key]: value };
12+
listeners.forEach((l) => void l());
13+
},
14+
subscribe: (_l: () => void) => {
15+
listeners.push(_l);
16+
},
17+
};
18+
19+
beforeEach(() => {
20+
listeners = [];
21+
hostState = {};
22+
});
23+
24+
it("should do nothing without host store", () => {
25+
store.setState({ configuration: {} });
26+
const oldState = store.getState();
27+
handleHostStoreChange();
28+
const newState = store.getState();
29+
expect(newState).toBe(oldState);
30+
expect(newState).toEqual(oldState);
31+
});
32+
33+
it("should synchronize theme mode", () => {
34+
store.setState({ configuration: { hostStore } });
35+
expect(store.getState().themeMode).toBeUndefined();
36+
hostStore.set("themeMode", "light");
37+
handleHostStoreChange();
38+
expect(store.getState().themeMode).toEqual("light");
39+
});
40+
41+
it("should generate callback requests", () => {
42+
const extensions = [{ name: "e0", version: "0", contributes: ["panels"] }];
43+
store.setState({
44+
configuration: { hostStore },
45+
extensions,
46+
contributionsResult: {
47+
status: "ok",
48+
data: {
49+
extensions,
50+
contributions: {
51+
panels: [
52+
{
53+
name: "p0",
54+
extension: "e0",
55+
layout: {
56+
function: {
57+
name: "layout",
58+
parameters: [],
59+
return: {},
60+
},
61+
inputs: [],
62+
outputs: [],
63+
},
64+
callbacks: [
65+
{
66+
function: {
67+
name: "callback",
68+
parameters: [],
69+
return: {},
70+
},
71+
inputs: [{ id: "@app", property: "variableName" }],
72+
outputs: [{ id: "select", property: "value" }],
73+
},
74+
],
75+
initialState: {},
76+
},
77+
],
78+
},
79+
},
80+
},
81+
});
82+
hostStore.set("variableName", "CHL");
83+
handleHostStoreChange();
84+
});
85+
});

0 commit comments

Comments
 (0)