The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/alerting/unified/utils/templates.test.ts

36 lines
1.6 KiB

import { updateDefinesWithUniqueValue } from './templates';
jest.mock('lodash', () => ({
...jest.requireActual('lodash'),
now: jest.fn().mockImplementation(() => 99),
}));
describe('updateDefinesWithUniqueValue method', () => {
describe('only onw define', () => {
it('Should update the define values with a unique new one', () => {
expect(updateDefinesWithUniqueValue(`{{ define "t" }}\n{{.Alerts.Firing}}\n{{ end }}`)).toEqual(
`{{ define "t_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}`
);
});
});
describe('more than one define in the template', () => {
it('Should update the define values with a unique new one ', () => {
expect(
updateDefinesWithUniqueValue(
`{{ define "t1" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t2" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t3" }}\n{{.Alerts.Firing}}\n{{ end }}\n`
)
).toEqual(
`{{ define "t1_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t2_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t3_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}\n`
);
});
it('Should update the define values with a unique new one, special chars included in the value', () => {
expect(
updateDefinesWithUniqueValue(
`{{ define "t1 /^*;$@" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t2" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t3" }}\n{{.Alerts.Firing}}\n{{ end }}\n`
)
).toEqual(
`{{ define "t1 /^*;$@_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t2_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}\n{{ define "t3_NEW_99" }}\n{{.Alerts.Firing}}\n{{ end }}\n`
);
});
});
});