value_select_dropdown_specs.js 4.6 KB

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