import { getTopMetricsAggConfig } from './config' ; import { PivotAggsConfigTopMetrics } from './types' ; describe ( 'top metrics agg config' , ( ) => { let config ; beforeEach ( ( ) => { config = getTopMetricsAggConfig ( { agg : 'top_metrics' , aggName : 'test-agg' , field : [ 'test-field' ] , dropDownName : 'test-agg' , } ) ; } ) ; describe ( '#setUiConfigFromEs' , ( ) => { test ( 'sets config with a special field' , ( ) => { config . setUiConfigFromEs ( { metrics : { field : 'test-field-01' , } , sort : '_score' , } ) ; expect ( config . field ) . toEqual ( [ 'test-field-01' ] ) ; expect ( config . aggConfig ) . toEqual ( { sortField : '_score' , } ) ; } ) ; test ( 'sets config with a simple sort direction definition' , ( ) => { config . setUiConfigFromEs ( { metrics : [ { field : 'test-field-01' , } , { field : 'test-field-02' , } , ] , sort : { 'sort-field' : 'asc' , } , } ) ; expect ( config . field ) . toEqual ( [ 'test-field-01' , 'test-field-02' ] ) ; expect ( config . aggConfig ) . toEqual ( { sortField : 'sort-field' , sortSettings : { order : 'asc' , } , } ) ; } ) ; test ( 'sets config with a sort definition params not supported by the UI' , ( ) => { config . setUiConfigFromEs ( { metrics : [ { field : 'test-field-01' , } , ] , sort : { 'offer.price' : { order : 'desc' , mode : 'avg' , nested : { path : 'offer' , filter : { term : { 'offer.color' : 'blue' } , } , } , } , } , } ) ; expect ( config . field ) . toEqual ( [ 'test-field-01' ] ) ; expect ( config . aggConfig ) . toEqual ( { sortField : 'offer.price' , sortSettings : { order : 'desc' , mode : 'avg' , nested : { path : 'offer' , filter : { term : { 'offer.color' : 'blue' } , } , } , } , } ) ; } ) ; } ) ; describe ( '#getEsAggConfig' , ( ) => { test ( 'rejects invalid config' , ( ) => { config . field = [ 'field-01' , 'field-02' ] ; config . aggConfig = { sortSettings : { order : 'asc' , } , } ; expect ( config . getEsAggConfig ( ) ) . toEqual ( null ) ; } ) ; test ( 'rejects invalid config with missing sort direction' , ( ) => { config . field = [ 'field-01' , 'field-02' ] ; config . aggConfig = { sortField : 'sort-field' , } ; expect ( config . getEsAggConfig ( ) ) . toEqual ( null ) ; } ) ; test ( 'converts valid config' , ( ) => { config . field = [ 'field-01' , 'field-02' ] ; config . aggConfig = { sortField : 'sort-field' , sortSettings : { order : 'asc' , } , } ; expect ( config . getEsAggConfig ( ) ) . toEqual ( { metrics : [ { field : 'field-01' } , { field : 'field-02' } ] , sort : { 'sort-field' : 'asc' , } , } ) ; } ) ; test ( 'preserves unsupported config' , ( ) => { config . field = [ 'field-01' , 'field-02' ] ; config . aggConfig = { sortField : 'sort-field' , sortSettings : { order : 'asc' , nested : { path : 'order' , } , } , size : 2 , } ; expect ( config . getEsAggConfig ( ) ) . toEqual ( { metrics : [ { field : 'field-01' } , { field : 'field-02' } ] , sort : { 'sort-field' : { order : 'asc' , nested : { path : 'order' , } , } , } , size : 2 , } ) ; } ) ; test ( 'converts configs with a special sorting field' , ( ) => { config . field = [ 'field-01' , 'field-02' ] ; config . aggConfig = { sortField : '_score' , } ; expect ( config . getEsAggConfig ( ) ) . toEqual ( { metrics : [ { field : 'field-01' } , { field : 'field-02' } ] , sort : '_score' , } ) ; } ) ; } ) ; } ) ;