2023-01-09 16:30:21 +01:00
import { render , screen , waitFor } from '@testing-library/react' ;
// eslint-disable-next-line lodash/import-scope
import lodash from 'lodash' ;
import React from 'react' ;
import { config } from '@grafana/runtime' ;
2023-01-13 20:03:08 +01:00
import { logGroupNamesVariable , setupMockedDataSource } from '../../__mocks__/CloudWatchDataSource' ;
2023-01-09 16:30:21 +01:00
import { LogGroupsField } from './LogGroupsField' ;
const originalFeatureToggleValue = config . featureToggles . cloudWatchCrossAccountQuerying ;
const originalDebounce = lodash . debounce ;
const defaultProps = {
datasource : setupMockedDataSource ( ) . datasource ,
region : '' ,
onChange : jest.fn ( ) ,
} ;
describe ( 'LogGroupSelection' , ( ) = > {
beforeEach ( ( ) = > {
jest . resetAllMocks ( ) ;
lodash . debounce = jest . fn ( ) . mockImplementation ( ( fn ) = > {
fn . cancel = ( ) = > { } ;
return fn ;
} ) ;
} ) ;
afterEach ( ( ) = > {
config . featureToggles . cloudWatchCrossAccountQuerying = originalFeatureToggleValue ;
lodash . debounce = originalDebounce ;
} ) ;
2023-01-13 20:03:08 +01:00
it ( 'should call getLogGroups to get associated log group arns and then update props if rendered with legacy log group names' , async ( ) = > {
2023-01-09 16:30:21 +01:00
config . featureToggles . cloudWatchCrossAccountQuerying = true ;
2023-01-13 20:03:08 +01:00
defaultProps . datasource . api . getLogGroups = jest
2023-01-09 16:30:21 +01:00
. fn ( )
. mockResolvedValue ( [ { value : { arn : 'arn' , name : 'loggroupname' } } ] ) ;
render ( < LogGroupsField { ...defaultProps } legacyLogGroupNames = { [ 'loggroupname' ] } / > ) ;
await waitFor ( async ( ) = > expect ( screen . getByText ( 'Select Log Groups' ) ) . toBeInTheDocument ( ) ) ;
2023-01-13 20:03:08 +01:00
expect ( defaultProps . datasource . api . getLogGroups ) . toHaveBeenCalledWith ( {
2023-01-09 16:30:21 +01:00
region : defaultProps.region ,
logGroupNamePrefix : 'loggroupname' ,
} ) ;
expect ( defaultProps . onChange ) . toHaveBeenCalledWith ( [ { arn : 'arn' , name : 'loggroupname' } ] ) ;
} ) ;
2023-01-13 20:03:08 +01:00
it ( 'should not call getLogGroups to get associated log group arns for template variables that were part of the legacy log group names array, only include them in the call to onChange' , async ( ) = > {
2023-01-09 16:30:21 +01:00
config . featureToggles . cloudWatchCrossAccountQuerying = true ;
2023-01-13 20:03:08 +01:00
defaultProps . datasource = setupMockedDataSource ( { variables : [ logGroupNamesVariable ] } ) . datasource ;
defaultProps . datasource . api . getLogGroups = jest
. fn ( )
. mockResolvedValue ( [ { value : { arn : 'arn' , name : 'loggroupname' } } ] ) ;
render ( < LogGroupsField { ...defaultProps } legacyLogGroupNames = { [ 'loggroupname' , logGroupNamesVariable . name ] } / > ) ;
await waitFor ( async ( ) = > expect ( screen . getByText ( 'Select Log Groups' ) ) . toBeInTheDocument ( ) ) ;
expect ( defaultProps . datasource . api . getLogGroups ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( defaultProps . datasource . api . getLogGroups ) . toHaveBeenCalledWith ( {
region : defaultProps.region ,
logGroupNamePrefix : 'loggroupname' ,
} ) ;
expect ( defaultProps . onChange ) . toHaveBeenCalledWith ( [
{ arn : 'arn' , name : 'loggroupname' } ,
{ arn : logGroupNamesVariable.name , name : logGroupNamesVariable.name } ,
] ) ;
} ) ;
it ( 'should not call getLogGroups and update props if rendered with log groups' , async ( ) = > {
config . featureToggles . cloudWatchCrossAccountQuerying = true ;
defaultProps . datasource . api . getLogGroups = jest
2023-01-09 16:30:21 +01:00
. fn ( )
. mockResolvedValue ( [ { value : { arn : 'arn' , name : 'loggroupname' } } ] ) ;
render ( < LogGroupsField { ...defaultProps } logGroups = { [ { arn : 'arn' , name : 'loggroupname' } ] } / > ) ;
await waitFor ( ( ) = > expect ( screen . getByText ( 'Select Log Groups' ) ) . toBeInTheDocument ( ) ) ;
2023-01-13 20:03:08 +01:00
expect ( defaultProps . datasource . api . getLogGroups ) . not . toHaveBeenCalled ( ) ;
2023-01-09 16:30:21 +01:00
expect ( defaultProps . onChange ) . not . toHaveBeenCalled ( ) ;
} ) ;
} ) ;