@@ -3,33 +3,98 @@ import drawChart, { addHoverEffects, hoverEffectsId } from './index';
33
44describe ( 'Sparkline Chart' , ( ) => {
55 const testId = 'test-chart-id' ;
6+ let container ;
67
7- const container = document . createElement ( 'div' ) ;
8- container . setAttribute ( 'id' , testId ) ;
9-
10- document . body . appendChild ( container ) ;
8+ beforeAll ( ( ) => {
9+ Element . prototype . getBoundingClientRect = jest . fn ( ( ) => {
10+ return {
11+ width : 500 ,
12+ height : 100 ,
13+ top : 0 ,
14+ left : 0 ,
15+ bottom : 0 ,
16+ right : 0 ,
17+ } ;
18+ } ) ;
19+ } ) ;
1120
12- drawChart ( mockData . data , testId , dateField , fields ) ;
21+ beforeEach ( ( ) => {
22+ document . body . innerHTML = '' ;
23+ container = document . createElement ( 'div' ) ;
24+ container . setAttribute ( 'id' , testId ) ;
25+ document . body . appendChild ( container ) ;
26+ } ) ;
1327
1428 it ( 'displays the expected number of lines on the chart' , ( ) => {
29+ drawChart ( mockData . data , testId , dateField , fields [ 0 ] ) ;
1530 expect ( container . querySelectorAll ( '[data-test-id="chart-sparkline"]' ) . length ) . toBe ( 1 ) ;
1631 } ) ;
1732
1833 it ( 'places one dot on the chart' , ( ) => {
34+ drawChart ( mockData . data , testId , dateField , fields [ 0 ] ) ;
1935 expect ( container . querySelectorAll ( '[data-test-id="marker"]' ) . length ) . toBe ( 1 ) ;
2036 } ) ;
2137
2238 it ( 'adds hover effects on mouseover and removes them on mouseout' , ( ) => {
39+ drawChart ( mockData . data , testId , dateField , fields [ 0 ] ) ;
40+
2341 const hoverFunction = jest . fn ( ) ;
2442 addHoverEffects ( mockData . data , testId , dateField , fields [ 0 ] , hoverFunction ) ;
2543
2644 const hoverEffectsRect = document . getElementById ( hoverEffectsId ) ;
2745
28- hoverEffectsRect . dispatchEvent ( new MouseEvent ( 'mousemove' , { bubbles : true } ) ) ;
46+ const event = new MouseEvent ( 'mousemove' , {
47+ bubbles : true ,
48+ clientX : 250 ,
49+ clientY : 50 ,
50+ } ) ;
51+
52+ hoverEffectsRect . dispatchEvent ( event ) ;
2953 expect ( hoverFunction ) . toHaveBeenCalled ( ) ;
3054
3155 hoverFunction . mockClear ( ) ;
3256 hoverEffectsRect . dispatchEvent ( new MouseEvent ( 'mouseout' , { bubbles : true } ) ) ;
3357 expect ( hoverFunction ) . toHaveBeenCalledWith ( null , null ) ;
3458 } ) ;
59+
60+ it ( 'renders X-Axis labels and start/end text when showXLabel is true' , ( ) => {
61+ const startLabel = 'Start Label' ;
62+ const endLabel = 'End Label' ;
63+
64+ drawChart ( mockData . data , testId , dateField , fields [ 0 ] , true , startLabel , endLabel ) ;
65+
66+ const axis = container . querySelector ( '.x-label' ) ;
67+ expect ( axis ) . toBeTruthy ( ) ;
68+
69+ const texts = container . querySelectorAll ( '.x-label-text' ) ;
70+ expect ( texts . length ) . toBe ( 2 ) ;
71+ expect ( texts [ 0 ] . textContent ) . toBe ( startLabel ) ;
72+ expect ( texts [ 1 ] . textContent ) . toBe ( endLabel ) ;
73+ } ) ;
74+
75+ it ( 'applies custom formatting options to X labels' , ( ) => {
76+ const customFormatter = { fill : 'blue' , 'font-weight' : 'bold' } ;
77+
78+ drawChart ( mockData . data , testId , dateField , fields [ 0 ] , true , 'Start' , 'End' , customFormatter ) ;
79+
80+ const label = container . querySelector ( '.x-label-text' ) ;
81+ expect ( label . getAttribute ( 'fill' ) ) . toBe ( 'blue' ) ;
82+ expect ( label . getAttribute ( 'font-weight' ) ) . toBe ( 'bold' ) ;
83+ } ) ;
84+
85+ it ( 'renders a blue marker for positive values and red for negative values' , ( ) => {
86+ const mixedData = [
87+ { [ dateField ] : '2020-01-01' , val : 10 } ,
88+ { [ dateField ] : '2020-01-02' , val : - 10 } ,
89+ ] ;
90+
91+ drawChart ( mixedData . slice ( 0 , 1 ) , testId , dateField , 'val' ) ;
92+ let marker = container . querySelector ( '[data-test-id="marker"]' ) ;
93+ expect ( marker . getAttribute ( 'fill' ) ) . toBe ( '#4971b7' ) ; // Blue
94+
95+ container . innerHTML = '' ;
96+ drawChart ( mixedData , testId , dateField , 'val' ) ;
97+ marker = container . querySelector ( '[data-test-id="marker"]' ) ;
98+ expect ( marker . getAttribute ( 'fill' ) ) . toBe ( '#f2655b' ) ; // Red
99+ } ) ;
35100} ) ;
0 commit comments