value_select_dropdown_specs.js 4.9 KB

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