mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
use to verify param substitutions, update tests to match
This commit is contained in:
parent
17fa030f97
commit
259857d23a
2 changed files with 42 additions and 12 deletions
|
@ -5,7 +5,7 @@ define(function (require) {
|
|||
var rison = require('utils/rison');
|
||||
var location = require('modules').get('kibana/url');
|
||||
|
||||
location.service('kbnUrl', function ($route, $location, $rootScope, globalState) {
|
||||
location.service('kbnUrl', function ($route, $location, $rootScope, globalState, $parse) {
|
||||
var self = this;
|
||||
self.reloading = false;
|
||||
|
||||
|
@ -47,9 +47,7 @@ define(function (require) {
|
|||
};
|
||||
|
||||
self.eval = function (url, paramObj) {
|
||||
if (!_.isObject(paramObj)) {
|
||||
return url;
|
||||
}
|
||||
paramObj = paramObj || {};
|
||||
|
||||
return parseUrlPrams(url, paramObj);
|
||||
};
|
||||
|
@ -65,10 +63,15 @@ define(function (require) {
|
|||
|
||||
function parseUrlPrams(url, paramObj) {
|
||||
return url.replace(/\{\{([^\}]+)\}\}/g, function (match, expr) {
|
||||
// remove filters
|
||||
var key = expr.split('|')[0].trim();
|
||||
|
||||
if (_.isUndefined(paramObj[key])) {
|
||||
throw new Error('Replacement failed, key not found: ' + key);
|
||||
// verify that the expression can be evaluated
|
||||
var p = $parse(key)(paramObj);
|
||||
|
||||
// if evaluation can't be made, throw
|
||||
if (_.isUndefined(p)) {
|
||||
throw new Error('Replacement failed, unresolved expression: ' + expr);
|
||||
}
|
||||
|
||||
// append uriescape filter if not included
|
||||
|
@ -76,7 +79,7 @@ define(function (require) {
|
|||
expr += '|uriescape';
|
||||
}
|
||||
|
||||
return $rootScope.$eval(expr, paramObj);
|
||||
return $parse(expr)(paramObj);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -191,18 +191,45 @@ define(function (require) {
|
|||
expect(locationUrlSpy.secondCall.args[0]).to.be(testUrl);
|
||||
});
|
||||
|
||||
it('should handle dot notation', function () {
|
||||
var url = '/some/thing/{{that.is.substituted}}';
|
||||
|
||||
kbnUrl.change(url, {
|
||||
that: {
|
||||
is: {
|
||||
substituted: 'test'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
expect($location.url()).to.be('/some/thing/test');
|
||||
});
|
||||
|
||||
it('should throw when params are missing', function () {
|
||||
var url = '/{{replace-me}}/{{but-not-me}}';
|
||||
var params = {
|
||||
'replace-me': 'done'
|
||||
};
|
||||
var url = '/{{replace_me}}';
|
||||
var params = {};
|
||||
|
||||
try {
|
||||
kbnUrl.change(url, params);
|
||||
throw new Error('this should not run');
|
||||
} catch (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.message).to.match(/but-not-me$/);
|
||||
console.log(err.message);
|
||||
expect(err.message).to.match(/replace_me/);
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw when filtered params are missing', function () {
|
||||
var url = '/{{replace_me|number}}';
|
||||
var params = {};
|
||||
|
||||
try {
|
||||
kbnUrl.change(url, params);
|
||||
throw new Error('this should not run');
|
||||
} catch (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
console.log(err.message);
|
||||
expect(err.message).to.match(/replace_me\|number/);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue