Skip to content

Commit cdf60f7

Browse files
authored
🐛 fix(line): prevent Line Chart from crashing when having no data (#2669)
* fix + test * test refacto
1 parent 78a63f9 commit cdf60f7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/line/tests/Line.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ describe('mouse events on slices', () => {
435435
})
436436
})
437437

438-
describe('touch events with useMesh', () => {
438+
describe('events with useMesh', () => {
439439
const data = [
440440
{
441441
id: 'A',
@@ -457,13 +457,26 @@ describe('touch events with useMesh', () => {
457457
enableTouchCrosshair: true,
458458
}
459459

460+
it('should not throw onMouseEnter on empty data', () => {
461+
const onMouseEnter = jest.fn()
462+
const wrapper = mount(<Line {...baseProps} data={[]} onMouseEnter={onMouseEnter} />)
463+
expect(() =>
464+
wrapper.find(`[data-ref='mesh-interceptor']`).simulate('mouseenter', {
465+
clientX: 50,
466+
clientY: 50,
467+
})
468+
).not.toThrow()
469+
wrapper.unmount()
470+
})
471+
460472
it('should call onTouchStart', () => {
461473
const onTouchStart = jest.fn()
462474
const wrapper = mount(<Line {...baseProps} onTouchStart={onTouchStart} />)
463475
wrapper.find(`[data-ref='mesh-interceptor']`).simulate('touchstart', {
464476
touches: [{ clientX: 50, clientY: 50 }],
465477
})
466478
expect(onTouchStart).toHaveBeenCalledTimes(1)
479+
wrapper.unmount()
467480
})
468481

469482
it('should call onTouchMove', () => {
@@ -473,6 +486,7 @@ describe('touch events with useMesh', () => {
473486
touches: [{ clientX: 50, clientY: 50 }],
474487
})
475488
expect(onTouchMove).toHaveBeenCalledTimes(1)
489+
wrapper.unmount()
476490
})
477491

478492
it('should call onTouchEnd', () => {
@@ -485,6 +499,7 @@ describe('touch events with useMesh', () => {
485499
})
486500
.simulate('touchend')
487501
expect(onTouchEnd).toHaveBeenCalledTimes(1)
502+
wrapper.unmount()
488503
})
489504
})
490505

packages/voronoi/src/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export const useMeshEvents = <Node, ElementType extends Element>({
171171

172172
const findNode = useCallback(
173173
(event: MouseEvent<ElementType> | TouchEvent<ElementType>): null | [number, Node] => {
174-
if (!elementRef.current) return null
174+
if (!elementRef.current || nodes.length === 0) return null
175175

176176
const [x, y] = getRelativeCursor(elementRef.current, event)
177177

0 commit comments

Comments
 (0)