@@ -9,13 +9,11 @@ import {
99 symbolOpposite ,
1010 getGradientConfig ,
1111 generateThresholds ,
12+ generateValueThresholds ,
1213} from './bar_gauge_utils' ;
1314import { AxisColumnMappings , Threshold , VisFieldType } from '../types' ;
1415import { BarGaugeChartStyle } from './bar_gauge_vis_config' ;
15-
16- jest . mock ( '../theme/default_colors' , ( ) => ( {
17- getColors : jest . fn ( ( ) => ( { statusGreen : '#00FF00' } ) ) ,
18- } ) ) ;
16+ import { getColors } from '../theme/default_colors' ;
1917
2018describe ( 'bar_gauge_utils' , ( ) => {
2119 describe ( 'getBarOrientation' , ( ) => {
@@ -142,56 +140,55 @@ describe('bar_gauge_utils', () => {
142140
143141 describe ( 'Basic functionality' , ( ) => {
144142 it ( 'should handle empty thresholds array' , ( ) => {
145- const result = generateThresholds ( 0 , 100 , [ ] , '#BLUE' , [ ] ) ;
143+ const result = generateThresholds ( 0 , 100 , [ ] , '#BLUE' ) ;
146144
147- expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
148- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
149- expect ( result . valueThresholds ) . toEqual ( [ ] ) ;
145+ expect ( result ) . toHaveLength ( 1 ) ;
146+ expect ( result [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
150147 } ) ;
151148
152149 it ( 'should use default color when baseColor is undefined' , ( ) => {
153- const result = generateThresholds ( 0 , 100 , [ ] , undefined , [ ] ) ;
150+ const result = generateThresholds ( 0 , 100 , [ ] , undefined ) ;
154151
155- expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
156- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#00FF00 ' } ) ;
152+ expect ( result ) . toHaveLength ( 1 ) ;
153+ expect ( result [ 0 ] ) . toEqual ( { value : 0 , color : '#00BD6B ' } ) ;
157154 } ) ;
158155
159156 it ( 'should process normal thresholds correctly' , ( ) => {
160- const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
157+ const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' ) ;
161158
162- expect ( result . mergedThresholds ) . toHaveLength ( 4 ) ;
163- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
164- expect ( result . mergedThresholds [ 1 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
165- expect ( result . mergedThresholds [ 2 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
166- expect ( result . mergedThresholds [ 3 ] ) . toEqual ( { value : 80 , color : '#0037ffff' } ) ;
159+ expect ( result ) . toHaveLength ( 4 ) ;
160+ expect ( result [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
161+ expect ( result [ 1 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
162+ expect ( result [ 2 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
163+ expect ( result [ 3 ] ) . toEqual ( { value : 80 , color : '#0037ffff' } ) ;
167164 } ) ;
168165 } ) ;
169166
170167 describe ( 'Threshold filtering and range handling' , ( ) => {
171168 it ( 'should filter thresholds above maxBase' , ( ) => {
172- const result = generateThresholds ( 0 , 60 , mockThresholds , '#BLUE' , [ ] ) ;
169+ const result = generateThresholds ( 0 , 60 , mockThresholds , '#BLUE' ) ;
173170
174- expect ( result . mergedThresholds ) . toHaveLength ( 3 ) ;
175- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
176- expect ( result . mergedThresholds [ 1 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
177- expect ( result . mergedThresholds [ 2 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
171+ expect ( result ) . toHaveLength ( 3 ) ;
172+ expect ( result [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
173+ expect ( result [ 1 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
174+ expect ( result [ 2 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
178175 // Should not include the threshold with value 80
179176 } ) ;
180177
181178 it ( 'should handle minBase higher than first threshold' , ( ) => {
182- const result = generateThresholds ( 25 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
179+ const result = generateThresholds ( 25 , 100 , mockThresholds , '#BLUE' ) ;
183180
184- expect ( result . mergedThresholds ) . toHaveLength ( 3 ) ;
185- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 25 , color : '#FF0000' } ) ;
186- expect ( result . mergedThresholds [ 1 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
187- expect ( result . mergedThresholds [ 2 ] ) . toEqual ( { value : 80 , color : '#0037ffff' } ) ;
181+ expect ( result ) . toHaveLength ( 3 ) ;
182+ expect ( result [ 0 ] ) . toEqual ( { value : 25 , color : '#FF0000' } ) ;
183+ expect ( result [ 1 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
184+ expect ( result [ 2 ] ) . toEqual ( { value : 80 , color : '#0037ffff' } ) ;
188185 } ) ;
189186
190187 it ( 'should handle minBase higher than all thresholds' , ( ) => {
191- const result = generateThresholds ( 90 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
188+ const result = generateThresholds ( 90 , 100 , mockThresholds , '#BLUE' ) ;
192189
193- expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
194- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 90 , color : '#0037ffff' } ) ;
190+ expect ( result ) . toHaveLength ( 1 ) ;
191+ expect ( result [ 0 ] ) . toEqual ( { value : 90 , color : '#0037ffff' } ) ;
195192 } ) ;
196193 } ) ;
197194
@@ -204,109 +201,82 @@ describe('bar_gauge_utils', () => {
204201 { value : 80 , color : '#00FF00' } ,
205202 ] ;
206203
207- const result = generateThresholds ( 0 , 100 , duplicateThresholds , '#BLUE' , [ ] ) ;
204+ const result = generateThresholds ( 0 , 100 , duplicateThresholds , '#BLUE' ) ;
208205
209- expect ( result . mergedThresholds ) . toHaveLength ( 4 ) ;
210- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
211- expect ( result . mergedThresholds [ 1 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
212- expect ( result . mergedThresholds [ 2 ] ) . toEqual ( { value : 50 , color : '#00FFFF' } ) ; // Latest color
213- expect ( result . mergedThresholds [ 3 ] ) . toEqual ( { value : 80 , color : '#00FF00' } ) ;
206+ expect ( result ) . toHaveLength ( 4 ) ;
207+ expect ( result [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
208+ expect ( result [ 1 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
209+ expect ( result [ 2 ] ) . toEqual ( { value : 50 , color : '#00FFFF' } ) ; // Latest color
210+ expect ( result [ 3 ] ) . toEqual ( { value : 80 , color : '#00FF00' } ) ;
214211 } ) ;
215212 } ) ;
213+ } ) ;
214+
215+ describe ( 'generateValueThresholds' , ( ) => {
216+ const mockThresholds : Threshold [ ] = [
217+ { value : 10 , color : '#FF0000' } ,
218+ { value : 50 , color : '#FFFF00' } ,
219+ { value : 80 , color : '#0037ffff' } ,
220+ ] ;
221+
222+ afterEach ( ( ) => {
223+ jest . restoreAllMocks ( ) ;
224+ } ) ;
216225
217226 describe ( 'Value stops processing' , ( ) => {
218227 it ( 'should process value stops correctly' , ( ) => {
219228 const valueStops = [ 15 , 45 , 75 ] ;
220- const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , valueStops ) ;
229+ const result = generateValueThresholds ( 0 , 100 , valueStops , mockThresholds ) ;
221230
222- expect ( result . valueThresholds ) . toHaveLength ( 3 ) ;
223- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ; // Uses threshold at 10
224- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ; // Uses threshold at 10
225- expect ( result . valueThresholds [ 2 ] ) . toEqual ( { value : 75 , color : '#FFFF00' } ) ; // Uses threshold at 50
231+ expect ( result ) . toHaveLength ( 3 ) ;
232+ expect ( result [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ; // Uses threshold at 10
233+ expect ( result [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ; // Uses threshold at 10
234+ expect ( result [ 2 ] ) . toEqual ( { value : 75 , color : '#FFFF00' } ) ; // Uses threshold at 50
226235 } ) ;
227236
228237 it ( 'should filter value stops outside range' , ( ) => {
229238 const valueStops = [ 5 , 15 , 45 , 95 , 105 ] ; // 105 is above maxBase, 5 is below minBase (when minBase > 0)
230- const result = generateThresholds ( 10 , 90 , mockThresholds , '#BLUE' , valueStops ) ;
239+ const result = generateValueThresholds ( 10 , 90 , valueStops , mockThresholds ) ;
231240
232241 // Should only include stops between minBase (10) and maxBase (90)
233- expect ( result . valueThresholds ) . toHaveLength ( 2 ) ;
234- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ;
235- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ;
242+ expect ( result ) . toHaveLength ( 2 ) ;
243+ expect ( result [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ;
244+ expect ( result [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ;
236245 } ) ;
237246
238247 it ( 'should handle duplicate value stops' , ( ) => {
239248 const valueStops = [ 15 , 15 , 45 , 45 , 75 ] ; // Duplicates
240- const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , valueStops ) ;
241-
242- expect ( result . valueThresholds ) . toHaveLength ( 3 ) ; // Should deduplicate
243- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ;
244- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ;
245- expect ( result . valueThresholds [ 2 ] ) . toEqual ( { value : 75 , color : '#FFFF00' } ) ;
249+ const result = generateValueThresholds ( 0 , 100 , valueStops , mockThresholds ) ;
250+ expect ( result ) . toHaveLength ( 3 ) ; // Should deduplicate
251+ expect ( result [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ;
252+ expect ( result [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ;
253+ expect ( result [ 2 ] ) . toEqual ( { value : 75 , color : '#FFFF00' } ) ;
246254 } ) ;
247255
248256 it ( 'should handle unsorted value stops' , ( ) => {
249257 const valueStops = [ 75 , 15 , 45 ] ; // Unsorted
250- const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , valueStops ) ;
258+ const result = generateValueThresholds ( 0 , 100 , valueStops , mockThresholds ) ;
251259
252- expect ( result . valueThresholds ) . toHaveLength ( 3 ) ;
253- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ;
254- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ;
255- expect ( result . valueThresholds [ 2 ] ) . toEqual ( { value : 75 , color : '#FFFF00' } ) ;
260+ expect ( result ) . toHaveLength ( 3 ) ;
261+ expect ( result [ 0 ] ) . toEqual ( { value : 15 , color : '#FF0000' } ) ;
262+ expect ( result [ 1 ] ) . toEqual ( { value : 45 , color : '#FF0000' } ) ;
263+ expect ( result [ 2 ] ) . toEqual ( { value : 75 , color : '#FFFF00' } ) ;
256264 } ) ;
257265
258266 it ( 'should handle empty value stops array' , ( ) => {
259- const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
267+ const result = generateValueThresholds ( 0 , 100 , [ ] , mockThresholds ) ;
260268
261- expect ( result . valueThresholds ) . toEqual ( [ ] ) ;
262- } ) ;
263- } ) ;
264-
265- describe ( 'Edge cases' , ( ) => {
266- it ( 'should handle single threshold' , ( ) => {
267- const singleThreshold : Threshold [ ] = [ { value : 50 , color : '#FF0000' } ] ;
268- const result = generateThresholds ( 0 , 100 , singleThreshold , '#BLUE' , [ 25 , 75 ] ) ;
269-
270- expect ( result . mergedThresholds ) . toHaveLength ( 2 ) ;
271- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
272- expect ( result . mergedThresholds [ 1 ] ) . toEqual ( { value : 50 , color : '#FF0000' } ) ;
273-
274- expect ( result . valueThresholds ) . toHaveLength ( 2 ) ;
275- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : 25 , color : '#BLUE' } ) ;
276- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 75 , color : '#FF0000' } ) ;
277- } ) ;
278-
279- it ( 'should handle minBase equal to maxBase' , ( ) => {
280- const result = generateThresholds ( 50 , 50 , mockThresholds , '#BLUE' , [ ] ) ;
281-
282- expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
283- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
269+ expect ( result ) . toEqual ( [ ] ) ;
284270 } ) ;
285271
286272 it ( 'should handle value stops equal to threshold values' , ( ) => {
287273 const valueStops = [ 10 , 50 , 80 ] ; // Exact threshold values
288- const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , valueStops ) ;
289-
290- expect ( result . valueThresholds ) . toHaveLength ( 3 ) ;
291- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
292- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
293- expect ( result . valueThresholds [ 2 ] ) . toEqual ( { value : 80 , color : '#0037ffff' } ) ;
294- } ) ;
295-
296- it ( 'should handle negative values' , ( ) => {
297- const negativeThresholds : Threshold [ ] = [
298- { value : - 50 , color : '#FF0000' } ,
299- { value : 0 , color : '#FFFF00' } ,
300- { value : 50 , color : '#00FF00' } ,
301- ] ;
302-
303- const result = generateThresholds ( - 100 , 100 , negativeThresholds , '#BLUE' , [ - 25 , 25 ] ) ;
274+ const result = generateValueThresholds ( 0 , 100 , valueStops , mockThresholds ) ;
304275
305- expect ( result . mergedThresholds ) . toHaveLength ( 4 ) ;
306- expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : - 100 , color : '#BLUE' } ) ;
307- expect ( result . valueThresholds ) . toHaveLength ( 2 ) ;
308- expect ( result . valueThresholds [ 0 ] ) . toEqual ( { value : - 25 , color : '#FF0000' } ) ;
309- expect ( result . valueThresholds [ 1 ] ) . toEqual ( { value : 25 , color : '#FFFF00' } ) ;
276+ expect ( result ) . toHaveLength ( 3 ) ;
277+ expect ( result [ 0 ] ) . toEqual ( { value : 10 , color : '#FF0000' } ) ;
278+ expect ( result [ 1 ] ) . toEqual ( { value : 50 , color : '#FFFF00' } ) ;
279+ expect ( result [ 2 ] ) . toEqual ( { value : 80 , color : '#0037ffff' } ) ;
310280 } ) ;
311281 } ) ;
312282 } ) ;
0 commit comments