Skip to content

Commit 2984f3f

Browse files
committed
chore: add test for tooltip plugin
1 parent bbfd57a commit 2984f3f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/__tests__/SolidUplot.test.tsx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,4 +987,87 @@ describe("PLUGIN: tooltip", () => {
987987
expect(document.querySelector("#solid-uplot-tooltip-root")).not.toBeInTheDocument();
988988
});
989989
});
990+
991+
test("calls onPositionCalculated callback with position and placement", async () => {
992+
const bus = createPluginBus<CursorPluginMessageBus>({
993+
cursor: {
994+
state: {},
995+
},
996+
});
997+
998+
let uPlotInstance!: uPlot;
999+
1000+
const onPositionCalculatedSpy = vi.fn(
1001+
(position: { left: number; top: number }, _placement: string) => ({
1002+
left: position.left + 50, // Add custom offset
1003+
top: position.top + 25,
1004+
}),
1005+
);
1006+
1007+
render(() => (
1008+
<SolidUplot
1009+
width={600}
1010+
height={300}
1011+
data={[
1012+
[1, 2, 3],
1013+
[4, 5, 6],
1014+
]}
1015+
series={[{}, { label: "Series 1" }]}
1016+
pluginBus={bus}
1017+
plugins={[
1018+
cursor(),
1019+
tooltip(MockTooltip, {
1020+
placement: "top-left",
1021+
onPositionCalculated: onPositionCalculatedSpy,
1022+
}),
1023+
]}
1024+
onCreate={(chart) => {
1025+
uPlotInstance = chart;
1026+
}}
1027+
/>
1028+
));
1029+
1030+
await waitFor(() => expect(uPlotInstance).toBeDefined());
1031+
1032+
// Set cursor data to trigger tooltip positioning
1033+
bus.setData("cursor", {
1034+
sourceId: uPlotInstance.root.id,
1035+
state: {
1036+
[uPlotInstance.root.id]: {
1037+
plotId: uPlotInstance.root.id,
1038+
idx: 1,
1039+
xValue: 2,
1040+
visible: true,
1041+
position: { top: 100, left: 200 },
1042+
},
1043+
},
1044+
});
1045+
1046+
await waitFor(() => {
1047+
const tooltip = document.querySelector("#solid-uplot-tooltip-root") as HTMLElement;
1048+
expect(tooltip).toBeInTheDocument();
1049+
});
1050+
1051+
// Verify the callback was called with the correct arguments
1052+
await waitFor(() => {
1053+
expect(onPositionCalculatedSpy).toHaveBeenCalled();
1054+
});
1055+
1056+
const callArgs = onPositionCalculatedSpy.mock.calls[0]!;
1057+
expect(callArgs).toBeDefined();
1058+
1059+
const [position, placement] = callArgs;
1060+
expect(position).toHaveProperty("left");
1061+
expect(position).toHaveProperty("top");
1062+
expect(placement).toBe("top-left");
1063+
1064+
// Verify the modified position was applied
1065+
const tooltipElement = document.querySelector("#solid-uplot-tooltip-root") as HTMLElement;
1066+
const leftValue = parseInt(tooltipElement.style.left);
1067+
const topValue = parseInt(tooltipElement.style.top);
1068+
1069+
// The callback adds 50 to left and 25 to top
1070+
expect(leftValue).toBe(position.left + 50);
1071+
expect(topValue).toBe(position.top + 25);
1072+
});
9901073
});

0 commit comments

Comments
 (0)