history_ctrl_specs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. calculateDiff: 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. dashboard: {
  40. id: 2,
  41. version: 3,
  42. formatDate: sinon.stub().returns('date'),
  43. }
  44. });
  45. }));
  46. it('should immediately attempt to fetch the history list', function() {
  47. expect(historySrv.getHistoryList.calledOnce).to.be(true);
  48. });
  49. describe('and the history list is successfully fetched', function() {
  50. beforeEach(function() {
  51. deferred.resolve(versionsResponse);
  52. ctx.ctrl.$scope.$apply();
  53. });
  54. it('should reset the controller\'s state', function() {
  55. expect(ctx.ctrl.mode).to.be('list');
  56. expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' });
  57. expect(ctx.ctrl.canCompare).to.be(false);
  58. expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be.undefined;
  59. });
  60. it('should indicate loading has finished', function() {
  61. expect(ctx.ctrl.loading).to.be(false);
  62. });
  63. it('should store the revisions sorted desc by version id', function() {
  64. expect(ctx.ctrl.revisions[0].version).to.be(4);
  65. expect(ctx.ctrl.revisions[1].version).to.be(3);
  66. expect(ctx.ctrl.revisions[2].version).to.be(2);
  67. expect(ctx.ctrl.revisions[3].version).to.be(1);
  68. });
  69. it('should add a checked property to each revision', function() {
  70. var actual = _.filter(ctx.ctrl.revisions, rev => rev.hasOwnProperty('checked'));
  71. expect(actual.length).to.be(4);
  72. });
  73. it('should set all checked properties to false on reset', function() {
  74. ctx.ctrl.revisions[0].checked = true;
  75. ctx.ctrl.revisions[2].checked = true;
  76. ctx.ctrl.reset();
  77. var actual = _.filter(ctx.ctrl.revisions, rev => !rev.checked);
  78. expect(actual.length).to.be(4);
  79. });
  80. });
  81. describe('and fetching the history list fails', function() {
  82. beforeEach(function() {
  83. deferred.reject(new Error('HistoryListError'));
  84. ctx.ctrl.$scope.$apply();
  85. });
  86. it('should reset the controller\'s state', function() {
  87. expect(ctx.ctrl.mode).to.be('list');
  88. expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' });
  89. expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be.undefined;
  90. });
  91. it('should indicate loading has finished', function() {
  92. expect(ctx.ctrl.loading).to.be(false);
  93. });
  94. it('should have an empty revisions list', function() {
  95. expect(ctx.ctrl.revisions).to.eql([]);
  96. });
  97. });
  98. describe('should update the history list when the dashboard is saved', function() {
  99. beforeEach(function() {
  100. ctx.ctrl.dashboard = {version: 3 };
  101. ctx.ctrl.resetFromSource = sinon.spy();
  102. });
  103. it('should listen for the `dashboard-saved` appEvent', function() {
  104. expect($rootScope.onAppEvent.calledOnce).to.be(true);
  105. expect($rootScope.onAppEvent.getCall(0).args[0]).to.be('dashboard-saved');
  106. });
  107. it('should call `onDashboardSaved` when the appEvent is received', function() {
  108. expect($rootScope.onAppEvent.getCall(0).args[1]).to.not.be(ctx.ctrl.onDashboardSaved);
  109. expect($rootScope.onAppEvent.getCall(0).args[1].toString).to.be(ctx.ctrl.onDashboardSaved.toString);
  110. });
  111. });
  112. });
  113. describe('when the user wants to compare two revisions', function() {
  114. var deferred;
  115. beforeEach(angularMocks.inject(($controller, $q) => {
  116. deferred = $q.defer();
  117. historySrv.getHistoryList.returns($q.when(versionsResponse));
  118. historySrv.calculateDiff.returns(deferred.promise);
  119. ctx.ctrl = $controller(HistoryListCtrl, {
  120. historySrv,
  121. $rootScope,
  122. $scope: ctx.scope,
  123. }, {
  124. dashboard: {
  125. id: 2,
  126. version: 3,
  127. formatDate: sinon.stub().returns('date'),
  128. }
  129. });
  130. ctx.ctrl.$scope.onDashboardSaved = sinon.spy();
  131. ctx.ctrl.$scope.$apply();
  132. }));
  133. it('should have already fetched the history list', function() {
  134. expect(historySrv.getHistoryList.calledOnce).to.be(true);
  135. expect(ctx.ctrl.revisions.length).to.be.above(0);
  136. });
  137. it('should check that two valid versions are selected', function() {
  138. // []
  139. expect(ctx.ctrl.canCompare).to.be(false);
  140. // single value
  141. ctx.ctrl.revisions = [{checked: true}];
  142. ctx.ctrl.revisionSelectionChanged();
  143. expect(ctx.ctrl.canCompare).to.be(false);
  144. // both values in range
  145. ctx.ctrl.revisions = [{checked: true}, {checked: true}];
  146. ctx.ctrl.revisionSelectionChanged();
  147. expect(ctx.ctrl.canCompare).to.be(true);
  148. });
  149. describe('and the basic diff is successfully fetched', function() {
  150. beforeEach(function() {
  151. deferred.resolve(compare('basic'));
  152. ctx.ctrl.revisions[1].checked = true;
  153. ctx.ctrl.revisions[3].checked = true;
  154. ctx.ctrl.getDiff('basic');
  155. ctx.ctrl.$scope.$apply();
  156. });
  157. it('should fetch the basic diff if two valid versions are selected', function() {
  158. expect(historySrv.calculateDiff.calledOnce).to.be(true);
  159. expect(ctx.ctrl.delta.basic).to.be('<div></div>');
  160. expect(ctx.ctrl.delta.json).to.be('');
  161. });
  162. it('should set the basic diff view as active', function() {
  163. expect(ctx.ctrl.mode).to.be('compare');
  164. expect(ctx.ctrl.diff).to.be('basic');
  165. });
  166. it('should indicate loading has finished', function() {
  167. expect(ctx.ctrl.loading).to.be(false);
  168. });
  169. });
  170. describe('and the json diff is successfully fetched', function() {
  171. beforeEach(function() {
  172. deferred.resolve(compare('json'));
  173. ctx.ctrl.revisions[1].checked = true;
  174. ctx.ctrl.revisions[3].checked = true;
  175. ctx.ctrl.getDiff('json');
  176. ctx.ctrl.$scope.$apply();
  177. });
  178. it('should fetch the json diff if two valid versions are selected', function() {
  179. expect(historySrv.calculateDiff.calledOnce).to.be(true);
  180. expect(ctx.ctrl.delta.basic).to.be('');
  181. expect(ctx.ctrl.delta.json).to.be('<pre><code></code></pre>');
  182. });
  183. it('should set the json diff view as active', function() {
  184. expect(ctx.ctrl.mode).to.be('compare');
  185. expect(ctx.ctrl.diff).to.be('json');
  186. });
  187. it('should indicate loading has finished', function() {
  188. expect(ctx.ctrl.loading).to.be(false);
  189. });
  190. });
  191. describe('and diffs have already been fetched', function() {
  192. beforeEach(function() {
  193. deferred.resolve(compare('basic'));
  194. ctx.ctrl.revisions[3].checked = true;
  195. ctx.ctrl.revisions[1].checked = true;
  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.calculateDiff.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.revisions[3].checked = true;
  212. ctx.ctrl.revisions[1].checked = true;
  213. ctx.ctrl.getDiff('basic');
  214. ctx.ctrl.$scope.$apply();
  215. });
  216. it('should fetch the diff if two valid versions are selected', function() {
  217. expect(historySrv.calculateDiff.calledOnce).to.be(true);
  218. });
  219. it('should return to the history list view', function() {
  220. expect(ctx.ctrl.mode).to.be('list');
  221. });
  222. it('should indicate loading has finished', function() {
  223. expect(ctx.ctrl.loading).to.be(false);
  224. });
  225. it('should have an empty delta/changeset', function() {
  226. expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' });
  227. });
  228. });
  229. });
  230. describe('when the user wants to restore a revision', function() {
  231. var deferred;
  232. beforeEach(angularMocks.inject(($controller, $q) => {
  233. deferred = $q.defer();
  234. historySrv.getHistoryList.returns($q.when(versionsResponse));
  235. historySrv.restoreDashboard.returns(deferred.promise);
  236. ctx.ctrl = $controller(HistoryListCtrl, {
  237. historySrv,
  238. contextSrv: { user: { name: 'Carlos' }},
  239. $rootScope,
  240. $scope: ctx.scope,
  241. });
  242. ctx.ctrl.dashboard = { id: 1 };
  243. ctx.ctrl.restore();
  244. ctx.ctrl.$scope.$apply();
  245. }));
  246. it('should display a modal allowing the user to restore or cancel', function() {
  247. expect($rootScope.appEvent.calledOnce).to.be(true);
  248. expect($rootScope.appEvent.calledWith('confirm-modal')).to.be(true);
  249. });
  250. describe('and restore fails to fetch', function() {
  251. beforeEach(function() {
  252. deferred.reject(new Error('RestoreError'));
  253. ctx.ctrl.restoreConfirm(RESTORE_ID);
  254. try {
  255. // this throws error, due to promise rejection
  256. ctx.ctrl.$scope.$apply();
  257. } catch (e) {}
  258. });
  259. it('should indicate loading has finished', function() {
  260. expect(ctx.ctrl.loading).to.be(false);
  261. });
  262. });
  263. });
  264. });