value_select_dropdown.test.ts 4.3 KB

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