2022-06-15 18:03:47 -07:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { openMenu } from 'react-select-event';
|
|
|
|
|
|
2023-12-20 08:26:39 -07:00
|
|
|
import { CustomVariableModel } from '@grafana/data';
|
2022-06-15 18:03:47 -07:00
|
|
|
|
2022-09-15 10:12:26 -04:00
|
|
|
import { createMockDatasource } from '../__mocks__/cloudMonitoringDatasource';
|
2022-12-20 12:47:49 +01:00
|
|
|
import { createMockMetricDescriptor } from '../__mocks__/cloudMonitoringMetricDescriptor';
|
|
|
|
|
import { createMockTimeSeriesList } from '../__mocks__/cloudMonitoringQuery';
|
2023-05-22 17:19:54 +01:00
|
|
|
import { MetricKind, ValueTypes } from '../types/query';
|
2022-06-15 18:03:47 -07:00
|
|
|
|
|
|
|
|
import { Alignment } from './Alignment';
|
|
|
|
|
|
2023-12-20 08:26:39 -07:00
|
|
|
let getTempVars = () => [] as CustomVariableModel[];
|
|
|
|
|
let replace = () => '';
|
|
|
|
|
|
|
|
|
|
jest.mock('@grafana/runtime', () => {
|
|
|
|
|
return {
|
|
|
|
|
__esModule: true,
|
|
|
|
|
...jest.requireActual('@grafana/runtime'),
|
|
|
|
|
getTemplateSrv: () => ({
|
|
|
|
|
replace: replace,
|
|
|
|
|
getVariables: getTempVars,
|
|
|
|
|
updateTimeRange: jest.fn(),
|
|
|
|
|
containsTemplate: jest.fn(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-06-15 18:03:47 -07:00
|
|
|
|
|
|
|
|
describe('Alignment', () => {
|
|
|
|
|
it('renders alignment fields', () => {
|
|
|
|
|
const datasource = createMockDatasource();
|
2022-12-20 12:47:49 +01:00
|
|
|
const query = createMockTimeSeriesList();
|
2022-06-15 18:03:47 -07:00
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
|
|
render(
|
|
|
|
|
<Alignment
|
|
|
|
|
refId="refId"
|
|
|
|
|
customMetaData={{}}
|
|
|
|
|
datasource={datasource}
|
|
|
|
|
query={query}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
templateVariableOptions={[]}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByLabelText('Alignment function')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByLabelText('Alignment period')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can set the alignment function', async () => {
|
|
|
|
|
const datasource = createMockDatasource();
|
2022-12-20 12:47:49 +01:00
|
|
|
const query = createMockTimeSeriesList();
|
2022-06-15 18:03:47 -07:00
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
|
|
render(
|
|
|
|
|
<Alignment
|
|
|
|
|
refId="refId"
|
|
|
|
|
customMetaData={{}}
|
|
|
|
|
datasource={datasource}
|
|
|
|
|
query={query}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
templateVariableOptions={[]}
|
2022-12-20 12:47:49 +01:00
|
|
|
metricDescriptor={createMockMetricDescriptor({ metricKind: MetricKind.GAUGE, valueType: ValueTypes.INT64 })}
|
2022-06-15 18:03:47 -07:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const alignmentFunction = screen.getByLabelText('Alignment function');
|
|
|
|
|
openMenu(alignmentFunction);
|
|
|
|
|
await userEvent.click(screen.getByText('percent change'));
|
|
|
|
|
expect(onChange).toBeCalledWith(expect.objectContaining({ perSeriesAligner: 'ALIGN_PERCENT_CHANGE' }));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can set the alignment period', async () => {
|
|
|
|
|
const datasource = createMockDatasource();
|
2022-12-20 12:47:49 +01:00
|
|
|
const query = createMockTimeSeriesList();
|
2022-06-15 18:03:47 -07:00
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
|
|
render(
|
|
|
|
|
<Alignment
|
|
|
|
|
refId="refId"
|
|
|
|
|
customMetaData={{}}
|
|
|
|
|
datasource={datasource}
|
|
|
|
|
query={query}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
templateVariableOptions={[]}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const alignmentPeriod = screen.getByLabelText('Alignment period');
|
|
|
|
|
openMenu(alignmentPeriod);
|
|
|
|
|
await userEvent.click(screen.getByText('1m'));
|
|
|
|
|
expect(onChange).toBeCalledWith(expect.objectContaining({ alignmentPeriod: '+60s' }));
|
|
|
|
|
});
|
|
|
|
|
});
|