Skip to content

Commit 3b8769d

Browse files
committed
Fixed tests
1 parent dc54528 commit 3b8769d

File tree

2 files changed

+40
-79
lines changed

2 files changed

+40
-79
lines changed

chartlets.js/src/lib/actions/helpers/getInputValues.test.ts

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,85 +24,48 @@ const componentState = {
2424

2525
describe("Test that getInputValueFromComponent()", () => {
2626
it("works on 1st level", () => {
27-
expect(
28-
getInputValueFromComponent(
29-
{
30-
id: "b1",
31-
property: "value",
32-
},
33-
componentState,
34-
),
35-
).toEqual(14);
27+
expect(getInputValueFromComponent(componentState, "b1", "value")).toEqual(
28+
14,
29+
);
3630
});
3731

3832
it("works on 2nd level", () => {
39-
expect(
40-
getInputValueFromComponent(
41-
{
42-
id: "p1",
43-
property: "chart",
44-
},
45-
componentState,
46-
),
47-
).toEqual(null);
33+
expect(getInputValueFromComponent(componentState, "p1", "chart")).toEqual(
34+
null,
35+
);
4836
});
4937

5038
it("works on 3rd level", () => {
51-
expect(
52-
getInputValueFromComponent(
53-
{
54-
id: "cb1",
55-
property: "value",
56-
},
57-
componentState,
58-
),
59-
).toEqual(true);
60-
61-
expect(
62-
getInputValueFromComponent(
63-
{
64-
id: "dd1",
65-
property: "value",
66-
},
67-
componentState,
68-
),
69-
).toEqual(13);
70-
});
71-
});
72-
73-
describe("Test that getInputValueFromState()", () => {
74-
it("works with input.id and input.property", () => {
75-
const state = { x: { y: 26 } };
76-
expect(getInputValueFromState({ id: "x", property: "y" }, state)).toEqual(
77-
26,
39+
expect(getInputValueFromComponent(componentState, "cb1", "value")).toEqual(
40+
true,
7841
);
79-
});
8042

81-
it("works with arrays indexes", () => {
82-
const state = { x: [4, 5, 6] };
83-
expect(getInputValueFromState({ id: "x", property: "1" }, state)).toEqual(
84-
5,
43+
expect(getInputValueFromComponent(componentState, "dd1", "value")).toEqual(
44+
13,
8545
);
8646
});
47+
});
8748

49+
describe("Test that getInputValueFromState()", () => {
8850
it("works without input.id", () => {
8951
const state = { x: [4, 5, 6] };
90-
expect(
91-
getInputValueFromState({ id: "@container", property: "x" }, state),
92-
).toEqual([4, 5, 6]);
52+
expect(getInputValueFromState(state, "x")).toEqual([4, 5, 6]);
9353
});
9454

9555
it("works on 2nd level", () => {
9656
const state = { x: { y: 15 } };
97-
expect(
98-
getInputValueFromState({ id: "@container", property: "x.y" }, state),
99-
).toEqual(15);
57+
expect(getInputValueFromState(state, "x.y")).toEqual(15);
10058
});
10159

10260
it("works on 3nd level", () => {
10361
const state = { x: { y: [4, 5, 6] } };
62+
expect(getInputValueFromState(state, "x.y.2")).toEqual(6);
63+
});
64+
65+
it("works with non-object states", () => {
66+
const state = 13;
10467
expect(
105-
getInputValueFromState({ id: "@container", property: "x.y.2" }, state),
106-
).toEqual(6);
68+
getInputValueFromState(state as unknown as object, "x.y.2"),
69+
).toBeUndefined();
10770
});
10871
});

chartlets.js/src/lib/actions/helpers/getInputValues.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
isComponentState,
1111
isContainerState,
1212
} from "@/lib/types/state/component";
13-
import { formatObjPath, getValue, normalizeObjPath } from "@/lib/utils/objPath";
13+
import { formatObjPath, getValue, type ObjPathLike } from "@/lib/utils/objPath";
1414
import { isObject } from "@/lib/utils/isObject";
1515
import type { HostStore } from "@/lib/types/state/options";
1616

@@ -32,12 +32,17 @@ export function getInputValue(
3232
hostStore?: HostStore,
3333
): unknown {
3434
let inputValue: unknown = undefined;
35+
const { id, property } = input;
3536
if (isComponentChannel(input) && contributionState.component) {
36-
inputValue = getInputValueFromComponent(input, contributionState.component);
37+
inputValue = getInputValueFromComponent(
38+
contributionState.component,
39+
id,
40+
property,
41+
);
3742
} else if (isContainerChannel(input) && contributionState.container) {
38-
inputValue = getInputValueFromState(input, contributionState.container);
43+
inputValue = getInputValueFromState(contributionState.container, property);
3944
} else if (isHostChannel(input) && hostStore) {
40-
inputValue = getInputValueFromHostStore(input, hostStore);
45+
inputValue = getInputValueFromHostStore(hostStore, property);
4146
} else {
4247
console.warn(`input with unknown data source:`, input);
4348
}
@@ -51,16 +56,17 @@ export function getInputValue(
5156

5257
// we export for testing only
5358
export function getInputValueFromComponent(
54-
input: Input,
5559
componentState: ComponentState,
60+
id: string,
61+
property: ObjPathLike,
5662
): unknown {
57-
if (componentState.id === input.id) {
58-
return getValue(componentState, input.property);
63+
if (componentState.id === id) {
64+
return getValue(componentState, property);
5965
} else if (isContainerState(componentState)) {
6066
for (let i = 0; i < componentState.children.length; i++) {
6167
const item = componentState.children[i];
6268
if (isComponentState(item)) {
63-
const itemValue = getInputValueFromComponent(input, item);
69+
const itemValue = getInputValueFromComponent(item, id, property);
6470
if (itemValue !== noValue) {
6571
return itemValue;
6672
}
@@ -72,24 +78,16 @@ export function getInputValueFromComponent(
7278

7379
// we export for testing only
7480
export function getInputValueFromState(
75-
input: Input,
7681
state: object | undefined,
82+
property: ObjPathLike,
7783
): unknown {
78-
let inputValue: unknown = state;
79-
if (input.id && isObject(inputValue)) {
80-
inputValue = inputValue[input.id];
81-
}
82-
if (isObject(inputValue)) {
83-
const state = inputValue;
84-
const property = normalizeObjPath(input.property);
85-
inputValue = getValue(state, property);
86-
}
87-
return inputValue;
84+
return isObject(state) ? getValue(state, property) : undefined;
8885
}
8986

87+
// we export for testing only
9088
export function getInputValueFromHostStore(
91-
input: Input,
9289
hostStore: HostStore,
90+
property: ObjPathLike,
9391
): unknown {
94-
return hostStore.get(formatObjPath(input.property));
92+
return hostStore.get(formatObjPath(property));
9593
}

0 commit comments

Comments
 (0)