diff --git a/public/app/features/manage-dashboards/DashboardImportCtrl.test.ts b/public/app/features/manage-dashboards/DashboardImportCtrl.test.ts index b15b40d87f0..07a1f47252f 100644 --- a/public/app/features/manage-dashboards/DashboardImportCtrl.test.ts +++ b/public/app/features/manage-dashboards/DashboardImportCtrl.test.ts @@ -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(); }); diff --git a/public/app/features/manage-dashboards/DashboardImportCtrl.ts b/public/app/features/manage-dashboards/DashboardImportCtrl.ts index 4aa7f74ee53..07add9b3e22 100644 --- a/public/app/features/manage-dashboards/DashboardImportCtrl.ts +++ b/public/app/features/manage-dashboards/DashboardImportCtrl.ts @@ -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,20 +118,22 @@ export class DashboardImportCtrl { this.titleTouched = true; this.nameExists = false; - this.validationSrv - .validateNewDashboardName(this.folderId, this.dash.title) - .then(() => { - this.nameExists = false; - this.hasNameValidationError = false; - }) - .catch(err => { - if (err.type === 'EXISTING') { - this.nameExists = true; - } + promiseToDigest(this.$scope)( + this.validationSrv + .validateNewDashboardName(this.folderId, this.dash.title) + .then(() => { + this.nameExists = false; + this.hasNameValidationError = false; + }) + .catch(err => { + if (err.type === 'EXISTING') { + this.nameExists = true; + } - this.hasNameValidationError = true; - this.nameValidationError = err.message; - }); + this.hasNameValidationError = true; + this.nameValidationError = err.message; + }) + ); } uidChanged(initial: boolean) { @@ -144,17 +148,19 @@ export class DashboardImportCtrl { return; } - backendSrv - // @ts-ignore - .getDashboardByUid(this.dash.uid) - .then((res: any) => { - this.uidExists = true; - this.hasUidValidationError = true; - this.uidValidationError = `Dashboard named '${res.dashboard.title}' in folder '${res.meta.folderTitle}' has the same uid`; - }) - .catch((err: any) => { - err.isHandled = true; - }); + promiseToDigest(this.$scope)( + backendSrv + // @ts-ignore + .getDashboardByUid(this.dash.uid) + .then((res: any) => { + this.uidExists = true; + this.hasUidValidationError = true; + this.uidValidationError = `Dashboard named '${res.dashboard.title}' in folder '${res.meta.folderTitle}' has the same uid`; + }) + .catch((err: any) => { + err.isHandled = true; + }) + ); } onFolderChange(folder: any) { @@ -184,17 +190,19 @@ export class DashboardImportCtrl { }; }); - return backendSrv - .post('api/dashboards/import', { - dashboard: this.dash, - overwrite: true, - inputs: inputs, - folderId: this.folderId, - }) - .then(res => { - const dashUrl = locationUtil.stripBaseFromUrl(res.importedUrl); - this.$location.url(dashUrl); - }); + return promiseToDigest(this.$scope)( + backendSrv + .post('api/dashboards/import', { + dashboard: this.dash, + overwrite: true, + inputs: inputs, + folderId: this.folderId, + }) + .then(res => { + const dashUrl = locationUtil.stripBaseFromUrl(res.importedUrl); + this.$location.url(dashUrl); + }) + ); } loadJsonText() { @@ -223,18 +231,20 @@ export class DashboardImportCtrl { this.gnetError = 'Could not find dashboard'; } - return backendSrv - .get('api/gnet/dashboards/' + dashboardId) - .then(res => { - this.gnetInfo = res; - // store reference to grafana.com - res.json.gnetId = res.id; - this.onUpload(res.json); - }) - .catch(err => { - err.isHandled = true; - this.gnetError = err.data.message || err; - }); + return promiseToDigest(this.$scope)( + backendSrv + .get('api/gnet/dashboards/' + dashboardId) + .then(res => { + this.gnetInfo = res; + // store reference to grafana.com + res.json.gnetId = res.id; + this.onUpload(res.json); + }) + .catch(err => { + err.isHandled = true; + this.gnetError = err.data.message || err; + }) + ); } back() {