valueSelectDropdown-specs.js 4.6 KB

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