import { Vis } from './vis' ; jest . mock ( './services' , ( ) => { class MockVisualizationController { constructor ( ) { } render ( ) { return new Promise ( ( resolve ) => { resolve ( ) ; } ) ; } destroy ( ) { } } const { BaseVisType } = require ( './vis_types/base_vis_type' ) ; const { SearchSource } = require ( '../../data/common/search/search_source' ) ; const stubIndexPattern = require ( '../../data/common/stubs' ) ; const visType = new BaseVisType ( { name : 'pie' , title : 'pie' , icon : 'pie-icon' , visualization : MockVisualizationController , } ) ; return { getTypes : ( ) => ( { get : ( ) => visType } ) , getAggs : ( ) => ( { createAggConfigs : ( indexPattern , cfg ) => ( { aggs : cfg . map ( ( aggConfig ) => ( { ... aggConfig , toJSON : ( ) => aggConfig } ) ) , } ) , } ) , getSearch : ( ) => ( { searchSource : { create : ( ) => { return new SearchSource ( { index : stubIndexPattern } ) ; } , } , } ) , } ; } ) ; describe ( 'Vis Class' , function ( ) { let vis ; const stateFixture = { type : 'pie' , title : 'pie' , data : { aggs : [ { type : 'avg' as any , schema : 'metric' , params : { field : 'bytes' } } , { type : 'terms' as any , schema : 'segment' , params : { field : 'machine.os' } } , { type : 'terms' as any , schema : 'segment' , params : { field : 'geo.src' } } , ] , searchSource : { index : '123' , } , } , params : { isDonut : true } , } ; beforeEach ( async function ( ) { vis = new Vis ( 'test' , stateFixture as any ) ; await vis . setState ( stateFixture as any ) ; } ) ; const verifyVis = function ( visToVerify ) { expect ( visToVerify ) . toHaveProperty ( 'data' ) ; expect ( visToVerify . data ) . toHaveProperty ( 'aggs' ) ; expect ( visToVerify . data . aggs ! . aggs ) . toHaveLength ( 3 ) ; expect ( visToVerify ) . toHaveProperty ( 'type' ) ; expect ( visToVerify ) . toHaveProperty ( 'params' ) ; expect ( visToVerify . params ) . toHaveProperty ( 'isDonut' , true ) ; } ; describe ( 'initialization' , function ( ) { it ( 'should set the state' , function ( ) { verifyVis ( vis ) ; } ) ; } ) ; describe ( 'getState()' , function ( ) { it ( 'should get a state that represents the... er... state' , function ( ) { const state = vis . serialize ( ) ; expect ( state ) . toHaveProperty ( 'type' , 'pie' ) ; expect ( state ) . toHaveProperty ( 'params' ) ; expect ( state . params ) . toHaveProperty ( 'isDonut' , true ) ; expect ( state . data ) . toHaveProperty ( 'aggs' ) ; expect ( state . data . aggs ) . toHaveLength ( 3 ) ; } ) ; } ) ; describe ( 'isHierarchical()' , function ( ) { it ( 'should return false for non-hierarchical vis (like histogram)' , function ( ) { expect ( vis . isHierarchical ( ) ) . toBe ( false ) ; } ) ; it ( 'should return true for hierarchical vis (like pie)' , function ( ) { ( vis . type as any ) . hierarchicalData = true ; expect ( vis . isHierarchical ( ) ) . toBe ( true ) ; } ) ; } ) ; } ) ;