AzureMonitor: Fix template variables in ARG subscription field (#63731)

Add support for template variables in sub field
This commit is contained in:
Andreas Christou 2023-02-24 16:37:09 +00:00 committed by GitHub
parent 7e6ed3bfcc
commit 823aaaeb7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -92,4 +92,30 @@ describe('ArgQueryEditor', () => {
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo', 'bar'] }));
expect(onChange).not.toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo', 'bar', 'foobar'] }));
});
it('should keep a template variable if used in the subscription field', async () => {
const onChange = jest.fn();
const datasource = createMockDatasource({
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }]),
});
const query = createMockQuery({
subscriptions: ['$test'],
});
render(
<ArgQueryEditor
{...defaultProps}
datasource={datasource}
onChange={onChange}
query={query}
variableOptionGroup={{ label: 'Template Variables', options: [{ label: '$test', value: '$test' }] }}
/>
);
expect(
await screen.findByTestId(selectors.components.queryEditor.argsQueryEditor.container.input)
).toBeInTheDocument();
expect(
await screen.findByTestId(selectors.components.queryEditor.argsQueryEditor.subscriptions.input)
).toHaveTextContent('$test');
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['$test'] }));
});
});

View File

@ -33,7 +33,9 @@ function selectSubscriptions(
if (querySubscriptions.length === 0 && fetchedSubscriptions.length) {
querySubscriptions = [fetchedSubscriptions[0]];
}
const commonSubscriptions = intersection(querySubscriptions, fetchedSubscriptions);
const templateVars = querySubscriptions.filter((sub) => sub.includes('$'));
const commonSubscriptions = intersection(querySubscriptions, fetchedSubscriptions).concat(templateVars);
if (fetchedSubscriptions.length && querySubscriptions.length > commonSubscriptions.length) {
// If not all of the query subscriptions are in the list of fetched subscriptions, then
// select only the ones present (or the first one if none is present)