@ -1,4 +1,5 @@
import { act , render , screen , waitFor , within } from '@testing-library/react' ;
import { act , render , screen , waitFor } from '@testing-library/react' ;
import userEvent from '@testing-library/user-event' ;
import React from 'react' ;
import { openMenu , select } from 'react-select-event' ;
@ -60,6 +61,7 @@ describe('VisualMetricQueryEditor', () => {
const mockMetricDescriptor = createMockMetricDescriptor ( { displayName : 'metricName_test' , type : 'test_type' } ) ;
const datasource = createMockDatasource ( {
getMetricTypes : jest.fn ( ) . mockResolvedValue ( [ createMockMetricDescriptor ( ) , mockMetricDescriptor ] ) ,
filterMetricsByType : jest.fn ( ) . mockResolvedValue ( [ createMockMetricDescriptor ( ) , mockMetricDescriptor ] ) ,
getLabels : jest.fn ( ) . mockResolvedValue ( [ ] ) ,
} ) ;
@ -72,6 +74,7 @@ describe('VisualMetricQueryEditor', () => {
} ) ;
const metricName = await screen . findByLabelText ( 'Metric name' ) ;
openMenu ( metricName ) ;
await userEvent . type ( metricName , 'test' ) ;
await waitFor ( ( ) = > expect ( document . body ) . toHaveTextContent ( 'metricName_test' ) ) ;
await act ( async ( ) = > {
await select ( metricName , 'metricName_test' , { container : document.body } ) ;
@ -81,66 +84,6 @@ describe('VisualMetricQueryEditor', () => {
) ;
} ) ;
it ( 'should render available metric options according to the selected service' , async ( ) = > {
const onChange = jest . fn ( ) ;
const query = createMockTimeSeriesList ( ) ;
const datasource = createMockDatasource ( {
getMetricTypes : jest.fn ( ) . mockResolvedValue ( [
createMockMetricDescriptor ( {
service : 'service_a' ,
serviceShortName : 'srv_a' ,
type : 'metric1' ,
description : 'description_metric1' ,
displayName : 'displayName_metric1' ,
} ) ,
createMockMetricDescriptor ( {
service : 'service_b' ,
serviceShortName : 'srv_b' ,
type : 'metric2' ,
description : 'description_metric2' ,
displayName : 'displayName_metric2' ,
} ) ,
createMockMetricDescriptor ( {
service : 'service_b' ,
serviceShortName : 'srv_b' ,
type : 'metric3' ,
description : 'description_metric3' ,
displayName : 'displayName_metric3' ,
} ) ,
] ) ,
getLabels : jest.fn ( ) . mockResolvedValue ( [ ] ) ,
} ) ;
render ( < VisualMetricQueryEditor { ...defaultProps } onChange = { onChange } datasource = { datasource } query = { query } / > ) ;
const service = await screen . findByLabelText ( 'Service' ) ;
openMenu ( service ) ;
await act ( async ( ) = > {
await select ( service , 'Srv A' , { container : document.body } ) ;
} ) ;
const metricName = await screen . findByLabelText ( 'Metric name' ) ;
openMenu ( metricName ) ;
const metricNameOptions = screen . getByLabelText ( 'Select options menu' ) ;
expect ( within ( metricNameOptions ) . getByText ( 'description_metric1' ) ) . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . getByText ( 'displayName_metric1' ) ) . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . queryByText ( 'displayName_metric2' ) ) . not . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . queryByText ( 'description_metric2' ) ) . not . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . queryByText ( 'displayName_metric3' ) ) . not . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . queryByText ( 'description_metric3' ) ) . not . toBeInTheDocument ( ) ;
openMenu ( service ) ;
await act ( async ( ) = > {
await select ( service , 'Srv B' , { container : document.body } ) ;
} ) ;
expect ( within ( metricNameOptions ) . queryByText ( 'displayName_metric1' ) ) . not . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . queryByText ( 'description_metric1' ) ) . not . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . getByText ( 'displayName_metric2' ) ) . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . getByText ( 'description_metric2' ) ) . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . getByText ( 'displayName_metric3' ) ) . toBeInTheDocument ( ) ;
expect ( within ( metricNameOptions ) . getByText ( 'description_metric3' ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should have a distinct list of services' , async ( ) = > {
const onChange = jest . fn ( ) ;
const datasource = createMockDatasource ( {
@ -210,6 +153,12 @@ describe('VisualMetricQueryEditor', () => {
} ) ;
const onChange = jest . fn ( ) ;
const datasource = createMockDatasource ( {
filterMetricsByType : jest
. fn ( )
. mockResolvedValue ( [
createMockMetricDescriptor ( ) ,
createMockMetricDescriptor ( { type : 'type2' , displayName : 'metricName2' , metricKind : MetricKind.GAUGE } ) ,
] ) ,
getMetricTypes : jest
. fn ( )
. mockResolvedValue ( [
@ -227,7 +176,11 @@ describe('VisualMetricQueryEditor', () => {
expect ( await screen . findByText ( 'metric.test_groupby' ) ) . toBeInTheDocument ( ) ;
const metric = await screen . findByLabelText ( 'Metric name' ) ;
openMenu ( metric ) ;
await select ( metric , 'metricName2' , { container : document.body } ) ;
await userEvent . type ( metric , 'type2' ) ;
await waitFor ( ( ) = > expect ( document . body ) . toHaveTextContent ( 'metricName2' ) ) ;
await act ( async ( ) = > {
await select ( metric , 'metricName2' , { container : document.body } ) ;
} ) ;
expect ( onChange ) . toBeCalledWith (
expect . objectContaining ( { filters : [ 'metric.test_label' , '=' , 'test' , 'AND' , 'metric.type' , '=' , 'type2' ] } )
) ;