Skip to content

Commit

Permalink
🐛 fix(line): prevent Line Chart from crashing when having no data (#2669
Browse files Browse the repository at this point in the history
)

* fix + test

* test refacto
  • Loading branch information
akassaei authored Nov 15, 2024
1 parent 78a63f9 commit cdf60f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion packages/line/tests/Line.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ describe('mouse events on slices', () => {
})
})

describe('touch events with useMesh', () => {
describe('events with useMesh', () => {
const data = [
{
id: 'A',
Expand All @@ -457,13 +457,26 @@ describe('touch events with useMesh', () => {
enableTouchCrosshair: true,
}

it('should not throw onMouseEnter on empty data', () => {
const onMouseEnter = jest.fn()
const wrapper = mount(<Line {...baseProps} data={[]} onMouseEnter={onMouseEnter} />)
expect(() =>
wrapper.find(`[data-ref='mesh-interceptor']`).simulate('mouseenter', {
clientX: 50,
clientY: 50,
})
).not.toThrow()
wrapper.unmount()
})

it('should call onTouchStart', () => {
const onTouchStart = jest.fn()
const wrapper = mount(<Line {...baseProps} onTouchStart={onTouchStart} />)
wrapper.find(`[data-ref='mesh-interceptor']`).simulate('touchstart', {
touches: [{ clientX: 50, clientY: 50 }],
})
expect(onTouchStart).toHaveBeenCalledTimes(1)
wrapper.unmount()
})

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

it('should call onTouchEnd', () => {
Expand All @@ -485,6 +499,7 @@ describe('touch events with useMesh', () => {
})
.simulate('touchend')
expect(onTouchEnd).toHaveBeenCalledTimes(1)
wrapper.unmount()
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/voronoi/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const useMeshEvents = <Node, ElementType extends Element>({

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

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

Expand Down

0 comments on commit cdf60f7

Please sign in to comment.