|
| 1 | +import React from 'react'; |
| 2 | +import { act, fireEvent, render } from '@testing-library/react-native'; |
| 3 | +import ChartTouchOverlay, { |
| 4 | + CHART_TOUCH_LONG_PRESS_DELAY_MS, |
| 5 | + buildChartTouchZones, |
| 6 | + type ChartTouchLayout, |
| 7 | +} from '../../src/components/ChartTouchOverlay'; |
| 8 | + |
| 9 | +const layout: ChartTouchLayout = { |
| 10 | + chartBounds: { |
| 11 | + left: 0, |
| 12 | + right: 60, |
| 13 | + top: 5, |
| 14 | + bottom: 45, |
| 15 | + }, |
| 16 | + points: [ |
| 17 | + { x: 10, xValue: '2026-03-01', y: 30, yValue: 1000 }, |
| 18 | + { x: 30, xValue: '2026-03-02', y: 20, yValue: 2000 }, |
| 19 | + { x: 50, xValue: '2026-03-03', y: 10, yValue: 3000 }, |
| 20 | + ], |
| 21 | +}; |
| 22 | + |
| 23 | +const createTouchEvent = (locationX: number, locationY: number) => ({ |
| 24 | + nativeEvent: { |
| 25 | + changedTouches: [{ locationX, locationY }], |
| 26 | + touches: [{ locationX, locationY }], |
| 27 | + locationX, |
| 28 | + locationY, |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +describe('ChartTouchOverlay', () => { |
| 33 | + beforeEach(() => { |
| 34 | + jest.useFakeTimers(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.runOnlyPendingTimers(); |
| 39 | + jest.useRealTimers(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('builds touch zones from the midpoints between plotted points', () => { |
| 43 | + expect(buildChartTouchZones(layout.points, layout.chartBounds)).toEqual([ |
| 44 | + { index: 0, left: 0, width: 20 }, |
| 45 | + { index: 1, left: 20, width: 20 }, |
| 46 | + { index: 2, left: 40, width: 20 }, |
| 47 | + ]); |
| 48 | + }); |
| 49 | + |
| 50 | + it('waits for the long press delay before selecting a zone', () => { |
| 51 | + const onSelect = jest.fn(); |
| 52 | + const onClear = jest.fn(); |
| 53 | + const screen = render( |
| 54 | + <ChartTouchOverlay |
| 55 | + layout={layout} |
| 56 | + onSelect={onSelect} |
| 57 | + onClear={onClear} |
| 58 | + testIDPrefix="touch-overlay" |
| 59 | + />, |
| 60 | + ); |
| 61 | + |
| 62 | + fireEvent( |
| 63 | + screen.getByTestId('touch-overlay'), |
| 64 | + 'touchStart', |
| 65 | + createTouchEvent(25, 20), |
| 66 | + ); |
| 67 | + |
| 68 | + expect(onSelect).not.toHaveBeenCalled(); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + jest.advanceTimersByTime(CHART_TOUCH_LONG_PRESS_DELAY_MS); |
| 72 | + }); |
| 73 | + |
| 74 | + expect(onSelect).toHaveBeenCalledWith(1); |
| 75 | + |
| 76 | + fireEvent( |
| 77 | + screen.getByTestId('touch-overlay'), |
| 78 | + 'touchEnd', |
| 79 | + createTouchEvent(25, 20), |
| 80 | + ); |
| 81 | + |
| 82 | + expect(onClear).toHaveBeenCalledTimes(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it('cancels activation when the touch turns into a drag before the delay', () => { |
| 86 | + const onSelect = jest.fn(); |
| 87 | + const screen = render( |
| 88 | + <ChartTouchOverlay |
| 89 | + layout={layout} |
| 90 | + onSelect={onSelect} |
| 91 | + testIDPrefix="touch-overlay" |
| 92 | + />, |
| 93 | + ); |
| 94 | + |
| 95 | + fireEvent( |
| 96 | + screen.getByTestId('touch-overlay'), |
| 97 | + 'touchStart', |
| 98 | + createTouchEvent(25, 20), |
| 99 | + ); |
| 100 | + fireEvent( |
| 101 | + screen.getByTestId('touch-overlay'), |
| 102 | + 'touchMove', |
| 103 | + createTouchEvent(25, 35), |
| 104 | + ); |
| 105 | + |
| 106 | + act(() => { |
| 107 | + jest.advanceTimersByTime(CHART_TOUCH_LONG_PRESS_DELAY_MS); |
| 108 | + }); |
| 109 | + |
| 110 | + expect(onSelect).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('updates the selected zone while dragging after activation', () => { |
| 114 | + const onSelect = jest.fn(); |
| 115 | + const onClear = jest.fn(); |
| 116 | + const screen = render( |
| 117 | + <ChartTouchOverlay |
| 118 | + layout={layout} |
| 119 | + onSelect={onSelect} |
| 120 | + onClear={onClear} |
| 121 | + testIDPrefix="touch-overlay" |
| 122 | + />, |
| 123 | + ); |
| 124 | + |
| 125 | + fireEvent( |
| 126 | + screen.getByTestId('touch-overlay'), |
| 127 | + 'touchStart', |
| 128 | + createTouchEvent(10, 20), |
| 129 | + ); |
| 130 | + act(() => { |
| 131 | + jest.advanceTimersByTime(CHART_TOUCH_LONG_PRESS_DELAY_MS); |
| 132 | + }); |
| 133 | + |
| 134 | + fireEvent( |
| 135 | + screen.getByTestId('touch-overlay'), |
| 136 | + 'touchMove', |
| 137 | + createTouchEvent(45, 20), |
| 138 | + ); |
| 139 | + |
| 140 | + expect(onSelect).toHaveBeenNthCalledWith(1, 0); |
| 141 | + expect(onSelect).toHaveBeenNthCalledWith(2, 2); |
| 142 | + }); |
| 143 | +}); |
0 commit comments