Commit 034ca696 by Tobias Skarhed

Add mock constructor

parent 7f4723a9
...@@ -19,8 +19,14 @@ describe('VariableSrv', function() { ...@@ -19,8 +19,14 @@ describe('VariableSrv', function() {
}, },
templateSrv: { templateSrv: {
setGrafanaVariable: jest.fn(), setGrafanaVariable: jest.fn(),
init: () => {}, init: vars => {
this.variables = vars;
},
updateTemplateData: () => {}, updateTemplateData: () => {},
replace: str =>
str.replace(this.regex, match => {
return match;
}),
}, },
$location: { $location: {
search: () => {}, search: () => {},
...@@ -54,17 +60,20 @@ describe('VariableSrv', function() { ...@@ -54,17 +60,20 @@ describe('VariableSrv', function() {
scenario.setupFn(); scenario.setupFn();
var ds: any = {}; var ds: any = {};
ds.metricFindQuery = Promise.resolve(scenario.queryResult); ds.metricFindQuery = () => Promise.resolve(scenario.queryResult);
ctx.variableSrv = new VariableSrv(ctx.$rootScope, $q, ctx.$location, ctx.$injector, ctx.templateSrv); ctx.variableSrv = new VariableSrv(ctx.$rootScope, $q, ctx.$location, ctx.$injector, ctx.templateSrv);
ctx.variableSrv.timeSrv = ctx.timeSrv; ctx.variableSrv.timeSrv = ctx.timeSrv;
console.log(ctx.variableSrv.timeSrv); ctx.datasourceSrv = {
ctx.variableSrv.datasourceSrv = { get: () => Promise.resolve(ds),
get: Promise.resolve(ds),
getMetricSources: () => scenario.metricSources, getMetricSources: () => scenario.metricSources,
}; };
ctx.$injector.instantiate = (ctr, model) => {
return getVarMockConstructor(ctr, model, ctx);
};
ctx.variableSrv.init({ ctx.variableSrv.init({
templating: { list: [] }, templating: { list: [] },
updateSubmenuVisibility: () => {}, updateSubmenuVisibility: () => {},
...@@ -74,7 +83,6 @@ describe('VariableSrv', function() { ...@@ -74,7 +83,6 @@ describe('VariableSrv', function() {
ctx.variableSrv.addVariable(scenario.variable); ctx.variableSrv.addVariable(scenario.variable);
ctx.variableSrv.updateOptions(scenario.variable); ctx.variableSrv.updateOptions(scenario.variable);
// ctx.$rootScope.$digest();
}); });
fn(scenario); fn(scenario);
...@@ -128,17 +136,17 @@ describe('VariableSrv', function() { ...@@ -128,17 +136,17 @@ describe('VariableSrv', function() {
}); });
it('should set $__auto_interval_test', function() { it('should set $__auto_interval_test', function() {
var call = ctx.templateSrv.setGrafanaVariable.firstCall; var call = ctx.templateSrv.setGrafanaVariable.mock.calls[0];
expect(call.args[0]).toBe('$__auto_interval_test'); expect(call[0]).toBe('$__auto_interval_test');
expect(call.args[1]).toBe('12h'); expect(call[1]).toBe('12h');
}); });
// updateAutoValue() gets called twice: once directly once via VariableSrv.validateVariableSelectionState() // updateAutoValue() gets called twice: once directly once via VariableSrv.validateVariableSelectionState()
// So use lastCall instead of a specific call number // So use lastCall instead of a specific call number
it('should set $__auto_interval', function() { it('should set $__auto_interval', function() {
var call = ctx.templateSrv.setGrafanaVariable.lastCall; var call = ctx.templateSrv.setGrafanaVariable.mock.calls.pop();
expect(call.args[0]).toBe('$__auto_interval'); expect(call[0]).toBe('$__auto_interval');
expect(call.args[1]).toBe('12h'); expect(call[1]).toBe('12h');
}); });
}); });
...@@ -547,7 +555,7 @@ describe('VariableSrv', function() { ...@@ -547,7 +555,7 @@ describe('VariableSrv', function() {
ctx.variableSrv.updateOptions(variable1); ctx.variableSrv.updateOptions(variable1);
ctx.variableSrv.updateOptions(variable2); ctx.variableSrv.updateOptions(variable2);
ctx.$rootScope.$digest(); // ctx.$rootScope.$digest();
}); });
it('should update options array', function() { it('should update options array', function() {
...@@ -568,7 +576,7 @@ describe('VariableSrv', function() { ...@@ -568,7 +576,7 @@ describe('VariableSrv', function() {
// So check that all calls are valid rather than expect a specific number and/or ordering of calls // So check that all calls are valid rather than expect a specific number and/or ordering of calls
for (var i = 0; i < ctx.templateSrv.setGrafanaVariable.mock.calls.length; i++) { for (var i = 0; i < ctx.templateSrv.setGrafanaVariable.mock.calls.length; i++) {
var call = ctx.templateSrv.setGrafanaVariable.mock.calls[i]; var call = ctx.templateSrv.setGrafanaVariable.mock.calls[i];
switch (call.args[0]) { switch (call[0]) {
case '$__auto_interval_variable1': case '$__auto_interval_variable1':
expect(call[1]).toBe('12h'); expect(call[1]).toBe('12h');
variable1Set = true; variable1Set = true;
...@@ -586,10 +594,25 @@ describe('VariableSrv', function() { ...@@ -586,10 +594,25 @@ describe('VariableSrv', function() {
break; break;
} }
} }
expect(variable1Set).toBe.equal(true); expect(variable1Set).toEqual(true);
expect(variable2Set).toBe.equal(true); expect(variable2Set).toEqual(true);
expect(legacySet).toBe.equal(true); expect(legacySet).toEqual(true);
expect(unknownSet).toBe.equal(false); expect(unknownSet).toEqual(false);
}); });
}); });
}); });
function getVarMockConstructor(variable, model, ctx) {
switch (model.model.type) {
case 'datasource':
return new variable(model.model, ctx.datasourceSrv, ctx.variableSrv, ctx.templateSrv);
case 'query':
return new variable(model.model, ctx.datasourceSrv, ctx.templateSrv, ctx.variableSrv);
case 'interval':
return new variable(model.model, ctx.timeSrv, ctx.templateSrv, ctx.variableSrv);
case 'custom':
return new variable(model.model, ctx.variableSrv);
default:
return new variable(model.model);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment