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 './history_mocks';
  5. describe('HistoryListCtrl', function() {
  6. var RESTORE_ID = 4;
  7. var ctx: any = {};
  8. var versionsResponse: any = versions();
  9. restore(7, RESTORE_ID);
  10. beforeEach(angularMocks.module('grafana.core'));
  11. beforeEach(angularMocks.module('grafana.services'));
  12. beforeEach(angularMocks.inject($rootScope => {
  13. ctx.scope = $rootScope.$new();
  14. }));
  15. var historySrv;
  16. var $rootScope;
  17. beforeEach(function() {
  18. historySrv = {
  19. getHistoryList: sinon.stub(),
  20. calculateDiff: sinon.stub(),
  21. restoreDashboard: sinon.stub(),
  22. };
  23. $rootScope = {
  24. appEvent: sinon.spy(),
  25. onAppEvent: sinon.spy(),
  26. };
  27. });
  28. describe('when the history list component is loaded', function() {
  29. var deferred;
  30. beforeEach(angularMocks.inject(($controller, $q) => {
  31. deferred = $q.defer();
  32. historySrv.getHistoryList.returns(deferred.promise);
  33. ctx.ctrl = $controller(HistoryListCtrl, {
  34. historySrv,
  35. $rootScope,
  36. $scope: ctx.scope,
  37. }, {
  38. dashboard: {
  39. id: 2,
  40. version: 3,
  41. formatDate: sinon.stub().returns('date'),
  42. }
  43. });
  44. }));
  45. it('should immediately attempt to fetch the history list', function() {
  46. expect(historySrv.getHistoryList.calledOnce).to.be(true);
  47. });
  48. describe('and the history list is successfully fetched', function() {
  49. beforeEach(function() {
  50. deferred.resolve(versionsResponse);
  51. ctx.ctrl.$scope.$apply();
  52. });
  53. it('should reset the controller\'s state', function() {
  54. expect(ctx.ctrl.mode).to.be('list');
  55. expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' });
  56. expect(ctx.ctrl.canCompare).to.be(false);
  57. expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be(undefined);
  58. });
  59. it('should indicate loading has finished', function() {
  60. expect(ctx.ctrl.loading).to.be(false);
  61. });
  62. it('should store the revisions sorted desc by version id', function() {
  63. expect(ctx.ctrl.revisions[0].version).to.be(4);
  64. expect(ctx.ctrl.revisions[1].version).to.be(3);
  65. expect(ctx.ctrl.revisions[2].version).to.be(2);
  66. expect(ctx.ctrl.revisions[3].version).to.be(1);
  67. });
  68. it('should add a checked property to each revision', function() {
  69. var actual = _.filter(ctx.ctrl.revisions, rev => rev.hasOwnProperty('checked'));
  70. expect(actual.length).to.be(4);
  71. });
  72. it('should set all checked properties to false on reset', function() {
  73. ctx.ctrl.revisions[0].checked = true;
  74. ctx.ctrl.revisions[2].checked = true;
  75. ctx.ctrl.reset();
  76. var actual = _.filter(ctx.ctrl.revisions, rev => !rev.checked);
  77. expect(actual.length).to.be(4);
  78. });
  79. });
  80. describe('and fetching the history list fails', function() {
  81. beforeEach(function() {
  82. deferred.reject(new Error('HistoryListError'));
  83. ctx.ctrl.$scope.$apply();
  84. });
  85. it('should reset the controller\'s state', function() {
  86. expect(ctx.ctrl.mode).to.be('list');
  87. expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' });
  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 have an empty revisions list', function() {
  94. expect(ctx.ctrl.revisions).to.eql([]);
  95. });
  96. });
  97. describe('should update the history list when the dashboard is saved', function() {
  98. beforeEach(function() {
  99. ctx.ctrl.dashboard = {version: 3 };
  100. ctx.ctrl.resetFromSource = sinon.spy();
  101. });
  102. it('should listen for the `dashboard-saved` appEvent', function() {
  103. expect($rootScope.onAppEvent.calledOnce).to.be(true);
  104. expect($rootScope.onAppEvent.getCall(0).args[0]).to.be('dashboard-saved');
  105. });
  106. it('should call `onDashboardSaved` when the appEvent is received', function() {
  107. expect($rootScope.onAppEvent.getCall(0).args[1]).to.not.be(ctx.ctrl.onDashboardSaved);
  108. expect($rootScope.onAppEvent.getCall(0).args[1].toString).to.be(ctx.ctrl.onDashboardSaved.toString);
  109. });
  110. });
  111. });
  112. describe('when the user wants to compare two revisions', function() {
  113. var deferred;
  114. beforeEach(angularMocks.inject(($controller, $q) => {
  115. deferred = $q.defer();
  116. historySrv.getHistoryList.returns($q.when(versionsResponse));
  117. historySrv.calculateDiff.returns(deferred.promise);
  118. ctx.ctrl = $controller(HistoryListCtrl, {
  119. historySrv,
  120. $rootScope,
  121. $scope: ctx.scope,
  122. }, {
  123. dashboard: {
  124. id: 2,
  125. version: 3,
  126. formatDate: sinon.stub().returns('date'),
  127. }
  128. });
  129. ctx.ctrl.$scope.onDashboardSaved = sinon.spy();
  130. ctx.ctrl.$scope.$apply();
  131. }));
  132. it('should have already fetched the history list', function() {
  133. expect(historySrv.getHistoryList.calledOnce).to.be(true);
  134. expect(ctx.ctrl.revisions.length).to.be.above(0);
  135. });
  136. it('should check that two valid versions are selected', function() {
  137. // []
  138. expect(ctx.ctrl.canCompare).to.be(false);
  139. // single value
  140. ctx.ctrl.revisions = [{checked: true}];
  141. ctx.ctrl.revisionSelectionChanged();
  142. expect(ctx.ctrl.canCompare).to.be(false);
  143. // both values in range
  144. ctx.ctrl.revisions = [{checked: true}, {checked: true}];
  145. ctx.ctrl.revisionSelectionChanged();
  146. expect(ctx.ctrl.canCompare).to.be(true);
  147. });
  148. describe('and the basic diff is successfully fetched', function() {
  149. beforeEach(function() {
  150. deferred.resolve(compare('basic'));
  151. ctx.ctrl.revisions[1].checked = true;
  152. ctx.ctrl.revisions[3].checked = true;
  153. ctx.ctrl.getDiff('basic');
  154. ctx.ctrl.$scope.$apply();
  155. });
  156. it('should fetch the basic diff if two valid versions are selected', function() {
  157. expect(historySrv.calculateDiff.calledOnce).to.be(true);
  158. expect(ctx.ctrl.delta.basic).to.be('<div></div>');
  159. expect(ctx.ctrl.delta.json).to.be('');
  160. });
  161. it('should set the basic diff view as active', function() {
  162. expect(ctx.ctrl.mode).to.be('compare');
  163. expect(ctx.ctrl.diff).to.be('basic');
  164. });
  165. it('should indicate loading has finished', function() {
  166. expect(ctx.ctrl.loading).to.be(false);
  167. });
  168. });
  169. describe('and the json diff is successfully fetched', function() {
  170. beforeEach(function() {
  171. deferred.resolve(compare('json'));
  172. ctx.ctrl.revisions[1].checked = true;
  173. ctx.ctrl.revisions[3].checked = true;
  174. ctx.ctrl.getDiff('json');
  175. ctx.ctrl.$scope.$apply();
  176. });
  177. it('should fetch the json diff if two valid versions are selected', function() {
  178. expect(historySrv.calculateDiff.calledOnce).to.be(true);
  179. expect(ctx.ctrl.delta.basic).to.be('');
  180. expect(ctx.ctrl.delta.json).to.be('<pre><code></code></pre>');
  181. });
  182. it('should set the json diff view as active', function() {
  183. expect(ctx.ctrl.mode).to.be('compare');
  184. expect(ctx.ctrl.diff).to.be('json');
  185. });
  186. it('should indicate loading has finished', function() {
  187. expect(ctx.ctrl.loading).to.be(false);
  188. });
  189. });
  190. describe('and diffs have already been fetched', function() {
  191. beforeEach(function() {
  192. deferred.resolve(compare('basic'));
  193. ctx.ctrl.revisions[3].checked = true;
  194. ctx.ctrl.revisions[1].checked = true;
  195. ctx.ctrl.delta.basic = 'cached basic';
  196. ctx.ctrl.getDiff('basic');
  197. ctx.ctrl.$scope.$apply();
  198. });
  199. it('should use the cached diffs instead of fetching', function() {
  200. expect(historySrv.calculateDiff.calledOnce).to.be(false);
  201. expect(ctx.ctrl.delta.basic).to.be('cached basic');
  202. });
  203. it('should indicate loading has finished', function() {
  204. expect(ctx.ctrl.loading).to.be(false);
  205. });
  206. });
  207. describe('and fetching the diff fails', function() {
  208. beforeEach(function() {
  209. deferred.reject(new Error('DiffError'));
  210. ctx.ctrl.revisions[3].checked = true;
  211. ctx.ctrl.revisions[1].checked = true;
  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.calculateDiff.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: '', json: '' });
  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. });