From 72dc4eaea00417b12fd3eadaeb639cbfeeb59b8c Mon Sep 17 00:00:00 2001 From: Theo Macron Date: Wed, 8 Jan 2025 10:11:34 +0100 Subject: [PATCH] fixup! charts: add weight property to operational points --- .../src/__tests__/utils.spec.ts | 51 +++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/ui-speedspacechart/src/__tests__/utils.spec.ts b/ui-speedspacechart/src/__tests__/utils.spec.ts index 9d1f41492..1655c9510 100644 --- a/ui-speedspacechart/src/__tests__/utils.spec.ts +++ b/ui-speedspacechart/src/__tests__/utils.spec.ts @@ -377,7 +377,7 @@ describe('filterVisibleElements', () => { const getPosition = (element: Element) => element.position; const getWeight = (element: Element) => element.weight; - it('filters visible elements based on minSpace', () => { + it('should filter visible elements based on minSpace', () => { const options: VisibilityFilterOptions = { elements, getPosition, @@ -394,7 +394,7 @@ describe('filterVisibleElements', () => { ]); }); - it('returns all elements if minSpace is 0', () => { + it('should return all elements if minSpace is 0', () => { const options: VisibilityFilterOptions = { elements, getPosition, @@ -408,7 +408,7 @@ describe('filterVisibleElements', () => { expect(result).toEqual(elements.sort((a, b) => a.position - b.position)); }); - it('returns an empty array if no elements are provided', () => { + it('should return an empty array if no elements are provided', () => { const options: VisibilityFilterOptions = { elements: [], getPosition, @@ -421,7 +421,7 @@ describe('filterVisibleElements', () => { expect(result).toEqual([]); }); - it('prioritizes higher weights when positions overlap', () => { + it('should prioritize higher weights when positions overlap', () => { const overlappingElements: Element[] = [ { id: 1, position: 10, weight: 5 }, { id: 2, position: 12, weight: 6 }, // Overlaps with id: 1 @@ -443,6 +443,49 @@ describe('filterVisibleElements', () => { { id: 3, position: 25, weight: 4 }, ]); }); + + it('should prioritize elements with higher weights when positions are identical', () => { + const elementsWithSamePosition: Element[] = [ + { id: 1, position: 10, weight: 3 }, + { id: 2, position: 10, weight: 5 }, + { id: 3, position: 10, weight: 1 }, + ]; + + const options: VisibilityFilterOptions = { + elements: elementsWithSamePosition, + getPosition, + getWeight, + minSpace: 5, + }; + + const result = filterVisibleElements(options); + + expect(result).toEqual([ + { id: 2, position: 10, weight: 5 }, // Highest weight + ]); + }); + + it('should handle elements with equal weights but conflicting positions', () => { + const equalWeightElements: Element[] = [ + { id: 1, position: 10, weight: 5 }, + { id: 2, position: 12, weight: 5 }, // Same weight, close position + { id: 3, position: 30, weight: 5 }, + ]; + + const options: VisibilityFilterOptions = { + elements: equalWeightElements, + getPosition, + getWeight, + minSpace: 10, + }; + + const result = filterVisibleElements(options); + + expect(result).toEqual([ + { id: 1, position: 10, weight: 5 }, + { id: 3, position: 30, weight: 5 }, + ]); + }); }); describe('getSnappedStop', () => {