Add tests for matcher to string method

This commit is contained in:
Konrad Lalik
2022-02-22 13:51:42 +01:00
parent 3cc598f65b
commit 4ade488494
2 changed files with 23 additions and 2 deletions
@@ -1,6 +1,12 @@
import { Matcher, MatcherOperator, Route } from 'app/plugins/datasource/alertmanager/types';
import { Labels } from 'app/types/unified-alerting-dto';
import { parseMatcher, parseMatchers, labelsMatchMatchers, removeMuteTimingFromRoute } from './alertmanager';
import {
parseMatcher,
parseMatchers,
labelsMatchMatchers,
removeMuteTimingFromRoute,
matchersToString,
} from './alertmanager';
describe('Alertmanager utils', () => {
describe('parseMatcher', () => {
@@ -162,4 +168,19 @@ describe('Alertmanager utils', () => {
});
});
});
describe('matchersToString', () => {
it('Should create a comma-separated list of labels and values wrapped into curly brackets', () => {
const matchers: Matcher[] = [
{ name: 'severity', value: 'critical', isEqual: true, isRegex: false },
{ name: 'resource', value: 'cpu', isEqual: true, isRegex: true },
{ name: 'rule_uid', value: '2Otf8canzz', isEqual: false, isRegex: false },
{ name: 'cluster', value: 'prom', isEqual: false, isRegex: true },
];
const matchersString = matchersToString(matchers);
expect(matchersString).toBe('{severity="critical",resource=~"cpu",rule_uid!="2Otf8canzz",cluster!~"prom"}');
});
});
});
@@ -109,7 +109,7 @@ export function matchersToString(matchers: Matcher[]) {
const combinedMatchers = matcherFields.reduce((acc, current) => {
const currentMatcherString = `${current.name}${current.operator}"${current.value}"`;
return acc ? `${acc},${currentMatcherString}"` : currentMatcherString;
return acc ? `${acc},${currentMatcherString}` : currentMatcherString;
}, '');
return `{${combinedMatchers}}`;