history_ctrl.test.ts 11 KB

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