Files
grafana/public/app/plugins/datasource/cloud-monitoring/components/VariableQueryEditor.test.tsx
Josh Hunt 3c6e0e8ef8 Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

68 lines
2.3 KiB
TypeScript

import React from 'react';
// @ts-ignore
import renderer from 'react-test-renderer';
import { VariableModel } from '@grafana/data';
import CloudMonitoringDatasource from '../datasource';
import { CloudMonitoringVariableQuery, MetricFindQueryTypes } from '../types';
import { CloudMonitoringVariableQueryEditor, Props } from './VariableQueryEditor';
jest.mock('../functions', () => ({
getMetricTypes: (): any => ({ metricTypes: [], selectedMetricType: '' }),
extractServicesFromMetricDescriptors: (): any[] => [],
}));
jest.mock('@grafana/runtime', () => {
const original = jest.requireActual('@grafana/runtime');
return {
...original,
getTemplateSrv: () => ({
replace: (s: string) => s,
getVariables: () => [] as unknown as VariableModel[],
}),
};
});
const props: Props = {
onChange: (query) => {},
query: {} as unknown as CloudMonitoringVariableQuery,
datasource: {
getDefaultProject: () => '',
getProjects: async () => Promise.resolve([]),
getMetricTypes: async (projectName: string) => Promise.resolve([]),
getSLOServices: async (projectName: string) => Promise.resolve([]),
getServiceLevelObjectives: (projectName: string, serviceId: string) => Promise.resolve([]),
} as unknown as CloudMonitoringDatasource,
onRunQuery: () => {},
};
describe('VariableQueryEditor', () => {
it('renders correctly', () => {
const tree = renderer.create(<CloudMonitoringVariableQueryEditor {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});
describe('and a new variable is created', () => {
it('should trigger a query using the first query type in the array', (done) => {
props.onChange = (query) => {
expect(query.selectedQueryType).toBe('projects');
done();
};
renderer.create(<CloudMonitoringVariableQueryEditor {...props} />).toJSON();
});
});
describe('and an existing variable is edited', () => {
it('should trigger new query using the saved query type', (done) => {
props.query = { selectedQueryType: MetricFindQueryTypes.LabelKeys } as unknown as CloudMonitoringVariableQuery;
props.onChange = (query) => {
expect(query.selectedQueryType).toBe('labelKeys');
done();
};
renderer.create(<CloudMonitoringVariableQueryEditor {...props} />).toJSON();
});
});
});