history_ctrl_specs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import _ from 'lodash';
  3. import {HistoryListCtrl} from 'app/features/dashboard/history/history';
  4. import { versions, compare, restore } from 'test/mocks/history-mocks';
  5. import config from 'app/core/config';
  6. describe('HistoryListCtrl', function() {
  7. var RESTORE_ID = 4;
  8. var ctx: any = {};
  9. var versionsResponse: any = versions();
  10. var restoreResponse: any = restore(7, RESTORE_ID);
  11. beforeEach(angularMocks.module('grafana.core'));
  12. beforeEach(angularMocks.module('grafana.services'));
  13. beforeEach(angularMocks.inject($rootScope => {
  14. ctx.scope = $rootScope.$new();
  15. }));
  16. var historySrv;
  17. var $rootScope;
  18. beforeEach(function() {
  19. historySrv = {
  20. getHistoryList: sinon.stub(),
  21. compareVersions: sinon.stub(),
  22. restoreDashboard: sinon.stub(),
  23. };
  24. $rootScope = {
  25. appEvent: sinon.spy(),
  26. onAppEvent: sinon.spy(),
  27. };
  28. });
  29. describe('when the history list component is loaded', function() {
  30. var deferred;
  31. beforeEach(angularMocks.inject(($controller, $q) => {
  32. deferred = $q.defer();
  33. historySrv.getHistoryList.returns(deferred.promise);
  34. ctx.ctrl = $controller(HistoryListCtrl, {
  35. historySrv,
  36. $rootScope,
  37. $scope: ctx.scope,
  38. });
  39. }));
  40. it('should immediately attempt to fetch the history list', function() {
  41. expect(historySrv.getHistoryList.calledOnce).to.be(true);
  42. });
  43. describe('and the history list is successfully fetched', function() {
  44. beforeEach(function() {
  45. deferred.resolve(versionsResponse);
  46. ctx.ctrl.$scope.$apply();
  47. });
  48. it('should reset the controller\'s state', function() {
  49. expect(ctx.ctrl.mode).to.be('list');
  50. expect(ctx.ctrl.delta).to.eql({ basic: '', html: '' });
  51. expect(ctx.ctrl.selected.length).to.be(0);
  52. expect(ctx.ctrl.selected).to.eql([]);
  53. expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be.undefined;
  54. });
  55. it('should indicate loading has finished', function() {
  56. expect(ctx.ctrl.loading).to.be(false);
  57. });
  58. it('should store the revisions sorted desc by version id', function() {
  59. expect(ctx.ctrl.revisions[0].version).to.be(4);
  60. expect(ctx.ctrl.revisions[1].version).to.be(3);
  61. expect(ctx.ctrl.revisions[2].version).to.be(2);
  62. expect(ctx.ctrl.revisions[3].version).to.be(1);
  63. });
  64. it('should add a checked property to each revision', function() {
  65. var actual = _.filter(ctx.ctrl.revisions, rev => rev.hasOwnProperty('checked'));
  66. expect(actual.length).to.be(4);
  67. });
  68. it('should set all checked properties to false on reset', function() {
  69. ctx.ctrl.revisions[0].checked = true;
  70. ctx.ctrl.revisions[2].checked = true;
  71. ctx.ctrl.selected = [0, 2];
  72. ctx.ctrl.reset();
  73. var actual = _.filter(ctx.ctrl.revisions, rev => !rev.checked);
  74. expect(actual.length).to.be(4);
  75. expect(ctx.ctrl.selected).to.eql([]);
  76. });
  77. });
  78. describe('and fetching the history list fails', function() {
  79. beforeEach(function() {
  80. deferred.reject(new Error('HistoryListError'));
  81. ctx.ctrl.$scope.$apply();
  82. });
  83. it('should reset the controller\'s state', function() {
  84. expect(ctx.ctrl.mode).to.be('list');
  85. expect(ctx.ctrl.delta).to.eql({ basic: '', html: '' });
  86. expect(ctx.ctrl.selected.length).to.be(0);
  87. expect(ctx.ctrl.selected).to.eql([]);
  88. expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be.undefined;
  89. });
  90. it('should indicate loading has finished', function() {
  91. expect(ctx.ctrl.loading).to.be(false);
  92. });
  93. it('should broadcast an event indicating the failure', function() {
  94. expect($rootScope.appEvent.calledOnce).to.be(true);
  95. expect($rootScope.appEvent.calledWith('alert-error')).to.be(true);
  96. });
  97. it('should have an empty revisions list', function() {
  98. expect(ctx.ctrl.revisions).to.eql([]);
  99. });
  100. });
  101. describe('should update the history list when the dashboard is saved', function() {
  102. beforeEach(function() {
  103. ctx.ctrl.dashboard = { version: 3 };
  104. ctx.ctrl.resetFromSource = sinon.spy();
  105. });
  106. it('should listen for the `dashboard-saved` appEvent', function() {
  107. expect($rootScope.onAppEvent.calledOnce).to.be(true);
  108. expect($rootScope.onAppEvent.getCall(0).args[0]).to.be('dashboard-saved');
  109. });
  110. it('should call `onDashboardSaved` when the appEvent is received', function() {
  111. expect($rootScope.onAppEvent.getCall(0).args[1]).to.not.be(ctx.ctrl.onDashboardSaved);
  112. expect($rootScope.onAppEvent.getCall(0).args[1].toString).to.be(ctx.ctrl.onDashboardSaved.toString);
  113. });
  114. it('should emit an appEvent to hide the changelog', function() {
  115. ctx.ctrl.onDashboardSaved();
  116. expect($rootScope.appEvent.calledOnce).to.be(true);
  117. expect($rootScope.appEvent.getCall(0).args[0]).to.be('hide-dash-editor');
  118. });
  119. });
  120. });
  121. describe('when the user wants to compare two revisions', function() {
  122. var deferred;
  123. beforeEach(angularMocks.inject(($controller, $q) => {
  124. deferred = $q.defer();
  125. historySrv.getHistoryList.returns($q.when(versionsResponse));
  126. historySrv.compareVersions.returns(deferred.promise);
  127. ctx.ctrl = $controller(HistoryListCtrl, {
  128. historySrv,
  129. $rootScope,
  130. $scope: ctx.scope,
  131. });
  132. ctx.ctrl.$scope.onDashboardSaved = sinon.spy();
  133. ctx.ctrl.$scope.$apply();
  134. }));
  135. it('should have already fetched the history list', function() {
  136. expect(historySrv.getHistoryList.calledOnce).to.be(true);
  137. expect(ctx.ctrl.revisions.length).to.be.above(0);
  138. });
  139. it('should check that two valid versions are selected', function() {
  140. // []
  141. expect(ctx.ctrl.isComparable()).to.be(false);
  142. // single value
  143. ctx.ctrl.selected = [4];
  144. expect(ctx.ctrl.isComparable()).to.be(false);
  145. // both values in range
  146. ctx.ctrl.selected = [4, 2];
  147. expect(ctx.ctrl.isComparable()).to.be(true);
  148. // values out of range
  149. ctx.ctrl.selected = [7, 4];
  150. expect(ctx.ctrl.isComparable()).to.be(false);
  151. });
  152. describe('and the basic diff is successfully fetched', function() {
  153. beforeEach(function() {
  154. deferred.resolve(compare('basic'));
  155. ctx.ctrl.selected = [3, 1];
  156. ctx.ctrl.getDiff('basic');
  157. ctx.ctrl.$scope.$apply();
  158. });
  159. it('should fetch the basic diff if two valid versions are selected', function() {
  160. expect(historySrv.compareVersions.calledOnce).to.be(true);
  161. expect(ctx.ctrl.delta.basic).to.be('<div></div>');
  162. expect(ctx.ctrl.delta.html).to.be('');
  163. });
  164. it('should set the basic diff view as active', function() {
  165. expect(ctx.ctrl.mode).to.be('compare');
  166. expect(ctx.ctrl.diff).to.be('basic');
  167. });
  168. it('should indicate loading has finished', function() {
  169. expect(ctx.ctrl.loading).to.be(false);
  170. });
  171. });
  172. describe('and the json diff is successfully fetched', function() {
  173. beforeEach(function() {
  174. deferred.resolve(compare('html'));
  175. ctx.ctrl.selected = [3, 1];
  176. ctx.ctrl.getDiff('html');
  177. ctx.ctrl.$scope.$apply();
  178. });
  179. it('should fetch the json diff if two valid versions are selected', function() {
  180. expect(historySrv.compareVersions.calledOnce).to.be(true);
  181. expect(ctx.ctrl.delta.basic).to.be('');
  182. expect(ctx.ctrl.delta.html).to.be('<pre><code></code></pre>');
  183. });
  184. it('should set the json diff view as active', function() {
  185. expect(ctx.ctrl.mode).to.be('compare');
  186. expect(ctx.ctrl.diff).to.be('html');
  187. });
  188. it('should indicate loading has finished', function() {
  189. expect(ctx.ctrl.loading).to.be(false);
  190. });
  191. });
  192. describe('and diffs have already been fetched', function() {
  193. beforeEach(function() {
  194. deferred.resolve(compare('basic'));
  195. ctx.ctrl.selected = [3, 1];
  196. ctx.ctrl.delta.basic = 'cached basic';
  197. ctx.ctrl.getDiff('basic');
  198. ctx.ctrl.$scope.$apply();
  199. });
  200. it('should use the cached diffs instead of fetching', function() {
  201. expect(historySrv.compareVersions.calledOnce).to.be(false);
  202. expect(ctx.ctrl.delta.basic).to.be('cached basic');
  203. });
  204. it('should indicate loading has finished', function() {
  205. expect(ctx.ctrl.loading).to.be(false);
  206. });
  207. });
  208. describe('and fetching the diff fails', function() {
  209. beforeEach(function() {
  210. deferred.reject(new Error('DiffError'));
  211. ctx.ctrl.selected = [4, 2];
  212. ctx.ctrl.getDiff('basic');
  213. ctx.ctrl.$scope.$apply();
  214. });
  215. it('should fetch the diff if two valid versions are selected', function() {
  216. expect(historySrv.compareVersions.calledOnce).to.be(true);
  217. });
  218. it('should return to the history list view', function() {
  219. expect(ctx.ctrl.mode).to.be('list');
  220. });
  221. it('should indicate loading has finished', function() {
  222. expect(ctx.ctrl.loading).to.be(false);
  223. });
  224. it('should have an empty delta/changeset', function() {
  225. expect(ctx.ctrl.delta).to.eql({ basic: '', html: '' });
  226. });
  227. });
  228. });
  229. describe('when the user wants to restore a revision', function() {
  230. var deferred;
  231. beforeEach(angularMocks.inject(($controller, $q) => {
  232. deferred = $q.defer();
  233. historySrv.getHistoryList.returns($q.when(versionsResponse));
  234. historySrv.restoreDashboard.returns(deferred.promise);
  235. ctx.ctrl = $controller(HistoryListCtrl, {
  236. historySrv,
  237. contextSrv: { user: { name: 'Carlos' }},
  238. $rootScope,
  239. $scope: ctx.scope,
  240. });
  241. ctx.ctrl.dashboard = { id: 1 };
  242. ctx.ctrl.restore();
  243. ctx.ctrl.$scope.$apply();
  244. }));
  245. it('should display a modal allowing the user to restore or cancel', function() {
  246. expect($rootScope.appEvent.calledOnce).to.be(true);
  247. expect($rootScope.appEvent.calledWith('confirm-modal')).to.be(true);
  248. });
  249. describe('and restore fails to fetch', function() {
  250. beforeEach(function() {
  251. deferred.reject(new Error('RestoreError'));
  252. ctx.ctrl.restoreConfirm(RESTORE_ID);
  253. try {
  254. // this throws error, due to promise rejection
  255. ctx.ctrl.$scope.$apply();
  256. } catch (e) {}
  257. });
  258. it('should indicate loading has finished', function() {
  259. expect(ctx.ctrl.loading).to.be(false);
  260. });
  261. });
  262. });
  263. });