utils.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { UrlQueryMap } from '@grafana/data';
  2. import { VariableRefresh } from './types';
  3. import {
  4. containsVariable,
  5. ensureStringValues,
  6. findTemplateVarChanges,
  7. getCurrentText,
  8. getVariableRefresh,
  9. isAllVariable,
  10. } from './utils';
  11. describe('isAllVariable', () => {
  12. it.each`
  13. variable | expected
  14. ${null} | ${false}
  15. ${undefined} | ${false}
  16. ${{}} | ${false}
  17. ${{ current: {} }} | ${false}
  18. ${{ current: { text: '' } }} | ${false}
  19. ${{ current: { text: null } }} | ${false}
  20. ${{ current: { text: undefined } }} | ${false}
  21. ${{ current: { text: 'Alll' } }} | ${false}
  22. ${{ current: { text: 'All' } }} | ${true}
  23. ${{ current: { text: [] } }} | ${false}
  24. ${{ current: { text: [null] } }} | ${false}
  25. ${{ current: { text: [undefined] } }} | ${false}
  26. ${{ current: { text: ['Alll'] } }} | ${false}
  27. ${{ current: { text: ['Alll', 'All'] } }} | ${false}
  28. ${{ current: { text: ['All'] } }} | ${true}
  29. ${{ current: { text: ['All', 'Alll'] } }} | ${true}
  30. ${{ current: { text: { prop1: 'test' } } }} | ${false}
  31. ${{ current: { value: '' } }} | ${false}
  32. ${{ current: { value: null } }} | ${false}
  33. ${{ current: { value: undefined } }} | ${false}
  34. ${{ current: { value: '$__alll' } }} | ${false}
  35. ${{ current: { value: '$__all' } }} | ${true}
  36. ${{ current: { value: [] } }} | ${false}
  37. ${{ current: { value: [null] } }} | ${false}
  38. ${{ current: { value: [undefined] } }} | ${false}
  39. ${{ current: { value: ['$__alll'] } }} | ${false}
  40. ${{ current: { value: ['$__alll', '$__all'] } }} | ${false}
  41. ${{ current: { value: ['$__all'] } }} | ${true}
  42. ${{ current: { value: ['$__all', '$__alll'] } }} | ${true}
  43. ${{ current: { value: { prop1: 'test' } } }} | ${false}
  44. ${{ current: { value: '', text: '' } }} | ${false}
  45. ${{ current: { value: '', text: 'All' } }} | ${true}
  46. ${{ current: { value: '$__all', text: '' } }} | ${true}
  47. ${{ current: { value: '', text: ['All'] } }} | ${true}
  48. ${{ current: { value: ['$__all'], text: '' } }} | ${true}
  49. `("when called with params: 'variable': '$variable' then result should be '$expected'", ({ variable, expected }) => {
  50. expect(isAllVariable(variable)).toEqual(expected);
  51. });
  52. });
  53. describe('getCurrentText', () => {
  54. it.each`
  55. variable | expected
  56. ${null} | ${''}
  57. ${undefined} | ${''}
  58. ${{}} | ${''}
  59. ${{ current: {} }} | ${''}
  60. ${{ current: { text: '' } }} | ${''}
  61. ${{ current: { text: null } }} | ${''}
  62. ${{ current: { text: undefined } }} | ${''}
  63. ${{ current: { text: 'A' } }} | ${'A'}
  64. ${{ current: { text: 'All' } }} | ${'All'}
  65. ${{ current: { text: [] } }} | ${''}
  66. ${{ current: { text: [null] } }} | ${''}
  67. ${{ current: { text: [undefined] } }} | ${''}
  68. ${{ current: { text: ['A'] } }} | ${'A'}
  69. ${{ current: { text: ['A', 'All'] } }} | ${'A,All'}
  70. ${{ current: { text: ['All'] } }} | ${'All'}
  71. ${{ current: { text: { prop1: 'test' } } }} | ${''}
  72. `("when called with params: 'variable': '$variable' then result should be '$expected'", ({ variable, expected }) => {
  73. expect(getCurrentText(variable)).toEqual(expected);
  74. });
  75. });
  76. describe('getVariableRefresh', () => {
  77. it.each`
  78. variable | expected
  79. ${null} | ${VariableRefresh.never}
  80. ${undefined} | ${VariableRefresh.never}
  81. ${{}} | ${VariableRefresh.never}
  82. ${{ refresh: VariableRefresh.never }} | ${VariableRefresh.never}
  83. ${{ refresh: VariableRefresh.onTimeRangeChanged }} | ${VariableRefresh.onTimeRangeChanged}
  84. ${{ refresh: VariableRefresh.onDashboardLoad }} | ${VariableRefresh.onDashboardLoad}
  85. ${{ refresh: 'invalid' }} | ${VariableRefresh.never}
  86. `("when called with params: 'variable': '$variable' then result should be '$expected'", ({ variable, expected }) => {
  87. expect(getVariableRefresh(variable)).toEqual(expected);
  88. });
  89. });
  90. describe('findTemplateVarChanges', () => {
  91. it('detect adding/removing a variable', () => {
  92. const a: UrlQueryMap = {};
  93. const b: UrlQueryMap = {
  94. 'var-xyz': 'hello',
  95. aaa: 'ignore me',
  96. };
  97. expect(findTemplateVarChanges(b, a)).toEqual({ 'var-xyz': { value: 'hello' } });
  98. expect(findTemplateVarChanges(a, b)).toEqual({ 'var-xyz': { value: '', removed: true } });
  99. });
  100. it('then should ignore equal values', () => {
  101. const a: UrlQueryMap = {
  102. 'var-xyz': 'hello',
  103. bbb: 'ignore me',
  104. };
  105. const b: UrlQueryMap = {
  106. 'var-xyz': 'hello',
  107. aaa: 'ignore me',
  108. };
  109. expect(findTemplateVarChanges(b, a)).toBeUndefined();
  110. expect(findTemplateVarChanges(a, b)).toBeUndefined();
  111. });
  112. it('then should ignore equal values with empty values', () => {
  113. const a: UrlQueryMap = {
  114. 'var-xyz': '',
  115. bbb: 'ignore me',
  116. };
  117. const b: UrlQueryMap = {
  118. 'var-xyz': '',
  119. aaa: 'ignore me',
  120. };
  121. expect(findTemplateVarChanges(b, a)).toBeUndefined();
  122. expect(findTemplateVarChanges(a, b)).toBeUndefined();
  123. });
  124. it('then should ignore empty array values', () => {
  125. const a: UrlQueryMap = {
  126. 'var-adhoc': [],
  127. };
  128. const b: UrlQueryMap = {};
  129. expect(findTemplateVarChanges(b, a)).toBeUndefined();
  130. expect(findTemplateVarChanges(a, b)).toBeUndefined();
  131. });
  132. it('Should handle array values with one value same as just value', () => {
  133. const a: UrlQueryMap = {
  134. 'var-test': ['test'],
  135. };
  136. const b: UrlQueryMap = {
  137. 'var-test': 'test',
  138. };
  139. expect(findTemplateVarChanges(b, a)).toBeUndefined();
  140. expect(findTemplateVarChanges(a, b)).toBeUndefined();
  141. });
  142. it('Should detect change in array value and return array with single value', () => {
  143. const a: UrlQueryMap = {
  144. 'var-test': ['test'],
  145. };
  146. const b: UrlQueryMap = {
  147. 'var-test': 'asd',
  148. };
  149. expect(findTemplateVarChanges(a, b)!['var-test']).toEqual({ value: ['test'] });
  150. });
  151. });
  152. describe('ensureStringValues', () => {
  153. it.each`
  154. value | expected
  155. ${null} | ${''}
  156. ${undefined} | ${''}
  157. ${{}} | ${''}
  158. ${{ current: {} }} | ${''}
  159. ${1} | ${'1'}
  160. ${[1, 2]} | ${['1', '2']}
  161. ${'1'} | ${'1'}
  162. ${['1', '2']} | ${['1', '2']}
  163. ${true} | ${'true'}
  164. `('when called with value:$value then result should be:$expected', ({ value, expected }) => {
  165. expect(ensureStringValues(value)).toEqual(expected);
  166. });
  167. });
  168. describe('containsVariable', () => {
  169. it.each`
  170. value | expected
  171. ${''} | ${false}
  172. ${'$var'} | ${true}
  173. ${{ thing1: '${var}' }} | ${true}
  174. ${{ thing1: '${var:fmt}' }} | ${true}
  175. ${{ thing1: ['1', '${var}'] }} | ${true}
  176. ${{ thing1: ['1', '[[var]]'] }} | ${true}
  177. ${{ thing1: ['1', '[[var:fmt]]'] }} | ${true}
  178. ${{ thing1: { thing2: '${var}' } }} | ${true}
  179. ${{ params: [['param', '$var']] }} | ${true}
  180. ${{ params: [['param', '${var}']] }} | ${true}
  181. `('when called with value:$value then result should be:$expected', ({ value, expected }) => {
  182. expect(containsVariable(value, 'var')).toEqual(expected);
  183. });
  184. });