@@ -6,21 +6,24 @@ import { highlightDataPlugin } from '@/components/DBHeatmapChart/highlightDataPl
66// Identity x positions; y pixels grow downward (pixel = 100 - 10 * value).
77const valToPos = ( v : number , axis : 'x' | 'y' ) =>
88 axis === 'x' ? v : 100 - 10 * v ;
9+ const posToVal = ( p : number , axis : 'x' | 'y' ) =>
10+ axis === 'x' ? p : ( 100 - p ) / 10 ;
911
10- function hover ( cursor : { left : number ; top : number } ) {
12+ function hover ( cursor : { left : number ; top : number } , cells = [ 2 , 6 ] ) {
1113 const onPointHighlight = jest . fn ( ) ;
12- const plugin = highlightDataPlugin ( { proximity : 20 , onPointHighlight } ) ;
14+ const plugin = highlightDataPlugin ( { margin : 20 , onPointHighlight } ) ;
1315 const u = {
1416 cursor,
1517 over : { offsetLeft : 5 , offsetTop : 7 } ,
1618 valToPos,
19+ posToVal,
1720 data : [
1821 [ ] ,
1922 gridToPlotData ( {
2023 times : [ 100 ] ,
2124 stepMs : 10 ,
2225 yAxis : { type : 'numeric' , scale : 'linear' , edges : [ 0 , 1 , 3 ] } ,
23- cells : [ 2 , 6 ] ,
26+ cells,
2427 cellKind : 'count' ,
2528 } ) ,
2629 ] ,
@@ -31,26 +34,49 @@ function hover(cursor: { left: number; top: number }) {
3134 return onPointHighlight ;
3235}
3336
37+ const reported = ( onPointHighlight : jest . Mock ) =>
38+ onPointHighlight . mock . calls . at ( - 1 ) ?. [ 0 ] ;
39+
3440describe ( 'highlightDataPlugin' , ( ) => {
35- it ( 'reports the nearest non-empty cell with its own size' , ( ) => {
36- // Row 1 spans y 1..3, centered at y=2 -> pixel 80
37- const onPointHighlight = hover ( { left : 100 , top : 80 } ) ;
38-
39- expect ( onPointHighlight ) . toHaveBeenCalledWith (
40- expect . objectContaining ( {
41- xVal : 100 ,
42- yVal : 2 ,
43- countVal : 6 ,
44- closestIndex : 1 ,
45- xCoord : 105 ,
46- yCoord : 87 ,
47- xSize : 10 ,
48- ySize : 20 ,
49- } ) ,
50- ) ;
41+ it ( 'reports the cell under the cursor with its own size' , ( ) => {
42+ expect ( reported ( hover ( { left : 100 , top : 80 } ) ) ) . toEqual ( {
43+ xVal : 100 ,
44+ yVal : 2 ,
45+ countVal : 6 ,
46+ closestDistance : 0 ,
47+ closestIndex : 1 ,
48+ xCoord : 105 ,
49+ yCoord : 87 ,
50+ xSize : 10 ,
51+ ySize : 20 ,
52+ } ) ;
53+ } ) ;
54+
55+ it ( 'prefers the cell under the cursor over a nearby one' , ( ) => {
56+ // Just inside row 0, a pixel from row 1's edge
57+ expect ( reported ( hover ( { left : 100 , top : 91 } ) ) ) . toMatchObject ( {
58+ closestIndex : 0 ,
59+ closestDistance : 0 ,
60+ } ) ;
61+ } ) ;
62+
63+ it ( 'reports a cell within the margin of its edge' , ( ) => {
64+ expect ( reported ( hover ( { left : 110 , top : 80 } ) ) ) . toMatchObject ( {
65+ closestIndex : 1 ,
66+ closestDistance : 5 ,
67+ } ) ;
68+ } ) ;
69+
70+ it ( 'reaches past an empty cell to a neighbor within the margin' , ( ) => {
71+ // Inside the empty row 0, 5px below row 1
72+ expect ( reported ( hover ( { left : 100 , top : 95 } , [ 0 , 6 ] ) ) ) . toMatchObject ( {
73+ closestIndex : 1 ,
74+ closestDistance : 5 ,
75+ } ) ;
5176 } ) ;
5277
53- it ( 'ignores the cursor when no cell is within the proximity' , ( ) => {
54- expect ( hover ( { left : 500 , top : 80 } ) ) . not . toHaveBeenCalled ( ) ;
78+ it ( 'reports nothing when no cell is within the margin' , ( ) => {
79+ const onPointHighlight = hover ( { left : 130 , top : 80 } ) ;
80+ expect ( onPointHighlight ) . toHaveBeenCalledWith ( undefined ) ;
5581 } ) ;
5682} ) ;
0 commit comments