common.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { initialCustomVariableModelState } from 'app/features/variables/custom/reducer';
  2. import { hasOption, interpolateVariable } from './common';
  3. describe('AzureMonitor: hasOption', () => {
  4. it('can find an option in flat array', () => {
  5. const options = [
  6. { value: 'a', label: 'a' },
  7. { value: 'b', label: 'b' },
  8. { value: 'c', label: 'c' },
  9. ];
  10. expect(hasOption(options, 'b')).toBeTruthy();
  11. });
  12. it('can not find an option in flat array', () => {
  13. const options = [
  14. { value: 'a', label: 'a' },
  15. { value: 'b', label: 'b' },
  16. { value: 'c', label: 'c' },
  17. ];
  18. expect(hasOption(options, 'not-there')).not.toBeTruthy();
  19. });
  20. it('can find an option in a nested group', () => {
  21. const options = [
  22. { value: 'a', label: 'a' },
  23. { value: 'b', label: 'b' },
  24. {
  25. label: 'c',
  26. value: 'c',
  27. options: [
  28. { value: 'c-a', label: 'c-a' },
  29. { value: 'c-b', label: 'c-b' },
  30. { value: 'c-c', label: 'c-c' },
  31. ],
  32. },
  33. { value: 'd', label: 'd' },
  34. ];
  35. expect(hasOption(options, 'c-b')).toBeTruthy();
  36. });
  37. });
  38. describe('When interpolating variables', () => {
  39. describe('and value is a string', () => {
  40. it('should return an unquoted value', () => {
  41. expect(interpolateVariable('abc', initialCustomVariableModelState)).toEqual('abc');
  42. });
  43. });
  44. describe('and value is a number', () => {
  45. it('should return an unquoted value', () => {
  46. expect(interpolateVariable(1000, initialCustomVariableModelState)).toEqual(1000);
  47. });
  48. });
  49. describe('and value is an array of strings', () => {
  50. it('should return comma separated quoted values', () => {
  51. expect(interpolateVariable(['a', 'b', 'c'], initialCustomVariableModelState)).toEqual("'a','b','c'");
  52. });
  53. });
  54. describe('and variable allows multi-value and value is a string', () => {
  55. it('should return a quoted value', () => {
  56. const variable = { ...initialCustomVariableModelState, multi: true };
  57. expect(interpolateVariable('abc', variable)).toEqual("'abc'");
  58. });
  59. });
  60. describe('and variable contains single quote', () => {
  61. it('should return a quoted value', () => {
  62. const variable = { ...initialCustomVariableModelState, multi: true };
  63. expect(interpolateVariable("a'bc", variable)).toEqual("'a'bc'");
  64. });
  65. });
  66. describe('and variable allows all and value is a string', () => {
  67. it('should return a quoted value', () => {
  68. const variable = { ...initialCustomVariableModelState, includeAll: true };
  69. expect(interpolateVariable('abc', variable)).toEqual("'abc'");
  70. });
  71. it('should not return a quoted value if the all value is modified', () => {
  72. const variable = { ...initialCustomVariableModelState, includeAll: true, allValue: 'All' };
  73. expect(interpolateVariable('abc', variable)).toEqual('abc');
  74. });
  75. it('should return a quoted value if multi is selected even if the allValue is set', () => {
  76. const variable = { ...initialCustomVariableModelState, includeAll: true, multi: true, allValue: 'All' };
  77. expect(interpolateVariable('abc', variable)).toEqual("'abc'");
  78. });
  79. });
  80. });