getAllVariableValuesForUrl.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { setTemplateSrv } from '@grafana/runtime';
  2. import { initTemplateSrv } from '../../../test/helpers/initTemplateSrv';
  3. import { variableAdapters } from './adapters';
  4. import { getVariablesUrlParams } from './getAllVariableValuesForUrl';
  5. import { createQueryVariableAdapter } from './query/adapter';
  6. const key = 'key';
  7. describe('getAllVariableValuesForUrl', () => {
  8. beforeAll(() => {
  9. variableAdapters.register(createQueryVariableAdapter());
  10. });
  11. describe('with multi value', () => {
  12. beforeEach(() => {
  13. setTemplateSrv(
  14. initTemplateSrv(key, [
  15. {
  16. type: 'query',
  17. name: 'test',
  18. rootStateKey: key,
  19. current: { value: ['val1', 'val2'] },
  20. getValueForUrl: function () {
  21. return this.current.value;
  22. },
  23. },
  24. ])
  25. );
  26. });
  27. it('should set multiple url params', () => {
  28. let params: any = getVariablesUrlParams();
  29. expect(params['var-test']).toMatchObject(['val1', 'val2']);
  30. });
  31. });
  32. describe('skip url sync', () => {
  33. beforeEach(() => {
  34. setTemplateSrv(
  35. initTemplateSrv(key, [
  36. {
  37. name: 'test',
  38. rootStateKey: key,
  39. skipUrlSync: true,
  40. current: { value: 'value' },
  41. getValueForUrl: function () {
  42. return this.current.value;
  43. },
  44. },
  45. ])
  46. );
  47. });
  48. it('should not include template variable value in url', () => {
  49. const params = getVariablesUrlParams();
  50. expect(params['var-test']).toBe(undefined);
  51. });
  52. });
  53. describe('with multi value with skip url sync', () => {
  54. beforeEach(() => {
  55. setTemplateSrv(
  56. initTemplateSrv(key, [
  57. {
  58. type: 'query',
  59. name: 'test',
  60. rootStateKey: key,
  61. skipUrlSync: true,
  62. current: { value: ['val1', 'val2'] },
  63. getValueForUrl: function () {
  64. return this.current.value;
  65. },
  66. },
  67. ])
  68. );
  69. });
  70. it('should not include template variable value in url', () => {
  71. const params = getVariablesUrlParams();
  72. expect(params['var-test']).toBe(undefined);
  73. });
  74. });
  75. describe('fillVariableValuesForUrl with multi value and scopedVars', () => {
  76. beforeEach(() => {
  77. setTemplateSrv(
  78. initTemplateSrv(key, [{ type: 'query', name: 'test', rootStateKey: key, current: { value: ['val1', 'val2'] } }])
  79. );
  80. });
  81. it('should set scoped value as url params', () => {
  82. const params = getVariablesUrlParams({
  83. test: { value: 'val1', text: 'val1text' },
  84. });
  85. expect(params['var-test']).toBe('val1');
  86. });
  87. });
  88. describe('fillVariableValuesForUrl with multi value, scopedVars and skip url sync', () => {
  89. beforeEach(() => {
  90. setTemplateSrv(
  91. initTemplateSrv(key, [{ type: 'query', name: 'test', rootStateKey: key, current: { value: ['val1', 'val2'] } }])
  92. );
  93. });
  94. it('should not set scoped value as url params', () => {
  95. const params = getVariablesUrlParams({
  96. test: { name: 'test', value: 'val1', text: 'val1text', skipUrlSync: true },
  97. });
  98. expect(params['var-test']).toBe(undefined);
  99. });
  100. });
  101. });