Azure Monitor: reset resource uri when resource changes (#48235)

This commit is contained in:
Kevin Yu
2022-04-26 07:36:37 -07:00
committed by GitHub
parent 0301d956da
commit 1be45d1ed9
3 changed files with 124 additions and 0 deletions
@@ -76,6 +76,123 @@ describe('Azure Monitor QueryEditor', () => {
});
});
it('should change the resource group when selected', async () => {
const mockDatasource = createMockDatasource();
const onChange = jest.fn();
const mockQuery = createMockQuery();
mockDatasource.getResourceGroups = jest.fn().mockResolvedValue([
{ text: 'grafanastaging', value: 'grafanastaging' },
{ text: 'Grafana Prod', value: 'grafanaprod' },
]);
render(
<MetricsQueryEditor
subscriptionId="123"
query={createMockQuery()}
datasource={mockDatasource}
variableOptionGroup={variableOptionGroup}
onChange={onChange}
setError={() => {}}
/>
);
await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument());
const resourceGroup = await screen.findByLabelText('Resource group');
await selectOptionInTest(resourceGroup, 'Grafana Prod');
expect(onChange).toHaveBeenLastCalledWith({
...mockQuery,
azureMonitor: {
...mockQuery.azureMonitor,
resourceUri: '',
resourceGroup: 'grafanaprod',
metricDefinition: undefined,
metricNamespace: undefined,
resourceName: undefined,
metricName: undefined,
aggregation: undefined,
timeGrain: '',
dimensionFilters: [],
},
});
});
it('should change the resource type when selected', async () => {
const mockDatasource = createMockDatasource();
const onChange = jest.fn();
const mockQuery = createMockQuery();
mockDatasource.getMetricDefinitions = jest.fn().mockResolvedValue([
{ text: 'Virtual Machine', value: 'azure/vm' },
{ text: 'Database', value: 'azure/db' },
]);
render(
<MetricsQueryEditor
subscriptionId="123"
query={createMockQuery()}
datasource={mockDatasource}
variableOptionGroup={variableOptionGroup}
onChange={onChange}
setError={() => {}}
/>
);
await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument());
const resourceGroup = await screen.findByLabelText('Resource type');
await selectOptionInTest(resourceGroup, 'Virtual Machine');
expect(onChange).toHaveBeenLastCalledWith({
...mockQuery,
azureMonitor: {
...mockQuery.azureMonitor,
resourceUri: '',
metricDefinition: 'azure/vm',
resourceName: undefined,
metricNamespace: undefined,
metricName: undefined,
aggregation: undefined,
timeGrain: '',
dimensionFilters: [],
},
});
});
it('should change the resource name when selected', async () => {
const mockDatasource = createMockDatasource();
const onChange = jest.fn();
const mockQuery = createMockQuery();
mockDatasource.getResourceNames = jest.fn().mockResolvedValue([
{ text: 'ResourceName1', value: 'resource-name-1' },
{ text: 'ResourceName2', value: 'resource-name-2' },
]);
render(
<MetricsQueryEditor
subscriptionId="123"
query={createMockQuery()}
datasource={mockDatasource}
variableOptionGroup={variableOptionGroup}
onChange={onChange}
setError={() => {}}
/>
);
await waitFor(() => expect(screen.getByTestId('azure-monitor-metrics-query-editor')).toBeInTheDocument());
const resourceGroup = await screen.findByLabelText('Resource name');
await selectOptionInTest(resourceGroup, 'ResourceName1');
expect(onChange).toHaveBeenLastCalledWith({
...mockQuery,
azureMonitor: {
...mockQuery.azureMonitor,
resourceUri: '',
resourceName: 'resource-name-1',
metricNamespace: undefined,
metricName: undefined,
aggregation: undefined,
timeGrain: '',
dimensionFilters: [],
},
});
});
it('should change the metric name when selected', async () => {
const mockDatasource = createMockDatasource();
const onChange = jest.fn();
@@ -314,6 +314,7 @@ describe('AzureMonitor: updateSubscriptions', () => {
azureMonitor: {
dimensionFilters: [],
timeGrain: '',
resourceUri: '',
},
},
},
@@ -327,6 +328,7 @@ describe('AzureMonitor: updateSubscriptions', () => {
azureMonitor: {
dimensionFilters: [],
timeGrain: '',
resourceUri: '',
},
},
},
@@ -341,6 +343,7 @@ describe('AzureMonitor: updateSubscriptions', () => {
azureMonitor: {
dimensionFilters: [],
timeGrain: '',
resourceUri: '',
},
},
},
@@ -25,6 +25,7 @@ export function setSubscriptionID(query: AzureMonitorQuery, subscriptionID: stri
subscription: subscriptionID,
azureMonitor: {
...query.azureMonitor,
resourceUri: '',
resourceGroup: undefined,
metricDefinition: undefined,
metricNamespace: undefined,
@@ -46,6 +47,7 @@ export function setResourceGroup(query: AzureMonitorQuery, resourceGroup: string
...query,
azureMonitor: {
...query.azureMonitor,
resourceUri: '',
resourceGroup: resourceGroup,
metricDefinition: undefined,
metricNamespace: undefined,
@@ -68,6 +70,7 @@ export function setResourceType(query: AzureMonitorQuery, resourceType: string |
...query,
azureMonitor: {
...query.azureMonitor,
resourceUri: '',
metricDefinition: resourceType,
resourceName: undefined,
metricNamespace: undefined,
@@ -90,6 +93,7 @@ export function setResourceName(query: AzureMonitorQuery, resourceName: string |
...query,
azureMonitor: {
...query.azureMonitor,
resourceUri: '',
resourceName: resourceName,
metricNamespace: undefined,
metricName: undefined,