mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Karma to Jest: history_srv (#12341)
* Karma to Jest: history_srv * Fix TS errors * Remove q, as it is not needed
This commit is contained in:
parent
d450ec94eb
commit
3a9a36d6cf
@ -32,11 +32,11 @@ export interface DiffTarget {
|
|||||||
|
|
||||||
export class HistorySrv {
|
export class HistorySrv {
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor(private backendSrv, private $q) {}
|
constructor(private backendSrv) {}
|
||||||
|
|
||||||
getHistoryList(dashboard: DashboardModel, options: HistoryListOpts) {
|
getHistoryList(dashboard: DashboardModel, options: HistoryListOpts) {
|
||||||
const id = dashboard && dashboard.id ? dashboard.id : void 0;
|
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) {
|
calculateDiff(options: CalculateDiffOptions) {
|
||||||
@ -46,7 +46,8 @@ export class HistorySrv {
|
|||||||
restoreDashboard(dashboard: DashboardModel, version: number) {
|
restoreDashboard(dashboard: DashboardModel, version: number) {
|
||||||
const id = dashboard && dashboard.id ? dashboard.id : void 0;
|
const id = dashboard && dashboard.id ? dashboard.id : void 0;
|
||||||
const url = `api/dashboards/id/${id}/restore`;
|
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({});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
61
public/app/features/dashboard/specs/history_srv.jest.ts
Normal file
61
public/app/features/dashboard/specs/history_srv.jest.ts
Normal file
@ -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({});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -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({});
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user