mirror of https://github.com/grafana/grafana
feat(templating): more work on new variable handling code, #6048
parent
4188c46f83
commit
5ce3e40cc9
@ -0,0 +1,34 @@ |
|||||||
|
///<reference path="../../headers/common.d.ts" />
|
||||||
|
|
||||||
|
import _ from 'lodash'; |
||||||
|
import {Variable} from './variable'; |
||||||
|
import {VariableSrv, variableConstructorMap} from './variable_srv'; |
||||||
|
|
||||||
|
export class ConstantVariable implements Variable { |
||||||
|
query: string; |
||||||
|
options: any[]; |
||||||
|
|
||||||
|
/** @ngInject */ |
||||||
|
constructor(private model, private variableSrv) { |
||||||
|
_.extend(this, model); |
||||||
|
} |
||||||
|
|
||||||
|
setValue(option) { |
||||||
|
this.variableSrv.setOptionAsCurrent(this, option); |
||||||
|
} |
||||||
|
|
||||||
|
updateOptions() { |
||||||
|
this.options = [{text: this.query.trim(), value: this.query.trim()}]; |
||||||
|
this.setValue(this.options[0]); |
||||||
|
} |
||||||
|
|
||||||
|
dependsOn(variable) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
setValueFromUrl(urlValue) { |
||||||
|
return this.variableSrv.setOptionFromUrl(this, urlValue); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
variableConstructorMap['constant'] = ConstantVariable; |
||||||
@ -0,0 +1,88 @@ |
|||||||
|
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common'; |
||||||
|
|
||||||
|
import moment from 'moment'; |
||||||
|
import helpers from 'test/specs/helpers'; |
||||||
|
import '../all'; |
||||||
|
|
||||||
|
describe('VariableSrv Init', function() { |
||||||
|
var ctx = new helpers.ControllerTestContext(); |
||||||
|
|
||||||
|
beforeEach(angularMocks.module('grafana.core')); |
||||||
|
beforeEach(angularMocks.module('grafana.controllers')); |
||||||
|
beforeEach(angularMocks.module('grafana.services')); |
||||||
|
|
||||||
|
beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location'])); |
||||||
|
beforeEach(angularMocks.inject(($rootScope, $q, $location, $injector) => { |
||||||
|
ctx.$q = $q; |
||||||
|
ctx.$rootScope = $rootScope; |
||||||
|
ctx.$location = $location; |
||||||
|
ctx.variableSrv = $injector.get('variableSrv'); |
||||||
|
ctx.variableSrv.init({templating: {list: []}}); |
||||||
|
ctx.$rootScope.$digest(); |
||||||
|
})); |
||||||
|
|
||||||
|
function describeInitSceneario(desc, fn) { |
||||||
|
describe(desc, function() { |
||||||
|
var scenario: any = { |
||||||
|
urlParams: {}, |
||||||
|
setup: setupFn => { |
||||||
|
scenario.setupFn = setupFn; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
beforeEach(function() { |
||||||
|
scenario.setupFn(); |
||||||
|
var ds: any = {}; |
||||||
|
ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult)); |
||||||
|
ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds)); |
||||||
|
ctx.datasourceSrv.getMetricSources = sinon.stub().returns(scenario.metricSources); |
||||||
|
|
||||||
|
ctx.$location.search = sinon.stub().returns(scenario.urlParams); |
||||||
|
|
||||||
|
ctx.dashboard = {templating: {list: scenario.variables}}; |
||||||
|
ctx.variableSrv.init(ctx.dashboard); |
||||||
|
ctx.$rootScope.$digest(); |
||||||
|
|
||||||
|
scenario.variables = ctx.variableSrv.variables; |
||||||
|
}); |
||||||
|
|
||||||
|
fn(scenario); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
describeInitSceneario('when setting query variable via url', scenario => { |
||||||
|
scenario.setup(() => { |
||||||
|
scenario.variables = [{ |
||||||
|
name: 'apps', |
||||||
|
type: 'query', |
||||||
|
current: {text: "test", value: "test"}, |
||||||
|
options: [{text: "test", value: "test"}] |
||||||
|
}]; |
||||||
|
scenario.urlParams["var-apps"] = "new"; |
||||||
|
}); |
||||||
|
|
||||||
|
it('should update current value', () => { |
||||||
|
expect(scenario.variables[0].current.value).to.be("new"); |
||||||
|
expect(scenario.variables[0].current.text).to.be("new"); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describeInitSceneario('when setting custom variable via url', scenario => { |
||||||
|
scenario.setup(() => { |
||||||
|
scenario.variables = [{ |
||||||
|
name: 'apps', |
||||||
|
type: 'custom', |
||||||
|
current: {text: "test", value: "test"}, |
||||||
|
options: [{text: "test", value: "test"}] |
||||||
|
}]; |
||||||
|
scenario.urlParams["var-apps"] = "new"; |
||||||
|
}); |
||||||
|
|
||||||
|
it('should update current value', () => { |
||||||
|
expect(scenario.variables[0].current.value).to.be("new"); |
||||||
|
expect(scenario.variables[0].current.text).to.be("new"); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
Loading…
Reference in new issue