diff --git a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/__snapshots__/SimplifiedRuleEditor.test.tsx.snap b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/__snapshots__/SimplifiedRuleEditor.test.tsx.snap index 4c4fa2694cc..4b78f3f6f26 100644 --- a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/__snapshots__/SimplifiedRuleEditor.test.tsx.snap +++ b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/__snapshots__/SimplifiedRuleEditor.test.tsx.snap @@ -79,9 +79,7 @@ exports[`Can create a new grafana managed alert using simplified routing can cre "type": "and", }, "query": { - "params": [ - "B", - ], + "params": [], }, "reducer": { "params": [], @@ -249,9 +247,7 @@ exports[`Can create a new grafana managed alert using simplified routing switch "type": "and", }, "query": { - "params": [ - "B", - ], + "params": [], }, "reducer": { "params": [], @@ -422,9 +418,7 @@ exports[`Can create a new grafana managed alert using simplified routing switch "type": "and", }, "query": { - "params": [ - "B", - ], + "params": [], }, "reducer": { "params": [], @@ -598,9 +592,7 @@ exports[`Can create a new grafana managed alert using simplified routing switch "type": "and", }, "query": { - "params": [ - "B", - ], + "params": [], }, "reducer": { "params": [], @@ -771,9 +763,7 @@ exports[`Can create a new grafana managed alert using simplified routing switch "type": "and", }, "query": { - "params": [ - "B", - ], + "params": [], }, "reducer": { "params": [], diff --git a/public/app/features/alerting/unified/rule-editor/__snapshots__/RuleEditorGrafanaRules.test.tsx.snap b/public/app/features/alerting/unified/rule-editor/__snapshots__/RuleEditorGrafanaRules.test.tsx.snap index 73ac81a4a3c..b95aee0339f 100644 --- a/public/app/features/alerting/unified/rule-editor/__snapshots__/RuleEditorGrafanaRules.test.tsx.snap +++ b/public/app/features/alerting/unified/rule-editor/__snapshots__/RuleEditorGrafanaRules.test.tsx.snap @@ -81,9 +81,7 @@ exports[`RuleEditor grafana managed rules can create new grafana managed alert 1 "type": "and", }, "query": { - "params": [ - "B", - ], + "params": [], }, "reducer": { "params": [], diff --git a/public/app/features/alerting/unified/utils/rule-form.test.ts b/public/app/features/alerting/unified/utils/rule-form.test.ts index fea528c2075..54154e199e5 100644 --- a/public/app/features/alerting/unified/utils/rule-form.test.ts +++ b/public/app/features/alerting/unified/utils/rule-form.test.ts @@ -1,5 +1,5 @@ import { PromQuery } from '@grafana/prometheus'; -import { ExpressionDatasourceUID, ExpressionQueryType } from 'app/features/expressions/types'; +import { ExpressionDatasourceUID, ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types'; import { RuleWithLocation } from 'app/types/unified-alerting'; import { AlertDataQuery, @@ -452,15 +452,24 @@ describe('getInstantFromDataQuery', () => { }); }); +function isExpressionQuery(model: unknown): model is ExpressionQuery { + return typeof model === 'object' && model !== null && 'type' in model; +} + describe('getDefaultExpressions', () => { it('should create a reduce expression as the first query', () => { const result = getDefaultExpressions('B', 'C'); const reduceQuery = result[0]; - const model = reduceQuery.model; + const { model } = reduceQuery; expect(reduceQuery.refId).toBe('B'); expect(reduceQuery.datasourceUid).toBe(ExpressionDatasourceUID); - expect(reduceQuery.queryType).toBe(''); + expect(reduceQuery.queryType).toBe('expression'); + + if (!isExpressionQuery(model)) { + throw new Error('Expected ExpressionQuery'); + } + expect(model.type).toBe(ExpressionQueryType.reduce); expect(model.datasource?.uid).toBe(ExpressionDatasourceUID); expect(model.reducer).toBe('last'); @@ -469,7 +478,11 @@ describe('getDefaultExpressions', () => { it('should create reduce expression with proper conditions structure', () => { const result = getDefaultExpressions('B', 'C'); const reduceQuery = result[0]; - const model = reduceQuery.model; + const { model } = reduceQuery; + + if (!isExpressionQuery(model)) { + throw new Error('Expected ExpressionQuery'); + } expect(model.conditions).toHaveLength(1); expect(model.expression).toBe('A'); @@ -495,11 +508,16 @@ describe('getDefaultExpressions', () => { it('should create a threshold expression as the second query', () => { const result = getDefaultExpressions('B', 'C'); const thresholdQuery = result[1]; - const model = thresholdQuery.model; + const { model } = thresholdQuery; expect(thresholdQuery.refId).toBe('C'); expect(thresholdQuery.datasourceUid).toBe(ExpressionDatasourceUID); - expect(thresholdQuery.queryType).toBe(''); + expect(thresholdQuery.queryType).toBe('expression'); + + if (!isExpressionQuery(model)) { + throw new Error('Expected ExpressionQuery'); + } + expect(model.type).toBe(ExpressionQueryType.threshold); expect(model.datasource?.uid).toBe(ExpressionDatasourceUID); }); @@ -507,7 +525,11 @@ describe('getDefaultExpressions', () => { it('should create threshold expression with proper conditions structure', () => { const result = getDefaultExpressions('B', 'C'); const thresholdQuery = result[1]; - const model = thresholdQuery.model; + const { model } = thresholdQuery; + + if (!isExpressionQuery(model)) { + throw new Error('Expected ExpressionQuery'); + } expect(model.conditions).toHaveLength(1); expect(model.conditions?.[0]).toEqual({ @@ -520,7 +542,7 @@ describe('getDefaultExpressions', () => { type: 'and', }, query: { - params: [], + params: ['C'], }, reducer: { params: [], @@ -532,7 +554,11 @@ describe('getDefaultExpressions', () => { it('should reference the reduce expression in the threshold expression', () => { const result = getDefaultExpressions('B', 'C'); const thresholdQuery = result[1]; - const model = thresholdQuery.model; + const { model } = thresholdQuery; + + if (!isExpressionQuery(model)) { + throw new Error('Expected ExpressionQuery'); + } expect(model.expression).toBe('B'); }); @@ -542,6 +568,10 @@ describe('getDefaultExpressions', () => { const reduceModel = result[0].model; const thresholdModel = result[1].model; + if (!isExpressionQuery(reduceModel) || !isExpressionQuery(thresholdModel)) { + throw new Error('Expected ExpressionQuery'); + } + expect(result[0].refId).toBe('X'); expect(reduceModel.refId).toBe('X'); expect(reduceModel.conditions?.[0].query.params).toEqual([]); diff --git a/public/app/features/alerting/unified/utils/rule-form.ts b/public/app/features/alerting/unified/utils/rule-form.ts index c0504c75b99..7d49d725bcb 100644 --- a/public/app/features/alerting/unified/utils/rule-form.ts +++ b/public/app/features/alerting/unified/utils/rule-form.ts @@ -559,7 +559,7 @@ export const getDefaultRecordingRulesQueries = ( ]; }; -const getDefaultExpressions = (...refIds: [string, string] | [string, string, string]): AlertQuery[] => { +export const getDefaultExpressions = (...refIds: [string, string] | [string, string, string]): AlertQuery[] => { const refOne = refIds[0]; const refTwo = refIds[1]; // If a third parameter is provided, use it as the source query refId, otherwise default to 'A' @@ -583,7 +583,7 @@ const getDefaultExpressions = (...refIds: [string, string] | [string, string, st type: 'and', }, query: { - params: [refOne], + params: [], }, reducer: { params: [],