urlParser.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { UrlQueryValue } from '@grafana/data';
  2. import { AdHocVariableFilter } from 'app/features/variables/types';
  3. import { toFilters, toUrl } from './urlParser';
  4. describe('urlParser', () => {
  5. describe('parsing toUrl with no filters', () => {
  6. it('then url params should be correct', () => {
  7. const filters: AdHocVariableFilter[] = [];
  8. const expected: string[] = [];
  9. expect(toUrl(filters)).toEqual(expected);
  10. });
  11. });
  12. describe('parsing toUrl with filters', () => {
  13. it('then url params should be correct', () => {
  14. const a = createFilter('a');
  15. const b = createFilter('b', '>');
  16. const filters: AdHocVariableFilter[] = [a, b];
  17. const expectedA = `${a.key}|${a.operator}|${a.value}`;
  18. const expectedB = `${b.key}|${b.operator}|${b.value}`;
  19. const expected: string[] = [expectedA, expectedB];
  20. expect(toUrl(filters)).toEqual(expected);
  21. });
  22. });
  23. describe('parsing toUrl with filters containing special chars', () => {
  24. it('then url params should be correct', () => {
  25. const a = createFilter('a|');
  26. const b = createFilter('b', '>');
  27. const filters: AdHocVariableFilter[] = [a, b];
  28. const expectedA = `a__gfp__-key|${a.operator}|a__gfp__-value`;
  29. const expectedB = `${b.key}|${b.operator}|${b.value}`;
  30. const expected: string[] = [expectedA, expectedB];
  31. expect(toUrl(filters)).toEqual(expected);
  32. });
  33. });
  34. describe('parsing toUrl with filters without values', () => {
  35. it('then url params should be correct', () => {
  36. const a: AdHocVariableFilter = {
  37. value: '',
  38. key: 'key',
  39. operator: '',
  40. condition: '',
  41. };
  42. const filters: AdHocVariableFilter[] = [a];
  43. const expectedA = `key||`;
  44. const expected: string[] = [expectedA];
  45. expect(toUrl(filters)).toEqual(expected);
  46. });
  47. });
  48. describe('parsing toUrl with filters with undefined values', () => {
  49. it('then url params should be correct', () => {
  50. const a = {
  51. value: undefined,
  52. key: 'key',
  53. operator: undefined,
  54. condition: '',
  55. } as unknown as AdHocVariableFilter;
  56. const filters: AdHocVariableFilter[] = [a];
  57. const expectedA = `key||`;
  58. const expected: string[] = [expectedA];
  59. expect(toUrl(filters)).toEqual(expected);
  60. });
  61. });
  62. describe('parsing toUrl with filters with number values', () => {
  63. it('then url params should be correct', () => {
  64. const a = {
  65. value: 1974,
  66. key: 'key',
  67. operator: '=',
  68. condition: '',
  69. } as unknown as AdHocVariableFilter;
  70. const filters: AdHocVariableFilter[] = [a];
  71. const expectedA = `key|=|1974`;
  72. const expected: string[] = [expectedA];
  73. expect(toUrl(filters)).toEqual(expected);
  74. });
  75. });
  76. describe('parsing toUrl with filters with boolean values', () => {
  77. it('then url params should be correct', () => {
  78. const a = {
  79. value: false,
  80. key: 'key',
  81. operator: '=',
  82. condition: '',
  83. } as unknown as AdHocVariableFilter;
  84. const filters: AdHocVariableFilter[] = [a];
  85. const expectedA = `key|=|false`;
  86. const expected: string[] = [expectedA];
  87. expect(toUrl(filters)).toEqual(expected);
  88. });
  89. });
  90. describe('parsing toFilters with url containing no filters as string', () => {
  91. it('then url params should be correct', () => {
  92. const url: UrlQueryValue = '';
  93. const expected: AdHocVariableFilter[] = [];
  94. expect(toFilters(url)).toEqual(expected);
  95. });
  96. });
  97. describe('parsing toFilters with url containing no filters as []', () => {
  98. it('then url params should be correct', () => {
  99. const url: UrlQueryValue = [];
  100. const expected: AdHocVariableFilter[] = [];
  101. expect(toFilters(url)).toEqual(expected);
  102. });
  103. });
  104. describe('parsing toFilters with url containing one filter as string', () => {
  105. it('then url params should be correct', () => {
  106. const url: UrlQueryValue = 'a-key|=|a-value';
  107. const a = createFilter('a', '=');
  108. const expected: AdHocVariableFilter[] = [a];
  109. expect(toFilters(url)).toEqual(expected);
  110. });
  111. });
  112. describe('parsing toFilters with url containing filters', () => {
  113. it('then url params should be correct', () => {
  114. const url: UrlQueryValue = ['a-key|=|a-value', 'b-key|>|b-value'];
  115. const a = createFilter('a', '=');
  116. const b = createFilter('b', '>');
  117. const expected: AdHocVariableFilter[] = [a, b];
  118. expect(toFilters(url)).toEqual(expected);
  119. });
  120. });
  121. describe('parsing toFilters with url containing special chars', () => {
  122. it('then url params should be correct', () => {
  123. const url: UrlQueryValue = ['a__gfp__-key|=|a__gfp__-value', 'b-key|>|b-value'];
  124. const a = createFilter('a|', '=');
  125. const b = createFilter('b', '>');
  126. const expected: AdHocVariableFilter[] = [a, b];
  127. expect(toFilters(url)).toEqual(expected);
  128. });
  129. });
  130. describe('parsing toFilters with url containing filter with empty values', () => {
  131. it('then url params should be correct', () => {
  132. const url: UrlQueryValue = 'key||';
  133. const expected: AdHocVariableFilter[] = [
  134. {
  135. value: '',
  136. key: 'key',
  137. operator: '',
  138. condition: '',
  139. },
  140. ];
  141. expect(toFilters(url)).toEqual(expected);
  142. });
  143. });
  144. describe('parsing toFilters with url containing no filters as string', () => {
  145. it('then url params should be correct', () => {
  146. const url: UrlQueryValue = '';
  147. const expected: AdHocVariableFilter[] = [];
  148. expect(toFilters(url)).toEqual(expected);
  149. });
  150. });
  151. });
  152. function createFilter(value: string, operator = '='): AdHocVariableFilter {
  153. return {
  154. value: `${value}-value`,
  155. key: `${value}-key`,
  156. operator: operator,
  157. condition: '',
  158. };
  159. }