Files
grafana/public/app/features/alerting/unified/GrafanaRuleQueryViewer.test.tsx
Sonia Aguilar 67722de343 Alerting: Add DAG errors to alert rule creation and view (#99423)
* catch error in query tab when running query throws an error

* add translations

* fix translations

* update query runner to omit nodes that failed to link

* remove unused function

* add DAG errors to AlertingQueryRunner

* bump CI

* fix test

* update test

* fix i18n

* revert code pieve

* Bring the piece of code back 😁

* bail from runner when no queries are to be executed

* add tests and translations

* refactor prepareQueries to omit broken refs and exclude descendant nodes

* update code comments

* fix omitting descendant nodes

* add all broken or missing nodes to panel errors

* go go drone

* remove unused function

* fix prettier and translations

* add export

---------

Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
2025-02-17 16:11:52 +01:00

88 lines
2.2 KiB
TypeScript

import { render, screen, waitFor } from 'test/test-utils';
import { AlertDataQuery, AlertQuery } from 'app/types/unified-alerting-dto';
import { GrafanaRuleQueryViewer } from './GrafanaRuleQueryViewer';
import { mockCombinedRule } from './mocks';
describe('GrafanaRuleQueryViewer', () => {
it('renders without crashing', async () => {
const rule = mockCombinedRule();
const expressions = [getExpression('F'), getExpression('G'), getExpression('H'), getExpression('I')];
render(<GrafanaRuleQueryViewer queries={[...queries, ...expressions]} condition="A" rule={rule} />);
await waitFor(() => expect(screen.getByTestId('queries-container')).toHaveStyle('flex-wrap: wrap'));
expect(screen.getByTestId('expressions-container')).toHaveStyle('flex-wrap: wrap');
});
it('should catch cyclical references', async () => {
const rule = mockCombinedRule();
const queries = [
getExpression('A'), // this always points to A
];
jest.spyOn(console, 'error').mockImplementation((message) => {
expect(message).toMatch(/Failed to parse thresholds/i);
});
render(<GrafanaRuleQueryViewer queries={queries} condition="A" rule={rule} />);
});
});
function getDataSourceQuery(sourceRefId: string) {
const query: AlertQuery<AlertDataQuery> = {
refId: sourceRefId,
datasourceUid: 'abc123',
queryType: '',
relativeTimeRange: {
from: 600,
to: 0,
},
model: {
refId: sourceRefId,
},
};
return query;
}
const queries = [
getDataSourceQuery('A'),
getDataSourceQuery('B'),
getDataSourceQuery('C'),
getDataSourceQuery('D'),
getDataSourceQuery('E'),
];
function getExpression(refId: string) {
const expr = {
refId: refId,
datasourceUid: '__expr__',
queryType: '',
model: {
refId: refId,
type: 'classic_conditions',
datasource: { type: '' },
conditions: [
{
type: 'query',
evaluator: {
params: [3],
type: 'gt',
},
operator: {
type: 'and',
},
query: {
params: ['A'],
},
reducer: {
params: [],
type: 'last',
},
},
],
},
};
return expr;
}