Skip to content

Commit 21b115a

Browse files
committed
Refined test
1 parent d45e7f0 commit 21b115a

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

chartlets.js/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* Static information about callbacks retrieved from API is not cached
77
reducing unnecessary processing and improving performance. (#113)
88

9+
* Relaxed requirements for adding new components to Chartlets.js via
10+
plugins. Implementing `ComponentProps` is now optional. (#115)
11+
912
* Callbacks will now only be invoked when there’s an actual change in state,
1013
reducing unnecessary processing and improving performance. (#112)
1114

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,31 @@ export interface PropertyRef extends ContribRef, CallbackRef, InputRef {
2424
property: string;
2525
}
2626

27-
export function handleHostStoreChange() {
27+
/**
28+
* This action is called once a host store change has been detected.
29+
* Note this will only create callback requests for callbacks whose input
30+
* value are affected by the change.
31+
*
32+
* @returns The array of callback requests made or `undefined`,
33+
* if no callback requests have been made.
34+
*/
35+
export function handleHostStoreChange(): CallbackRequest[] | undefined {
2836
const { extensions, configuration, contributionsRecord } = store.getState();
2937
const { hostStore } = configuration;
3038
if (!hostStore) {
3139
// Exit if no host store configured.
3240
// Actually, we should not come here.
33-
return;
41+
return undefined;
3442
}
3543
synchronizeThemeMode(hostStore);
3644
if (extensions.length === 0) {
3745
// Exit if there are no extensions (yet)
38-
return;
46+
return undefined;
3947
}
4048
const propertyRefs = getPropertyRefsForContribPoints(contributionsRecord);
4149
if (!propertyRefs || propertyRefs.length === 0) {
4250
// Exit if there are is nothing to be changed
43-
return;
51+
return undefined;
4452
}
4553
const callbackRequests = getCallbackRequests(
4654
propertyRefs,
@@ -52,9 +60,12 @@ export function handleHostStoreChange() {
5260
(callbackRequest): callbackRequest is CallbackRequest =>
5361
callbackRequest !== undefined,
5462
);
55-
if (filteredCallbackRequests && filteredCallbackRequests.length > 0) {
56-
invokeCallbacks(filteredCallbackRequests);
63+
if (!filteredCallbackRequests || !filteredCallbackRequests.length) {
64+
return undefined;
5765
}
66+
67+
invokeCallbacks(filteredCallbackRequests);
68+
return filteredCallbackRequests;
5869
}
5970

6071
// Exporting for testing only

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { ContributionState } from "@/types/state/contribution";
1212
describe("handleHostStoreChange", () => {
1313
let listeners: (() => void)[] = [];
1414
let hostState: Record<string, unknown> = {};
15+
// noinspection JSUnusedGlobalSymbols
1516
const hostStore = {
1617
get: (key: string) => hostState[key],
1718
set: (key: string, value: unknown) => {
@@ -181,12 +182,20 @@ describe("handleHostStoreChange", () => {
181182
},
182183
});
183184
hostStore.set("variableName", "CHL");
184-
handleHostStoreChange();
185+
expect(handleHostStoreChange()).toEqual([
186+
{
187+
contribPoint: "panel",
188+
contribIndex: 0,
189+
callbackIndex: 0,
190+
inputIndex: 0,
191+
inputValues: ["CHL"],
192+
property: "variableName",
193+
},
194+
]);
185195

186-
// calling it second time for coverage. No state change changes the
187-
// control flow
188-
handleHostStoreChange();
189-
// TODO: Update this test to assert the generated callback request
196+
// calling it second time for coverage,
197+
// because the no-state-change changes the control flow
198+
expect(handleHostStoreChange()).toBeUndefined();
190199
});
191200

192201
it("should memoize second call with same arguments", () => {

0 commit comments

Comments
 (0)