diff --git a/public/app/features/dashboard/history/history_srv.ts b/public/app/features/dashboard/history/history_srv.ts index bd6e7223a23..7f7dc950de3 100644 --- a/public/app/features/dashboard/history/history_srv.ts +++ b/public/app/features/dashboard/history/history_srv.ts @@ -32,11 +32,11 @@ export interface DiffTarget { export class HistorySrv { /** @ngInject */ - constructor(private backendSrv, private $q) {} + constructor(private backendSrv) {} getHistoryList(dashboard: DashboardModel, options: HistoryListOpts) { const id = dashboard && dashboard.id ? dashboard.id : void 0; - return id ? this.backendSrv.get(`api/dashboards/id/${id}/versions`, options) : this.$q.when([]); + return id ? this.backendSrv.get(`api/dashboards/id/${id}/versions`, options) : Promise.resolve([]); } calculateDiff(options: CalculateDiffOptions) { @@ -46,7 +46,8 @@ export class HistorySrv { restoreDashboard(dashboard: DashboardModel, version: number) { const id = dashboard && dashboard.id ? dashboard.id : void 0; const url = `api/dashboards/id/${id}/restore`; - return id && _.isNumber(version) ? this.backendSrv.post(url, { version }) : this.$q.when({}); + + return id && _.isNumber(version) ? this.backendSrv.post(url, { version }) : Promise.resolve({}); } } diff --git a/public/app/features/dashboard/specs/history_srv.jest.ts b/public/app/features/dashboard/specs/history_srv.jest.ts new file mode 100644 index 00000000000..401b098a0e1 --- /dev/null +++ b/public/app/features/dashboard/specs/history_srv.jest.ts @@ -0,0 +1,61 @@ +import '../history/history_srv'; +import { versions, restore } from './history_mocks'; +import { HistorySrv } from '../history/history_srv'; +import { DashboardModel } from '../dashboard_model'; +jest.mock('app/core/store'); + +describe('historySrv', function() { + const versionsResponse = versions(); + const restoreResponse = restore; + + let backendSrv = { + get: jest.fn(() => Promise.resolve({})), + post: jest.fn(() => Promise.resolve({})), + }; + + let historySrv = new HistorySrv(backendSrv); + + const dash = new DashboardModel({ id: 1 }); + const emptyDash = new DashboardModel({}); + const historyListOpts = { limit: 10, start: 0 }; + + describe('getHistoryList', function() { + it('should return a versions array for the given dashboard id', function() { + backendSrv.get = jest.fn(() => Promise.resolve(versionsResponse)); + historySrv = new HistorySrv(backendSrv); + + return historySrv.getHistoryList(dash, historyListOpts).then(function(versions) { + expect(versions).toEqual(versionsResponse); + }); + }); + + it('should return an empty array when not given an id', function() { + return historySrv.getHistoryList(emptyDash, historyListOpts).then(function(versions) { + expect(versions).toEqual([]); + }); + }); + + it('should return an empty array when not given a dashboard', function() { + return historySrv.getHistoryList(null, historyListOpts).then(function(versions) { + expect(versions).toEqual([]); + }); + }); + }); + + describe('restoreDashboard', () => { + it('should return a success response given valid parameters', function() { + let version = 6; + backendSrv.post = jest.fn(() => Promise.resolve(restoreResponse(version))); + historySrv = new HistorySrv(backendSrv); + return historySrv.restoreDashboard(dash, version).then(function(response) { + expect(response).toEqual(restoreResponse(version)); + }); + }); + + it('should return an empty object when not given an id', async () => { + historySrv = new HistorySrv(backendSrv); + let rsp = await historySrv.restoreDashboard(emptyDash, 6); + expect(rsp).toEqual({}); + }); + }); +}); diff --git a/public/app/features/dashboard/specs/history_srv_specs.ts b/public/app/features/dashboard/specs/history_srv_specs.ts deleted file mode 100644 index a4a28ab9a34..00000000000 --- a/public/app/features/dashboard/specs/history_srv_specs.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; - -import helpers from 'test/specs/helpers'; -import '../history/history_srv'; -import { versions, restore } from './history_mocks'; - -describe('historySrv', function() { - var ctx = new helpers.ServiceTestContext(); - - var versionsResponse = versions(); - var restoreResponse = restore; - - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.services')); - beforeEach( - angularMocks.inject(function($httpBackend) { - ctx.$httpBackend = $httpBackend; - $httpBackend.whenRoute('GET', 'api/dashboards/id/:id/versions').respond(versionsResponse); - $httpBackend - .whenRoute('POST', 'api/dashboards/id/:id/restore') - .respond(function(method, url, data, headers, params) { - const parsedData = JSON.parse(data); - return [200, restoreResponse(parsedData.version)]; - }); - }) - ); - - beforeEach(ctx.createService('historySrv')); - - function wrapPromise(ctx, angularPromise) { - return new Promise((resolve, reject) => { - angularPromise.then(resolve, reject); - ctx.$httpBackend.flush(); - }); - } - - describe('getHistoryList', function() { - it('should return a versions array for the given dashboard id', function() { - return wrapPromise( - ctx, - ctx.service.getHistoryList({ id: 1 }).then(function(versions) { - expect(versions).to.eql(versionsResponse); - }) - ); - }); - - it('should return an empty array when not given an id', function() { - return wrapPromise( - ctx, - ctx.service.getHistoryList({}).then(function(versions) { - expect(versions).to.eql([]); - }) - ); - }); - - it('should return an empty array when not given a dashboard', function() { - return wrapPromise( - ctx, - ctx.service.getHistoryList().then(function(versions) { - expect(versions).to.eql([]); - }) - ); - }); - }); - - describe('restoreDashboard', function() { - it('should return a success response given valid parameters', function() { - let version = 6; - return wrapPromise( - ctx, - ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) { - expect(response).to.eql(restoreResponse(version)); - }) - ); - }); - - it('should return an empty object when not given an id', function() { - return wrapPromise( - ctx, - ctx.service.restoreDashboard({}, 6).then(function(response) { - expect(response).to.eql({}); - }) - ); - }); - }); -});