history_ctrl.jest.ts 12 KB

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