history_ctrl_specs.ts 11 KB

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