From 8aa55ee352ca9c2e8f36485c69f6f124e0d958a1 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 25 Jan 2018 14:15:40 +0100 Subject: [PATCH] variables: fix when datasource returns error --- public/app/features/templating/editor_ctrl.ts | 14 +++---- .../templating/specs/editor_ctrl.jest.ts | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 public/app/features/templating/specs/editor_ctrl.jest.ts diff --git a/public/app/features/templating/editor_ctrl.ts b/public/app/features/templating/editor_ctrl.ts index af529e085c7..428770a21e5 100644 --- a/public/app/features/templating/editor_ctrl.ts +++ b/public/app/features/templating/editor_ctrl.ts @@ -1,6 +1,7 @@ import _ from 'lodash'; import coreModule from 'app/core/core_module'; import { variableTypes } from './variable'; +import appEvents from 'app/core/app_events'; export class VariableEditorCtrl { /** @ngInject **/ @@ -56,16 +57,13 @@ export class VariableEditorCtrl { } if (!$scope.current.name.match(/^\w+$/)) { - $scope.appEvent('alert-warning', [ - 'Validation', - 'Only word and digit characters are allowed in variable names', - ]); + appEvents.emit('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']); return false; } var sameName = _.find($scope.variables, { name: $scope.current.name }); if (sameName && sameName !== $scope.current) { - $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']); + appEvents.emit('alert-warning', ['Validation', 'Variable with the same name already exists']); return false; } @@ -73,7 +71,7 @@ export class VariableEditorCtrl { $scope.current.type === 'query' && $scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)')) ) { - $scope.appEvent('alert-warning', [ + appEvents.emit('alert-warning', [ 'Validation', 'Query cannot contain a reference to itself. Variable: $' + $scope.current.name, ]); @@ -96,11 +94,11 @@ export class VariableEditorCtrl { }; $scope.runQuery = function() { - return variableSrv.updateOptions($scope.current).then(null, function(err) { + return variableSrv.updateOptions($scope.current).catch(err => { if (err.data && err.data.message) { err.message = err.data.message; } - $scope.appEvent('alert-error', ['Templating', 'Template variables could not be initialized: ' + err.message]); + appEvents.emit('alert-error', ['Templating', 'Template variables could not be initialized: ' + err.message]); }); }; diff --git a/public/app/features/templating/specs/editor_ctrl.jest.ts b/public/app/features/templating/specs/editor_ctrl.jest.ts new file mode 100644 index 00000000000..29f292837d7 --- /dev/null +++ b/public/app/features/templating/specs/editor_ctrl.jest.ts @@ -0,0 +1,40 @@ +import { VariableEditorCtrl } from '../editor_ctrl'; + +let mockEmit; +jest.mock('app/core/app_events', () => { + mockEmit = jest.fn(); + return { + emit: mockEmit, + }; +}); + +describe('VariableEditorCtrl', () => { + let scope = { + runQuery: () => { + return Promise.resolve({}); + }, + }; + + describe('When running a variable query and the data source returns an error', () => { + beforeEach(() => { + const variableSrv = { + updateOptions: () => { + return Promise.reject({ + data: { message: 'error' }, + }); + }, + }; + + const ctrl = new VariableEditorCtrl(scope, {}, variableSrv, {}); + }); + + it('should emit an error', () => { + return scope.runQuery().then(res => { + expect(mockEmit).toBeCalled(); + expect(mockEmit.mock.calls[0][0]).toBe('alert-error'); + expect(mockEmit.mock.calls[0][1][0]).toBe('Templating'); + expect(mockEmit.mock.calls[0][1][1]).toBe('Template variables could not be initialized: error'); + }); + }); + }); +});