@@ -237,6 +237,226 @@ test('Should pluck the categorical style-meta from fieldmeta', async () => {
237237 } ) ;
238238} ) ;
239239
240+ describe ( 'get mapbox color expression' , ( ) => {
241+ describe ( 'ordinal color ramp' , ( ) => {
242+ test ( 'should return null when field is not provided' , async ( ) => {
243+ const dynamicStyleOptions = {
244+ type : COLOR_MAP_TYPE . ORDINAL ,
245+ } ;
246+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
247+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
248+ } ) ;
249+
250+ test ( 'should return null when field name is not provided' , async ( ) => {
251+ const dynamicStyleOptions = {
252+ type : COLOR_MAP_TYPE . ORDINAL ,
253+ field : { } ,
254+ } ;
255+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
256+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
257+ } ) ;
258+
259+ describe ( 'pre-defined color ramp' , ( ) => {
260+ test ( 'should return null when color ramp is not provided' , async ( ) => {
261+ const dynamicStyleOptions = {
262+ type : COLOR_MAP_TYPE . ORDINAL ,
263+ field : {
264+ name : 'myField' ,
265+ } ,
266+ } ;
267+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
268+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
269+ } ) ;
270+
271+ test ( 'should return mapbox expression for color ramp' , async ( ) => {
272+ const dynamicStyleOptions = {
273+ type : COLOR_MAP_TYPE . ORDINAL ,
274+ field : {
275+ name : 'myField' ,
276+ } ,
277+ color : 'Blues' ,
278+ } ;
279+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
280+ expect ( colorProperty . _getMbColor ( ) ) . toEqual ( [
281+ 'interpolate' ,
282+ [ 'linear' ] ,
283+ [ 'coalesce' , [ 'feature-state' , '__kbn__dynamic__myField__lineColor' ] , - 1 ] ,
284+ - 1 ,
285+ 'rgba(0,0,0,0)' ,
286+ 0 ,
287+ '#f7faff' ,
288+ 0.125 ,
289+ '#ddeaf7' ,
290+ 0.25 ,
291+ '#c5daee' ,
292+ 0.375 ,
293+ '#9dc9e0' ,
294+ 0.5 ,
295+ '#6aadd5' ,
296+ 0.625 ,
297+ '#4191c5' ,
298+ 0.75 ,
299+ '#2070b4' ,
300+ 0.875 ,
301+ '#072f6b' ,
302+ ] ) ;
303+ } ) ;
304+ } ) ;
305+
306+ describe ( 'custom color ramp' , ( ) => {
307+ test ( 'should return null when customColorRamp is not provided' , async ( ) => {
308+ const dynamicStyleOptions = {
309+ type : COLOR_MAP_TYPE . ORDINAL ,
310+ field : {
311+ name : 'myField' ,
312+ } ,
313+ useCustomColorRamp : true ,
314+ } ;
315+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
316+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
317+ } ) ;
318+
319+ test ( 'should return null when customColorRamp is empty' , async ( ) => {
320+ const dynamicStyleOptions = {
321+ type : COLOR_MAP_TYPE . ORDINAL ,
322+ field : {
323+ name : 'myField' ,
324+ } ,
325+ useCustomColorRamp : true ,
326+ customColorRamp : [ ] ,
327+ } ;
328+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
329+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
330+ } ) ;
331+
332+ test ( 'should return mapbox expression for custom color ramp' , async ( ) => {
333+ const dynamicStyleOptions = {
334+ type : COLOR_MAP_TYPE . ORDINAL ,
335+ field : {
336+ name : 'myField' ,
337+ } ,
338+ useCustomColorRamp : true ,
339+ customColorRamp : [
340+ { stop : 10 , color : '#f7faff' } ,
341+ { stop : 100 , color : '#072f6b' } ,
342+ ] ,
343+ } ;
344+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
345+ expect ( colorProperty . _getMbColor ( ) ) . toEqual ( [
346+ 'step' ,
347+ [ 'coalesce' , [ 'feature-state' , '__kbn__dynamic__myField__lineColor' ] , 9 ] ,
348+ 'rgba(0,0,0,0)' ,
349+ 10 ,
350+ '#f7faff' ,
351+ 100 ,
352+ '#072f6b' ,
353+ ] ) ;
354+ } ) ;
355+ } ) ;
356+ } ) ;
357+
358+ describe ( 'categorical color palette' , ( ) => {
359+ test ( 'should return null when field is not provided' , async ( ) => {
360+ const dynamicStyleOptions = {
361+ type : COLOR_MAP_TYPE . CATEGORICAL ,
362+ } ;
363+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
364+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
365+ } ) ;
366+
367+ test ( 'should return null when field name is not provided' , async ( ) => {
368+ const dynamicStyleOptions = {
369+ type : COLOR_MAP_TYPE . CATEGORICAL ,
370+ field : { } ,
371+ } ;
372+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
373+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
374+ } ) ;
375+
376+ describe ( 'pre-defined color palette' , ( ) => {
377+ test ( 'should return null when color palette is not provided' , async ( ) => {
378+ const dynamicStyleOptions = {
379+ type : COLOR_MAP_TYPE . CATEGORICAL ,
380+ field : {
381+ name : 'myField' ,
382+ } ,
383+ } ;
384+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
385+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
386+ } ) ;
387+
388+ test ( 'should return mapbox expression for color palette' , async ( ) => {
389+ const dynamicStyleOptions = {
390+ type : COLOR_MAP_TYPE . CATEGORICAL ,
391+ field : {
392+ name : 'myField' ,
393+ } ,
394+ colorCategory : 'palette_0' ,
395+ } ;
396+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
397+ expect ( colorProperty . _getMbColor ( ) ) . toEqual ( [
398+ 'match' ,
399+ [ 'to-string' , [ 'get' , 'myField' ] ] ,
400+ 'US' ,
401+ '#54B399' ,
402+ 'CN' ,
403+ '#6092C0' ,
404+ '#D36086' ,
405+ ] ) ;
406+ } ) ;
407+ } ) ;
408+
409+ describe ( 'custom color palette' , ( ) => {
410+ test ( 'should return null when customColorPalette is not provided' , async ( ) => {
411+ const dynamicStyleOptions = {
412+ type : COLOR_MAP_TYPE . CATEGORICAL ,
413+ field : {
414+ name : 'myField' ,
415+ } ,
416+ useCustomColorPalette : true ,
417+ } ;
418+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
419+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
420+ } ) ;
421+
422+ test ( 'should return null when customColorPalette is empty' , async ( ) => {
423+ const dynamicStyleOptions = {
424+ type : COLOR_MAP_TYPE . CATEGORICAL ,
425+ field : {
426+ name : 'myField' ,
427+ } ,
428+ useCustomColorPalette : true ,
429+ customColorPalette : [ ] ,
430+ } ;
431+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
432+ expect ( colorProperty . _getMbColor ( ) ) . toBeNull ( ) ;
433+ } ) ;
434+
435+ test ( 'should return mapbox expression for custom color palette' , async ( ) => {
436+ const dynamicStyleOptions = {
437+ type : COLOR_MAP_TYPE . CATEGORICAL ,
438+ field : {
439+ name : 'myField' ,
440+ } ,
441+ useCustomColorPalette : true ,
442+ customColorPalette : [
443+ { stop : null , color : '#f7faff' } ,
444+ { stop : 'MX' , color : '#072f6b' } ,
445+ ] ,
446+ } ;
447+ const colorProperty = makeProperty ( dynamicStyleOptions ) ;
448+ expect ( colorProperty . _getMbColor ( ) ) . toEqual ( [
449+ 'match' ,
450+ [ 'to-string' , [ 'get' , 'myField' ] ] ,
451+ 'MX' ,
452+ '#072f6b' ,
453+ '#f7faff' ,
454+ ] ) ;
455+ } ) ;
456+ } ) ;
457+ } ) ;
458+ } ) ;
459+
240460test ( 'isCategorical should return true when type is categorical' , async ( ) => {
241461 const categoricalColorStyle = makeProperty ( {
242462 type : COLOR_MAP_TYPE . CATEGORICAL ,
0 commit comments