Dashboard: fixed issues with re-rendering of UI when importing dashboard (#21723)

* Fixed issues with angular digest and new backendSrv.
* Fixed failing tests.
This commit is contained in:
Marcus Andersson 2020-01-27 11:29:46 +01:00 committed by GitHub
parent f46ee12364
commit 4e3ff19689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 49 deletions

View File

@ -1,12 +1,14 @@
import { DashboardImportCtrl } from './DashboardImportCtrl';
import config from 'app/core/config';
import { backendSrv } from 'app/core/services/backend_srv';
import { IScope } from 'angular';
describe('DashboardImportCtrl', () => {
const ctx: any = {};
jest.spyOn(backendSrv, 'getDashboardByUid').mockImplementation(() => Promise.resolve([]));
jest.spyOn(backendSrv, 'search').mockImplementation(() => Promise.resolve([]));
const getMock = jest.spyOn(backendSrv, 'get');
const $scope = ({ $evalAsync: jest.fn() } as any) as IScope;
let navModelSrv: any;
let validationSrv: any;
@ -20,7 +22,7 @@ describe('DashboardImportCtrl', () => {
validateNewDashboardName: jest.fn().mockReturnValue(Promise.resolve()),
};
ctx.ctrl = new DashboardImportCtrl(validationSrv, navModelSrv, {} as any, {} as any);
ctx.ctrl = new DashboardImportCtrl($scope, validationSrv, navModelSrv, {} as any, {} as any);
jest.clearAllMocks();
});

View File

@ -3,8 +3,9 @@ import config from 'app/core/config';
import locationUtil from 'app/core/utils/location_util';
import { ValidationSrv } from './services/ValidationSrv';
import { NavModelSrv } from 'app/core/core';
import { ILocationService } from 'angular';
import { ILocationService, IScope } from 'angular';
import { backendSrv } from 'app/core/services/backend_srv';
import { promiseToDigest } from 'app/core/utils/promiseToDigest';
export class DashboardImportCtrl {
navModel: any;
@ -32,6 +33,7 @@ export class DashboardImportCtrl {
/** @ngInject */
constructor(
private $scope: IScope,
private validationSrv: ValidationSrv,
navModelSrv: NavModelSrv,
private $location: ILocationService,
@ -116,6 +118,7 @@ export class DashboardImportCtrl {
this.titleTouched = true;
this.nameExists = false;
promiseToDigest(this.$scope)(
this.validationSrv
.validateNewDashboardName(this.folderId, this.dash.title)
.then(() => {
@ -129,7 +132,8 @@ export class DashboardImportCtrl {
this.hasNameValidationError = true;
this.nameValidationError = err.message;
});
})
);
}
uidChanged(initial: boolean) {
@ -144,6 +148,7 @@ export class DashboardImportCtrl {
return;
}
promiseToDigest(this.$scope)(
backendSrv
// @ts-ignore
.getDashboardByUid(this.dash.uid)
@ -154,7 +159,8 @@ export class DashboardImportCtrl {
})
.catch((err: any) => {
err.isHandled = true;
});
})
);
}
onFolderChange(folder: any) {
@ -184,7 +190,8 @@ export class DashboardImportCtrl {
};
});
return backendSrv
return promiseToDigest(this.$scope)(
backendSrv
.post('api/dashboards/import', {
dashboard: this.dash,
overwrite: true,
@ -194,7 +201,8 @@ export class DashboardImportCtrl {
.then(res => {
const dashUrl = locationUtil.stripBaseFromUrl(res.importedUrl);
this.$location.url(dashUrl);
});
})
);
}
loadJsonText() {
@ -223,7 +231,8 @@ export class DashboardImportCtrl {
this.gnetError = 'Could not find dashboard';
}
return backendSrv
return promiseToDigest(this.$scope)(
backendSrv
.get('api/gnet/dashboards/' + dashboardId)
.then(res => {
this.gnetInfo = res;
@ -234,7 +243,8 @@ export class DashboardImportCtrl {
.catch(err => {
err.isHandled = true;
this.gnetError = err.data.message || err;
});
})
);
}
back() {