import { Arguments , visDimension } from './vis_dimension' ; import { functionWrapper } from '../../../expressions/common/expression_functions/specs/tests/utils' ; import { Datatable } from '../../../expressions/common' ; import moment from 'moment' ; describe ( 'interpreter/functions#vis_dimension' , ( ) => { const fn = functionWrapper ( visDimension ( ) ) ; const column1 = 'username' ; const column2 = '@timestamp' ; const input = { type : 'datatable' , columns : [ { id : column1 , name : column1 , meta : { type : 'string' } } , { id : column2 , name : column2 , meta : { type : 'date' } } , ] , rows : [ { [ column1 ] : 'user1' , [ column2 ] : moment ( ) . toISOString ( ) } , { [ column1 ] : 'user2' , [ column2 ] : moment ( ) . toISOString ( ) } , ] , } ; it ( 'should return vis_dimension accessor in number format when type of the passed accessor is number' , ( ) => { const accessor = 0 ; const args = { accessor } ; const result = fn ( input , args ) ; expect ( result ) . toHaveProperty ( 'type' , 'vis_dimension' ) ; expect ( result ) . toHaveProperty ( 'accessor' , accessor ) ; expect ( result ) . toHaveProperty ( 'format' ) ; expect ( result . format ) . toBeDefined ( ) ; expect ( typeof result . format === 'object' ) . toBeTruthy ( ) ; } ) ; it ( 'should return vis_dimension accessor in DatatableColumn format when type of the passed accessor is string' , ( ) => { const accessor = column2 ; const args = { accessor } ; const searchingObject = input . columns . filter ( ( { id } ) => id === accessor ) [ 0 ] ; const result = fn ( input , args ) ; expect ( result ) . toHaveProperty ( 'type' , 'vis_dimension' ) ; expect ( result ) . toHaveProperty ( 'accessor' ) ; expect ( result . accessor ) . toMatchObject ( searchingObject ) ; expect ( result ) . toHaveProperty ( 'format' ) ; expect ( result . format ) . toBeDefined ( ) ; expect ( typeof result . format === 'object' ) . toBeTruthy ( ) ; } ) ; it ( 'should throw error when the passed number accessor is out of columns array boundary' , ( ) => { const accessor = input . columns . length ; const args = { accessor } ; expect ( ( ) => fn ( input , args ) ) . toThrowError ( 'Column name or index provided is invalid' ) ; } ) ; it ( "should throw error when the passed column doesn't exist in columns" , ( ) => { const accessor = column1 + '_modified' ; const args = { accessor } ; expect ( ( ) => fn ( input , args ) ) . toThrowError ( 'Column name or index provided is invalid' ) ; } ) ; } ) ;