history_srv_specs.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
  2. import helpers from 'test/specs/helpers';
  3. import '../history/history_srv';
  4. import {versions, restore} from './history_mocks';
  5. describe('historySrv', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. var versionsResponse = versions();
  8. var restoreResponse = restore;
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(angularMocks.inject(function($httpBackend) {
  12. ctx.$httpBackend = $httpBackend;
  13. $httpBackend.whenRoute('GET', 'api/dashboards/id/:id/versions').respond(versionsResponse);
  14. $httpBackend.whenRoute('POST', 'api/dashboards/id/:id/restore')
  15. .respond(function(method, url, data, headers, params) {
  16. const parsedData = JSON.parse(data);
  17. return [200, restoreResponse(parsedData.version)];
  18. });
  19. }));
  20. beforeEach(ctx.createService('historySrv'));
  21. function wrapPromise(ctx, angularPromise) {
  22. return new Promise((resolve, reject) => {
  23. angularPromise.then(resolve, reject);
  24. ctx.$httpBackend.flush();
  25. });
  26. }
  27. describe('getHistoryList', function() {
  28. it('should return a versions array for the given dashboard id', function() {
  29. return wrapPromise(ctx, ctx.service.getHistoryList({ id: 1 }).then(function(versions) {
  30. expect(versions).to.eql(versionsResponse);
  31. }));
  32. });
  33. it('should return an empty array when not given an id', function() {
  34. return wrapPromise(ctx, ctx.service.getHistoryList({ }).then(function(versions) {
  35. expect(versions).to.eql([]);
  36. }));
  37. });
  38. it('should return an empty array when not given a dashboard', function() {
  39. return wrapPromise(ctx, ctx.service.getHistoryList().then(function(versions) {
  40. expect(versions).to.eql([]);
  41. }));
  42. });
  43. });
  44. describe('restoreDashboard', function() {
  45. it('should return a success response given valid parameters', function() {
  46. let version = 6;
  47. return wrapPromise(ctx, ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) {
  48. expect(response).to.eql(restoreResponse(version));
  49. }));
  50. });
  51. it('should return an empty object when not given an id', function() {
  52. return wrapPromise(ctx, ctx.service.restoreDashboard({}, 6).then(function(response) {
  53. expect(response).to.eql({});
  54. }));
  55. });
  56. });
  57. });