grafana/public/app/features/templating/macroRegistry.test.ts
Torkel Ödegaard b7b608418d
Templating: Introduce macros to simplify and optimize some scopedVars (#65317)
* Templating: Introduce macros to simplify and optimize some scopedVars

* Fixing tests

* fix test

* minor fix

* refactoring so macros work with formatting

* remove breaking change and keep current inconsistency

* Rename valueIndex to rowIndex

* Minor fixes

* Added test dashboard

* Added tags to dashboard

* Update

* Added test to check it returns match

* Update

* Fixed dashboard

* fix
2023-03-28 19:22:34 +02:00

55 lines
1.6 KiB
TypeScript

import { initTemplateSrv } from 'test/helpers/initTemplateSrv';
import { DataLinkBuiltInVars } from '@grafana/data';
import { getTemplateSrv, setTemplateSrv } from '@grafana/runtime';
import { setTimeSrv } from '../dashboard/services/TimeSrv';
import { variableAdapters } from '../variables/adapters';
import { createQueryVariableAdapter } from '../variables/query/adapter';
describe('__all_variables', () => {
beforeAll(() => {
variableAdapters.register(createQueryVariableAdapter());
setTemplateSrv(
initTemplateSrv('hello', [
{
type: 'query',
name: 'test',
rootStateKey: 'hello',
current: { value: ['val1', 'val2'] },
getValueForUrl: function () {
return this.current.value;
},
},
])
);
});
it('should interpolate correctly', () => {
const out = getTemplateSrv().replace(`/d/1?$${DataLinkBuiltInVars.includeVars}`);
expect(out).toBe('/d/1?var-test=val1&var-test=val2');
});
it('should interpolate and take scopedVars into account', () => {
const out = getTemplateSrv().replace(`/d/1?$${DataLinkBuiltInVars.includeVars}`, { test: { value: 'val3' } });
expect(out).toBe('/d/1?var-test=val3');
});
});
describe('__url_time_range', () => {
beforeAll(() => {
setTimeSrv({
timeRangeForUrl: () => ({
from: 1607687293000,
to: 1607687293100,
}),
} as any);
});
it('should interpolate to url params', () => {
const out = getTemplateSrv().replace(`/d/1?$${DataLinkBuiltInVars.keepTime}`);
expect(out).toBe('/d/1?from=1607687293000&to=1607687293100');
});
});