import React from 'react' ; import { render } from '@testing-library/react' ; import { I18nProvider } from '@kbn/i18n-react' ; import { KibanaContextProvider } from '../../../../../../../../../src/plugins/kibana_react/public' ; import { coreMock } from '../../../../../../../../../src/core/public/mocks' ; import { dataPluginMock } from '../../../../../../../../../src/plugins/data/public/mocks' ; const startMock = coreMock . createStart ( ) ; import { PIVOT_SUPPORTED_AGGS } from '../../../../../../common/types/pivot_aggs' ; import { PivotAggsConfigDict , PivotGroupByConfigDict , PIVOT_SUPPORTED_GROUP_BY_AGGS , } from '../../../../common' ; import { SearchItems } from '../../../../hooks/use_search_items' ; import { getAggNameConflictToastMessages } from './common' ; import { StepDefineForm } from './step_define_form' ; jest . mock ( '../../../../../shared_imports' ) ; jest . mock ( '../../../../../app/app_dependencies' ) ; import { MlSharedContext } from '../../../../../app/__mocks__/shared_context' ; import { getMlSharedImports } from '../../../../../shared_imports' ; const createMockWebStorage = ( ) => ( { clear : jest . fn ( ) , getItem : jest . fn ( ) , key : jest . fn ( ) , removeItem : jest . fn ( ) , setItem : jest . fn ( ) , length : 0 , } ) ; const createMockStorage = ( ) => ( { storage : createMockWebStorage ( ) , get : jest . fn ( ) , set : jest . fn ( ) , remove : jest . fn ( ) , clear : jest . fn ( ) , } ) ; describe ( 'Transform: <DefinePivotForm />' , ( ) => { test ( 'Minimal initialization' , async ( ) => { const mlSharedImports = await getMlSharedImports ( ) ; const searchItems = { indexPattern : { title : 'the-index-pattern-title' , fields : [ ] as any [ ] , } as SearchItems [ 'indexPattern' ] , } ; const services = { ... startMock , data : dataPluginMock . createStartContract ( ) , appName : 'the-test-app' , storage : createMockStorage ( ) , } ; const { getByText } = render ( < I18nProvider > < KibanaContextProvider services = { services } > < MlSharedContext . Provider value = { mlSharedImports } > < StepDefineForm onChange = { jest . fn ( ) } searchItems = { searchItems as SearchItems } / > < /MlSharedContext.Provider> < /KibanaContextProvider> < /I18nProvider>); expect ( getByText ( 'Data view' ) ) . toBeInTheDocument ( ) ; expect ( getByText ( searchItems . indexPattern . title ) ) . toBeInTheDocument ( ) ; } ) ; } ) ; describe ( 'Transform: isAggNameConflict()' , ( ) => { test ( 'detect aggregation name conflicts' , ( ) => { const aggList = { 'the-agg-name' : { agg : PIVOT_SUPPORTED_AGGS . AVG , field : 'the-field-name' , aggName : 'the-agg-name' , dropDownName : 'the-dropdown-name' , } , 'the-namespaced-agg-name.namespace' : { agg : PIVOT_SUPPORTED_AGGS . AVG , field : 'the-field-name' , aggName : 'the-namespaced-agg-name.namespace' , dropDownName : 'the-dropdown-name' , } , } ; const groupByList = { 'the-group-by-agg-name' : { agg : PIVOT_SUPPORTED_GROUP_BY_AGGS . TERMS , field : 'the-field-name' , aggName : 'the-group-by-agg-name' , dropDownName : 'the-dropdown-name' , } , 'the-namespaced-group-by-agg-name.namespace' : { agg : PIVOT_SUPPORTED_GROUP_BY_AGGS . TERMS , field : 'the-field-name' , aggName : 'the-namespaced-group-by-agg-name.namespace' , dropDownName : 'the-dropdown-name' , } , } ; expect ( getAggNameConflictToastMessages ( 'the-other-agg-name' , aggList , groupByList ) ) . toHaveLength ( 0 ) ; expect ( getAggNameConflictToastMessages ( 'the-other-agg-name.namespace' , aggList , groupByList ) ) . toHaveLength ( 0 ) ; expect ( getAggNameConflictToastMessages ( 'the-agg-name' , aggList , groupByList ) ) . toStrictEqual ( [ `An aggregation configuration with the name 'the-agg-name' already exists.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-agg-name.namespace' , aggList , groupByList ) ) . toStrictEqual ( [ `Couldn't add configuration 'the-agg-name.namespace' because of a nesting conflict with 'the-agg-name'.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-group-by-agg-name' , aggList , groupByList ) ) . toStrictEqual ( [ `A group by configuration with the name 'the-group-by-agg-name' already exists.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-group-by-agg-name.namespace' , aggList , groupByList ) ) . toStrictEqual ( [ `Couldn't add configuration 'the-group-by-agg-name.namespace' because of a nesting conflict with 'the-group-by-agg-name'.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-namespaced-agg-name.namespace' , aggList , groupByList ) ) . toStrictEqual ( [ `An aggregation configuration with the name 'the-namespaced-agg-name.namespace' already exists.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-namespaced-agg-name.namespace2' , aggList , groupByList ) ) . toHaveLength ( 0 ) ; expect ( getAggNameConflictToastMessages ( 'the-namespaced-agg-name' , aggList , groupByList ) ) . toStrictEqual ( [ `Couldn't add configuration 'the-namespaced-agg-name' because of a nesting conflict with 'the-namespaced-agg-name.namespace'.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-namespaced-group-by-agg-name.namespace' , aggList , groupByList ) ) . toStrictEqual ( [ `A group by configuration with the name 'the-namespaced-group-by-agg-name.namespace' already exists.` , ] ) ; expect ( getAggNameConflictToastMessages ( 'the-namespaced-group-by-agg-name.namespace2' , aggList , groupByList ) ) . toHaveLength ( 0 ) ; expect ( getAggNameConflictToastMessages ( 'the-namespaced-group-by-agg-name' , aggList , groupByList ) ) . toStrictEqual ( [ `Couldn't add configuration 'the-namespaced-group-by-agg-name' because of a nesting conflict with 'the-namespaced-group-by-agg-name.namespace'.` , ] ) ; } ) ; } ) ;