From 69fc4989d8f77f1c53ada67886a80f2e7790897f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=E1=BA=A7n=20=C4=90=C3=ACnh=20Huy?= Date: Sat, 22 Aug 2026 22:32:41 +0700 Subject: [PATCH] fix: handle graph points sharing one pixel --- src/CreateGraphPath.ts | 6 +++- src/__tests__/CreateGraphPath.test.ts | 45 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/CreateGraphPath.test.ts diff --git a/src/CreateGraphPath.ts b/src/CreateGraphPath.ts index 5676695..0c7f1c0 100644 --- a/src/CreateGraphPath.ts +++ b/src/CreateGraphPath.ts @@ -162,7 +162,11 @@ function createGraphPathBase({ horizontalPadding; const getGraphDataIndex = (pixel: number) => - Math.round(((pixel - startX) / (endX - startX)) * (graphData.length - 1)); + endX === startX + ? 0 + : Math.round( + ((pixel - startX) / (endX - startX)) * (graphData.length - 1) + ); const getNextPixelValue = (pixel: number) => { if (pixel === endX || pixel + PIXEL_RATIO < endX) diff --git a/src/__tests__/CreateGraphPath.test.ts b/src/__tests__/CreateGraphPath.test.ts new file mode 100644 index 0000000..dc015dd --- /dev/null +++ b/src/__tests__/CreateGraphPath.test.ts @@ -0,0 +1,45 @@ +const mockPath = { + copy: jest.fn(), + cubicTo: jest.fn(), + lineTo: jest.fn(), + moveTo: jest.fn(), +}; + +jest.mock('@shopify/react-native-skia', () => ({ + Skia: { + Path: { + Make: () => mockPath, + }, + }, +})); + +import { createGraphPath } from '../CreateGraphPath'; + +beforeEach(() => jest.clearAllMocks()); + +it('creates a finite path when every graph point maps to the same pixel', () => { + const points = [ + { date: new Date(2024, 1, 1), value: 10 }, + { date: new Date(2024, 2, 1), value: 100 }, + ]; + + expect(() => + createGraphPath({ + pointsInRange: points, + range: { + x: { + min: new Date(2024, 1, 1), + max: new Date(2070, 2, 1), + }, + y: { min: 0, max: 100 }, + }, + horizontalPadding: 0, + verticalPadding: 0, + canvasHeight: 200, + canvasWidth: 300, + }) + ).not.toThrow(); + + expect(mockPath.moveTo).toHaveBeenCalledTimes(1); + expect(mockPath.moveTo.mock.calls[0]?.every(Number.isFinite)).toBe(true); +});