@@ -8,10 +8,15 @@ import {
88 thresholdsToGradient ,
99 symbolOpposite ,
1010 getGradientConfig ,
11+ generateThresholds ,
1112} from './bar_gauge_utils' ;
1213import { AxisColumnMappings , Threshold , VisFieldType } from '../types' ;
1314import { BarGaugeChartStyle } from './bar_gauge_vis_config' ;
1415
16+ jest . mock ( '../theme/default_colors' , ( ) => ( {
17+ getColors : jest . fn ( ( ) => ( { statusGreen : '#00FF00' } ) ) ,
18+ } ) ) ;
19+
1520describe ( 'bar_gauge_utils' , ( ) => {
1621 describe ( 'getBarOrientation' , ( ) => {
1722 const mockAxisColumnMappings : AxisColumnMappings = {
@@ -123,4 +128,186 @@ describe('bar_gauge_utils', () => {
123128 expect ( result ) . toBeUndefined ( ) ;
124129 } ) ;
125130 } ) ;
131+
132+ describe ( 'generateThresholds' , ( ) => {
133+ const mockThresholds : Threshold [ ] = [
134+ { value : 10 , color : '#FF0000' } ,
135+ { value : 50 , color : '#FFFF00' } ,
136+ { value : 80 , color : '#0037ffff' } ,
137+ ] ;
138+
139+ afterEach ( ( ) => {
140+ jest . restoreAllMocks ( ) ;
141+ } ) ;
142+
143+ describe ( 'Basic functionality' , ( ) => {
144+ it ( 'should handle empty thresholds array' , ( ) => {
145+ const result = generateThresholds ( 0 , 100 , [ ] , '#BLUE' , [ ] ) ;
146+
147+ expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
148+ expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#BLUE' } ) ;
149+ expect ( result . valueThresholds ) . toEqual ( [ ] ) ;
150+ } ) ;
151+
152+ it ( 'should use default color when baseColor is undefined' , ( ) => {
153+ const result = generateThresholds ( 0 , 100 , [ ] , undefined , [ ] ) ;
154+
155+ expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
156+ expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 0 , color : '#00FF00' } ) ;
157+ } ) ;
158+
159+ it ( 'should process normal thresholds correctly' , ( ) => {
160+ const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
161+
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' } ) ;
167+ } ) ;
168+ } ) ;
169+
170+ describe ( 'Threshold filtering and range handling' , ( ) => {
171+ it ( 'should filter thresholds above maxBase' , ( ) => {
172+ const result = generateThresholds ( 0 , 60 , mockThresholds , '#BLUE' , [ ] ) ;
173+
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' } ) ;
178+ // Should not include the threshold with value 80
179+ } ) ;
180+
181+ it ( 'should handle minBase higher than first threshold' , ( ) => {
182+ const result = generateThresholds ( 25 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
183+
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' } ) ;
188+ } ) ;
189+
190+ it ( 'should handle minBase higher than all thresholds' , ( ) => {
191+ const result = generateThresholds ( 90 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
192+
193+ expect ( result . mergedThresholds ) . toHaveLength ( 1 ) ;
194+ expect ( result . mergedThresholds [ 0 ] ) . toEqual ( { value : 90 , color : '#0037ffff' } ) ;
195+ } ) ;
196+ } ) ;
197+
198+ describe ( 'Duplicate threshold handling' , ( ) => {
199+ it ( 'should handle duplicate threshold values by keeping the latest' , ( ) => {
200+ const duplicateThresholds : Threshold [ ] = [
201+ { value : 10 , color : '#FF0000' } ,
202+ { value : 50 , color : '#FFFF00' } ,
203+ { value : 50 , color : '#00FFFF' } , // Duplicate value, different color
204+ { value : 80 , color : '#00FF00' } ,
205+ ] ;
206+
207+ const result = generateThresholds ( 0 , 100 , duplicateThresholds , '#BLUE' , [ ] ) ;
208+
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' } ) ;
214+ } ) ;
215+ } ) ;
216+
217+ describe ( 'Value stops processing' , ( ) => {
218+ it ( 'should process value stops correctly' , ( ) => {
219+ const valueStops = [ 15 , 45 , 75 ] ;
220+ const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , valueStops ) ;
221+
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
226+ } ) ;
227+
228+ it ( 'should filter value stops outside range' , ( ) => {
229+ 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 ) ;
231+
232+ // 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' } ) ;
236+ } ) ;
237+
238+ it ( 'should handle duplicate value stops' , ( ) => {
239+ 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' } ) ;
246+ } ) ;
247+
248+ it ( 'should handle unsorted value stops' , ( ) => {
249+ const valueStops = [ 75 , 15 , 45 ] ; // Unsorted
250+ const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , valueStops ) ;
251+
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' } ) ;
256+ } ) ;
257+
258+ it ( 'should handle empty value stops array' , ( ) => {
259+ const result = generateThresholds ( 0 , 100 , mockThresholds , '#BLUE' , [ ] ) ;
260+
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' } ) ;
284+ } ) ;
285+
286+ it ( 'should handle value stops equal to threshold values' , ( ) => {
287+ 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 ] ) ;
304+
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' } ) ;
310+ } ) ;
311+ } ) ;
312+ } ) ;
126313} ) ;
0 commit comments