value_select_dropdown.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'app/core/directives/value_select_dropdown';
  2. import { ValueSelectDropdownCtrl } from '../directives/value_select_dropdown';
  3. // @ts-ignore
  4. import q from 'q';
  5. describe('SelectDropdownCtrl', () => {
  6. const tagValuesMap: any = {};
  7. ValueSelectDropdownCtrl.prototype.onUpdated = jest.fn();
  8. let ctrl: ValueSelectDropdownCtrl;
  9. describe('Given simple variable', () => {
  10. beforeEach(() => {
  11. ctrl = new ValueSelectDropdownCtrl(q);
  12. ctrl.variable = {
  13. current: { text: 'hej', value: 'hej' },
  14. getValuesForTag: (key: string) => {
  15. return Promise.resolve(tagValuesMap[key]);
  16. },
  17. };
  18. ctrl.init();
  19. });
  20. it('Should init labelText and linkText', () => {
  21. expect(ctrl.linkText).toBe('hej');
  22. });
  23. });
  24. describe('Given variable with tags and dropdown is opened', () => {
  25. beforeEach(() => {
  26. ctrl = new ValueSelectDropdownCtrl(q);
  27. ctrl.variable = {
  28. current: { text: 'server-1', value: 'server-1' },
  29. options: [
  30. { text: 'server-1', value: 'server-1', selected: true },
  31. { text: 'server-2', value: 'server-2' },
  32. { text: 'server-3', value: 'server-3' },
  33. ],
  34. tags: ['key1', 'key2', 'key3'],
  35. getValuesForTag: (key: string) => {
  36. return Promise.resolve(tagValuesMap[key]);
  37. },
  38. multi: true,
  39. };
  40. tagValuesMap.key1 = ['server-1', 'server-3'];
  41. tagValuesMap.key2 = ['server-2', 'server-3'];
  42. tagValuesMap.key3 = ['server-1', 'server-2', 'server-3'];
  43. ctrl.init();
  44. ctrl.show();
  45. });
  46. it('should init tags model', () => {
  47. expect(ctrl.tags.length).toBe(3);
  48. expect(ctrl.tags[0].text).toBe('key1');
  49. });
  50. it('should init options model', () => {
  51. expect(ctrl.options.length).toBe(3);
  52. });
  53. it('should init selected values array', () => {
  54. expect(ctrl.selectedValues.length).toBe(1);
  55. });
  56. it('should set linkText', () => {
  57. expect(ctrl.linkText).toBe('server-1');
  58. });
  59. describe('after adititional value is selected', () => {
  60. beforeEach(() => {
  61. ctrl.selectValue(ctrl.options[2], {});
  62. ctrl.commitChanges();
  63. });
  64. it('should update link text', () => {
  65. expect(ctrl.linkText).toBe('server-1 + server-3');
  66. });
  67. });
  68. describe('When tag is selected', () => {
  69. beforeEach(async () => {
  70. await ctrl.selectTag(ctrl.tags[0]);
  71. ctrl.commitChanges();
  72. });
  73. it('should select tag', () => {
  74. expect(ctrl.selectedTags.length).toBe(1);
  75. });
  76. it('should select values', () => {
  77. expect(ctrl.options[0].selected).toBe(true);
  78. expect(ctrl.options[2].selected).toBe(true);
  79. });
  80. it('link text should not include tag values', () => {
  81. expect(ctrl.linkText).toBe('');
  82. });
  83. describe('and then dropdown is opened and closed without changes', () => {
  84. beforeEach(() => {
  85. ctrl.show();
  86. ctrl.commitChanges();
  87. });
  88. it('should still have selected tag', () => {
  89. expect(ctrl.selectedTags.length).toBe(1);
  90. });
  91. });
  92. describe('and then unselected', () => {
  93. beforeEach(async () => {
  94. await ctrl.selectTag(ctrl.tags[0]);
  95. });
  96. it('should deselect tag', () => {
  97. expect(ctrl.selectedTags.length).toBe(0);
  98. });
  99. });
  100. describe('and then value is unselected', () => {
  101. beforeEach(() => {
  102. ctrl.selectValue(ctrl.options[0], {});
  103. });
  104. it('should deselect tag', () => {
  105. expect(ctrl.selectedTags.length).toBe(0);
  106. });
  107. });
  108. });
  109. });
  110. describe('Given variable with selected tags', () => {
  111. beforeEach(() => {
  112. ctrl = new ValueSelectDropdownCtrl(q);
  113. ctrl.variable = {
  114. current: {
  115. text: 'server-1',
  116. value: 'server-1',
  117. tags: [{ text: 'key1', selected: true }],
  118. },
  119. options: [
  120. { text: 'server-1', value: 'server-1' },
  121. { text: 'server-2', value: 'server-2' },
  122. { text: 'server-3', value: 'server-3' },
  123. ],
  124. tags: ['key1', 'key2', 'key3'],
  125. getValuesForTag: (key: any) => {
  126. return Promise.resolve(tagValuesMap[key]);
  127. },
  128. multi: true,
  129. };
  130. ctrl.init();
  131. ctrl.show();
  132. });
  133. it('should set tag as selected', () => {
  134. expect(ctrl.tags[0].selected).toBe(true);
  135. });
  136. });
  137. });