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/variables/adhoc/urlParser.test.ts

192 lines
5.6 KiB

import { AdHocVariableFilter, UrlQueryValue } from '@grafana/data';
import { toFilters, toUrl } from './urlParser';
Variables: migrates ad hoc variable type to react/redux. (#22784) * Refactor: moves all the newVariables part to features/variables directory * Feature: adds datasource type * Tests: adds reducer tests * Tests: covers data source actions with tests * Chore: reduces strict null errors * boilerplate that will be replaced by real code. * added old editor template. * added initial version of ad hoc editor. * added working (apart from add) version of the editor. * Added placeholder for picker. * Have a working UI. Need to connect it so we refresh the variables on changes. * variable should be updated now. * removed console.log * made the url work. * cleaned up the adapter. * added possiblity to create filter directly from table. * moved infotext from general reducer to extended value of adhoc. * fixed strict null errors. * fixed strict null errors. * fixed issue where remove was displayed before being added. * fixed issue with fragment key. * changed so template_src is using the redux variables. * minor refactorings. * moved adhoc picker to adhoc variable. * adding tests for reducer and fixed bug. * added tests or urlparser. * added tests for ad hoc actions. * added more tests. * added more tests. * fixed strict null error. * fixed copy n pase error. * added utilit for getting new variable index. * removed console.log * added location to reducerTester type and created a module type for it. * changed so we only have one builder pattern. * fixed tests to use static expected values. * fixed strict errors. * fixed more strict errors. Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
5 years ago
describe('urlParser', () => {
describe('parsing toUrl with no filters', () => {
it('then url params should be correct', () => {
const filters: AdHocVariableFilter[] = [];
const expected: string[] = [];
expect(toUrl(filters)).toEqual(expected);
});
});
describe('parsing toUrl with filters', () => {
it('then url params should be correct', () => {
const a = createFilter('a');
const b = createFilter('b', '>');
const filters: AdHocVariableFilter[] = [a, b];
const expectedA = `${a.key}|${a.operator}|${a.value}`;
const expectedB = `${b.key}|${b.operator}|${b.value}`;
const expected: string[] = [expectedA, expectedB];
expect(toUrl(filters)).toEqual(expected);
});
});
describe('parsing toUrl with filters containing special chars', () => {
it('then url params should be correct', () => {
const a = createFilter('a|');
const b = createFilter('b', '>');
const filters: AdHocVariableFilter[] = [a, b];
const expectedA = `a__gfp__-key|${a.operator}|a__gfp__-value`;
const expectedB = `${b.key}|${b.operator}|${b.value}`;
const expected: string[] = [expectedA, expectedB];
expect(toUrl(filters)).toEqual(expected);
});
});
describe('parsing toUrl with filters without values', () => {
it('then url params should be correct', () => {
const a: AdHocVariableFilter = {
value: '',
key: 'key',
operator: '',
};
const filters: AdHocVariableFilter[] = [a];
const expectedA = `key||`;
const expected: string[] = [expectedA];
expect(toUrl(filters)).toEqual(expected);
});
});
describe('parsing toUrl with filters with undefined values', () => {
it('then url params should be correct', () => {
const a = {
value: undefined,
key: 'key',
operator: undefined,
} as unknown as AdHocVariableFilter;
const filters: AdHocVariableFilter[] = [a];
const expectedA = `key||`;
const expected: string[] = [expectedA];
expect(toUrl(filters)).toEqual(expected);
});
});
describe('parsing toUrl with filters with number values', () => {
it('then url params should be correct', () => {
const a = {
value: 1974,
key: 'key',
operator: '=',
} as unknown as AdHocVariableFilter;
const filters: AdHocVariableFilter[] = [a];
const expectedA = `key|=|1974`;
const expected: string[] = [expectedA];
expect(toUrl(filters)).toEqual(expected);
});
});
describe('parsing toUrl with filters with boolean values', () => {
it('then url params should be correct', () => {
const a = {
value: false,
key: 'key',
operator: '=',
} as unknown as AdHocVariableFilter;
const filters: AdHocVariableFilter[] = [a];
const expectedA = `key|=|false`;
const expected: string[] = [expectedA];
expect(toUrl(filters)).toEqual(expected);
});
});
Variables: migrates ad hoc variable type to react/redux. (#22784) * Refactor: moves all the newVariables part to features/variables directory * Feature: adds datasource type * Tests: adds reducer tests * Tests: covers data source actions with tests * Chore: reduces strict null errors * boilerplate that will be replaced by real code. * added old editor template. * added initial version of ad hoc editor. * added working (apart from add) version of the editor. * Added placeholder for picker. * Have a working UI. Need to connect it so we refresh the variables on changes. * variable should be updated now. * removed console.log * made the url work. * cleaned up the adapter. * added possiblity to create filter directly from table. * moved infotext from general reducer to extended value of adhoc. * fixed strict null errors. * fixed strict null errors. * fixed issue where remove was displayed before being added. * fixed issue with fragment key. * changed so template_src is using the redux variables. * minor refactorings. * moved adhoc picker to adhoc variable. * adding tests for reducer and fixed bug. * added tests or urlparser. * added tests for ad hoc actions. * added more tests. * added more tests. * fixed strict null error. * fixed copy n pase error. * added utilit for getting new variable index. * removed console.log * added location to reducerTester type and created a module type for it. * changed so we only have one builder pattern. * fixed tests to use static expected values. * fixed strict errors. * fixed more strict errors. Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
5 years ago
describe('parsing toFilters with url containing no filters as string', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = '';
const expected: AdHocVariableFilter[] = [];
expect(toFilters(url)).toEqual(expected);
});
});
describe('parsing toFilters with url containing no filters as []', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = [];
const expected: AdHocVariableFilter[] = [];
expect(toFilters(url)).toEqual(expected);
});
});
describe('parsing toFilters with url containing one filter as string', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = 'a-key|=|a-value';
const a = createFilter('a', '=');
const expected: AdHocVariableFilter[] = [a];
expect(toFilters(url)).toEqual(expected);
});
});
describe('parsing toFilters with url containing filters', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = ['a-key|=|a-value', 'b-key|>|b-value'];
const a = createFilter('a', '=');
const b = createFilter('b', '>');
const expected: AdHocVariableFilter[] = [a, b];
expect(toFilters(url)).toEqual(expected);
});
});
describe('parsing toFilters with url containing special chars', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = ['a__gfp__-key|=|a__gfp__-value', 'b-key|>|b-value'];
const a = createFilter('a|', '=');
const b = createFilter('b', '>');
const expected: AdHocVariableFilter[] = [a, b];
expect(toFilters(url)).toEqual(expected);
});
});
describe('parsing toFilters with url containing filter with empty values', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = 'key||';
const expected: AdHocVariableFilter[] = [
{
value: '',
key: 'key',
operator: '',
},
];
expect(toFilters(url)).toEqual(expected);
});
});
describe('parsing toFilters with url containing no filters as string', () => {
it('then url params should be correct', () => {
const url: UrlQueryValue = '';
const expected: AdHocVariableFilter[] = [];
expect(toFilters(url)).toEqual(expected);
});
});
Variables: migrates ad hoc variable type to react/redux. (#22784) * Refactor: moves all the newVariables part to features/variables directory * Feature: adds datasource type * Tests: adds reducer tests * Tests: covers data source actions with tests * Chore: reduces strict null errors * boilerplate that will be replaced by real code. * added old editor template. * added initial version of ad hoc editor. * added working (apart from add) version of the editor. * Added placeholder for picker. * Have a working UI. Need to connect it so we refresh the variables on changes. * variable should be updated now. * removed console.log * made the url work. * cleaned up the adapter. * added possiblity to create filter directly from table. * moved infotext from general reducer to extended value of adhoc. * fixed strict null errors. * fixed strict null errors. * fixed issue where remove was displayed before being added. * fixed issue with fragment key. * changed so template_src is using the redux variables. * minor refactorings. * moved adhoc picker to adhoc variable. * adding tests for reducer and fixed bug. * added tests or urlparser. * added tests for ad hoc actions. * added more tests. * added more tests. * fixed strict null error. * fixed copy n pase error. * added utilit for getting new variable index. * removed console.log * added location to reducerTester type and created a module type for it. * changed so we only have one builder pattern. * fixed tests to use static expected values. * fixed strict errors. * fixed more strict errors. Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
5 years ago
});
function createFilter(value: string, operator = '='): AdHocVariableFilter {
return {
value: `${value}-value`,
key: `${value}-key`,
operator: operator,
};
}