From 515d3012aed7802130e98eaba90bf25177ebf78f Mon Sep 17 00:00:00 2001 From: Aria Kassaei Date: Thu, 14 Nov 2024 18:29:51 +0100 Subject: [PATCH 1/2] fix + test --- packages/line/tests/Line.test.js | 18 +++++++++++++++++- packages/voronoi/src/hooks.ts | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/line/tests/Line.test.js b/packages/line/tests/Line.test.js index 49af2d27b..1879c7031 100644 --- a/packages/line/tests/Line.test.js +++ b/packages/line/tests/Line.test.js @@ -6,11 +6,13 @@ import { LINE_UNIQUE_ID_PREFIX } from '../src/hooks' import SlicesItem from '../src/SlicesItem' import renderer from 'react-test-renderer' import { DotsItem } from '@nivo/core' +import { shallow } from 'enzyme' // Handle useId mocks let id = 0 beforeEach(() => { id = 0 + jest.resetAllMocks() }) const generateId = () => `${LINE_UNIQUE_ID_PREFIX}${++id}` @@ -435,7 +437,7 @@ describe('mouse events on slices', () => { }) }) -describe('touch events with useMesh', () => { +describe('events with useMesh', () => { const data = [ { id: 'A', @@ -457,6 +459,17 @@ describe('touch events with useMesh', () => { enableTouchCrosshair: true, } + it('should not throw onMouseEnter on empty data', () => { + const onMouseEnter = jest.fn() + const wrapper = mount() + expect(() => + wrapper.find(`[data-ref='mesh-interceptor']`).simulate('mouseenter', { + clientX: 50, + clientY: 50, + }) + ).not.toThrow() + }) + it('should call onTouchStart', () => { const onTouchStart = jest.fn() const wrapper = mount() @@ -464,6 +477,7 @@ describe('touch events with useMesh', () => { touches: [{ clientX: 50, clientY: 50 }], }) expect(onTouchStart).toHaveBeenCalledTimes(1) + wrapper.unmount() }) it('should call onTouchMove', () => { @@ -473,6 +487,7 @@ describe('touch events with useMesh', () => { touches: [{ clientX: 50, clientY: 50 }], }) expect(onTouchMove).toHaveBeenCalledTimes(1) + wrapper.unmount() }) it('should call onTouchEnd', () => { @@ -485,6 +500,7 @@ describe('touch events with useMesh', () => { }) .simulate('touchend') expect(onTouchEnd).toHaveBeenCalledTimes(1) + wrapper.unmount() }) }) diff --git a/packages/voronoi/src/hooks.ts b/packages/voronoi/src/hooks.ts index b6ef38cc6..4fca811e7 100644 --- a/packages/voronoi/src/hooks.ts +++ b/packages/voronoi/src/hooks.ts @@ -171,7 +171,7 @@ export const useMeshEvents = ({ const findNode = useCallback( (event: MouseEvent | TouchEvent): null | [number, Node] => { - if (!elementRef.current) return null + if (!elementRef.current || nodes.length === 0) return null const [x, y] = getRelativeCursor(elementRef.current, event) From c1d8d5ce5cb942a893da4116c2a75726c287c8f1 Mon Sep 17 00:00:00 2001 From: Aria Kassaei Date: Thu, 14 Nov 2024 18:33:32 +0100 Subject: [PATCH 2/2] test refacto --- packages/line/tests/Line.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/line/tests/Line.test.js b/packages/line/tests/Line.test.js index 1879c7031..893faed70 100644 --- a/packages/line/tests/Line.test.js +++ b/packages/line/tests/Line.test.js @@ -6,13 +6,11 @@ import { LINE_UNIQUE_ID_PREFIX } from '../src/hooks' import SlicesItem from '../src/SlicesItem' import renderer from 'react-test-renderer' import { DotsItem } from '@nivo/core' -import { shallow } from 'enzyme' // Handle useId mocks let id = 0 beforeEach(() => { id = 0 - jest.resetAllMocks() }) const generateId = () => `${LINE_UNIQUE_ID_PREFIX}${++id}` @@ -468,6 +466,7 @@ describe('events with useMesh', () => { clientY: 50, }) ).not.toThrow() + wrapper.unmount() }) it('should call onTouchStart', () => {