2019-01-22 14:36:36 -06:00
|
|
|
import { DashboardImportCtrl } from './DashboardImportCtrl';
|
|
|
|
import config from 'app/core/config';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
2020-01-27 04:29:46 -06:00
|
|
|
import { IScope } from 'angular';
|
2016-05-27 06:52:19 -05:00
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
describe('DashboardImportCtrl', () => {
|
2018-08-26 13:19:23 -05:00
|
|
|
const ctx: any = {};
|
2020-01-21 03:08:07 -06:00
|
|
|
jest.spyOn(backendSrv, 'getDashboardByUid').mockImplementation(() => Promise.resolve([]));
|
|
|
|
jest.spyOn(backendSrv, 'search').mockImplementation(() => Promise.resolve([]));
|
|
|
|
const getMock = jest.spyOn(backendSrv, 'get');
|
2020-01-27 04:29:46 -06:00
|
|
|
const $scope = ({ $evalAsync: jest.fn() } as any) as IScope;
|
2016-05-27 06:52:19 -05:00
|
|
|
|
2019-07-18 01:03:04 -05:00
|
|
|
let navModelSrv: any;
|
|
|
|
let validationSrv: any;
|
2016-05-27 06:52:19 -05:00
|
|
|
|
2017-12-04 07:51:37 -06:00
|
|
|
beforeEach(() => {
|
|
|
|
navModelSrv = {
|
2017-12-20 05:33:33 -06:00
|
|
|
getNav: () => {},
|
2017-12-04 07:51:37 -06:00
|
|
|
};
|
|
|
|
|
2017-12-19 06:39:10 -06:00
|
|
|
validationSrv = {
|
2018-02-03 03:03:01 -06:00
|
|
|
validateNewDashboardName: jest.fn().mockReturnValue(Promise.resolve()),
|
2017-12-19 06:39:10 -06:00
|
|
|
};
|
|
|
|
|
2020-01-27 04:29:46 -06:00
|
|
|
ctx.ctrl = new DashboardImportCtrl($scope, validationSrv, navModelSrv, {} as any, {} as any);
|
2020-01-21 03:08:07 -06:00
|
|
|
|
|
|
|
jest.clearAllMocks();
|
2017-12-04 07:51:37 -06:00
|
|
|
});
|
2016-05-27 06:52:19 -05:00
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
describe('when uploading json', () => {
|
|
|
|
beforeEach(() => {
|
2016-05-27 06:52:19 -05:00
|
|
|
config.datasources = {
|
|
|
|
ds: {
|
2017-12-20 05:33:33 -06:00
|
|
|
type: 'test-db',
|
2019-04-04 11:30:15 -05:00
|
|
|
} as any,
|
2016-05-27 06:52:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ctrl.onUpload({
|
2017-12-19 09:06:54 -06:00
|
|
|
__inputs: [
|
|
|
|
{
|
2017-12-20 05:33:33 -06:00
|
|
|
name: 'ds',
|
|
|
|
pluginId: 'test-db',
|
|
|
|
type: 'datasource',
|
|
|
|
pluginName: 'Test DB',
|
|
|
|
},
|
|
|
|
],
|
2016-05-27 06:52:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
it('should build input model', () => {
|
2017-12-04 07:51:37 -06:00
|
|
|
expect(ctx.ctrl.inputs.length).toBe(1);
|
2017-12-20 05:33:33 -06:00
|
|
|
expect(ctx.ctrl.inputs[0].name).toBe('ds');
|
|
|
|
expect(ctx.ctrl.inputs[0].info).toBe('Select a Test DB data source');
|
2016-05-27 06:52:19 -05:00
|
|
|
});
|
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
it('should set inputValid to false', () => {
|
2017-12-04 07:51:37 -06:00
|
|
|
expect(ctx.ctrl.inputsValid).toBe(false);
|
2016-05-27 06:52:19 -05:00
|
|
|
});
|
2016-05-27 09:11:05 -05:00
|
|
|
});
|
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
describe('when specifying grafana.com url', () => {
|
|
|
|
beforeEach(() => {
|
2017-12-20 05:33:33 -06:00
|
|
|
ctx.ctrl.gnetUrl = 'http://grafana.com/dashboards/123';
|
2016-05-27 09:11:05 -05:00
|
|
|
// setup api mock
|
2020-01-21 03:08:07 -06:00
|
|
|
getMock.mockImplementation(() => Promise.resolve({ json: {} }));
|
2017-10-08 08:56:42 -05:00
|
|
|
return ctx.ctrl.checkGnetDashboard();
|
2016-05-27 09:11:05 -05:00
|
|
|
});
|
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
it('should call gnet api with correct dashboard id', () => {
|
2020-01-21 03:08:07 -06:00
|
|
|
expect(getMock.mock.calls[0][0]).toBe('api/gnet/dashboards/123');
|
2016-05-27 09:11:05 -05:00
|
|
|
});
|
|
|
|
});
|
2016-05-27 06:52:19 -05:00
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
describe('when specifying dashboard id', () => {
|
|
|
|
beforeEach(() => {
|
2017-12-20 05:33:33 -06:00
|
|
|
ctx.ctrl.gnetUrl = '2342';
|
2016-05-27 09:11:05 -05:00
|
|
|
// setup api mock
|
2020-01-21 03:08:07 -06:00
|
|
|
getMock.mockImplementation(() => Promise.resolve({ json: {} }));
|
2017-10-08 08:56:42 -05:00
|
|
|
return ctx.ctrl.checkGnetDashboard();
|
2016-05-27 09:11:05 -05:00
|
|
|
});
|
|
|
|
|
2018-09-04 08:55:41 -05:00
|
|
|
it('should call gnet api with correct dashboard id', () => {
|
2020-01-21 03:08:07 -06:00
|
|
|
expect(getMock.mock.calls[0][0]).toBe('api/gnet/dashboards/2342');
|
2016-05-27 09:11:05 -05:00
|
|
|
});
|
2016-05-27 06:52:19 -05:00
|
|
|
});
|
|
|
|
});
|