Skip to content

Commit

Permalink
[stringify/tests] seperated tests, added label template tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Alger committed May 5, 2015
1 parent a2b63fc commit b3f6baf
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 226 deletions.
2 changes: 1 addition & 1 deletion src/kibana/components/stringify/editors/url.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h4 class="hintbox-heading">
</ul>
</div>

<input ng-model="editor.formatParams.template" class="form-control">
<input ng-model="editor.formatParams.urlTemplate" class="form-control">
</div>

<div class="form-group">
Expand Down
4 changes: 2 additions & 2 deletions src/kibana/components/stringify/types/Url.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ define(function (require) {
Url.templateMatchRE = /{{([\s\S]+?)}}/g;
Url.paramDefaults = {
type: 'a',
template: null,
urlTemplate: null,
labelTemplate: null
};

Expand All @@ -45,7 +45,7 @@ define(function (require) {
];

Url.prototype._formatUrl = function (value) {
var template = this.param('template');
var template = this.param('urlTemplate');
if (!template) return value;

return this._compileTemplate(template)({
Expand Down
115 changes: 115 additions & 0 deletions test/unit/specs/components/stringify/_conformance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
define(function (require) {
var _ = require('lodash');

var fieldFormats;
var FieldFormat;
var config;

var formatIds = [
'bytes',
'date',
'ip',
'number',
'percent',
'string',
'url',
'_source'
];

return ['conformance', function () {

beforeEach(module('kibana'));
beforeEach(inject(function (Private, $injector) {
fieldFormats = Private(require('registry/field_formats'));
FieldFormat = Private(require('components/index_patterns/_field_format/FieldFormat'));
config = $injector.get('config');
}));

formatIds.forEach(function (id) {
var instance;
var Type;

beforeEach(function () {
Type = fieldFormats.getType(id);
instance = fieldFormats.getInstance(id);
});

describe(id + ' Type', function () {
it('has an id', function () {
expect(Type.id).to.be.a('string');
});

it('has a title', function () {
expect(Type.title).to.be.a('string');
});

it('declares compatible field formats as a string or array', function () {
expect(Type.fieldType).to.be.ok();
expect(_.isString(Type.fieldType) || _.isArray(Type.fieldType)).to.be(true);
});
});

describe(id + ' Instance', function () {
it('extends FieldFormat', function () {
expect(instance).to.be.a(FieldFormat);
});
});
});

it('registers all of the fieldFormats', function () {
expect(_.difference(fieldFormats.raw, formatIds.map(fieldFormats.getType))).to.eql([]);
});

describe('Bytes format', basicPatternTests('bytes', require('numeral')));
describe('Percent Format', basicPatternTests('percent', require('numeral')));
describe('Date Format', basicPatternTests('date', require('moment')));

describe('Number Format', function () {
basicPatternTests('number', require('numeral'))();

it('tries to parse strings', function () {
var number = new (fieldFormats.getType('number'))({ pattern: '0.0b' });
expect(number.convert(123.456)).to.be('123.5B');
expect(number.convert('123.456')).to.be('123.5B');
});

});

function basicPatternTests(id, lib) {
var confKey = id === 'date' ? 'dateFormat' : 'format:' + id + ':defaultPattern';

return function () {
it('converts using the format:' + id + ':defaultPattern config', function () {
var inst = fieldFormats.getInstance(id);
[
'0b',
'0 b',
'0.[000] b',
'0.[000]b',
'0.[0]b'
].forEach(function (pattern) {
var num = _.random(-10000, 10000, true);
config.set(confKey, pattern);
expect(inst.convert(num)).to.be(lib(num).format(pattern));
});
});

it('uses the pattern param if available', function () {
var num = _.random(-10000, 10000, true);
var defFormat = '0b';
var customFormat = '0.00000%';

config.set(confKey, defFormat);
var defInst = fieldFormats.getInstance(id);

var Type = fieldFormats.getType(id);
var customInst = new Type({ pattern: customFormat });

expect(defInst.convert(num)).to.not.be(customInst.convert(num));
expect(defInst.convert(num)).to.be(lib(num).format(defFormat));
expect(customInst.convert(num)).to.be(lib(num).format(customFormat));
});
};
}
}];
});
16 changes: 16 additions & 0 deletions test/unit/specs/components/stringify/_ip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
define(function (require) {
return ['IP Address Format', function () {
var fieldFormats;

beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
fieldFormats = Private(require('registry/field_formats'));
}));

it('convers a value from a decimal to a string', function () {
var ip = fieldFormats.getInstance('ip');
expect(ip.convert(1186489492)).to.be('70.184.100.148');
});

}];
});
34 changes: 34 additions & 0 deletions test/unit/specs/components/stringify/_source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
define(function (require) {
return ['_source formatting', function () {
var $ = require('jquery');
var _ = require('lodash');

var fieldFormats;

beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
fieldFormats = Private(require('registry/field_formats'));
}));

describe('Source format', function () {
var indexPattern;
var hits;
var format;
var convertHtml;

beforeEach(inject(function (Private) {
indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern'));
hits = Private(require('fixtures/hits'));
format = fieldFormats.getInstance('_source');
convertHtml = format.getConverterFor('html');
}));

it('uses the _source, field, and hit to create a <dl>', function () {
var hit = _.first(hits);
var $dl = $(convertHtml(hit._source, indexPattern.fields.byName._source, hit));
expect($dl.is('dl')).to.be.ok();
expect($dl.find('dt')).to.have.length(_.keys(indexPattern.flattenHit(hit)).length);
});
});
}];
});
119 changes: 119 additions & 0 deletions test/unit/specs/components/stringify/_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
define(function (require) {
return ['Url Format', function () {
var $ = require('jquery');
var fieldFormats;

beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
fieldFormats = Private(require('registry/field_formats'));
}));

describe('Url Format', function () {
var Url;

beforeEach(function () {
Url = fieldFormats.getType('url');
});

it('ouputs a simple <a> tab by default', function () {
var url = new Url();

var $a = $(url.convert('http://elastic.co', 'html'));
expect($a.is('a')).to.be(true);
expect($a.size()).to.be(1);
expect($a.attr('href')).to.be('http://elastic.co');
expect($a.attr('target')).to.be('_blank');
expect($a.children().size()).to.be(0);
});

it('outputs an <image> if type === "img"', function () {
var url = new Url({ type: 'img' });

var $img = $(url.convert('http://elastic.co', 'html'));
expect($img.is('img')).to.be(true);
expect($img.attr('src')).to.be('http://elastic.co');
});

describe('url template', function () {
it('accepts a template', function () {
var url = new Url({ urlTemplate: 'url: {{ value }}' });
var $a = $(url.convert('url', 'html'));
expect($a.is('a')).to.be(true);
expect($a.size()).to.be(1);
expect($a.attr('href')).to.be('url: url');
expect($a.attr('target')).to.be('_blank');
expect($a.children().size()).to.be(0);
});

it('only outputs the url if the contentType === "text"', function () {
var url = new Url();
expect(url.convert('url', 'text')).to.be('url');
});
});

describe('label template', function () {
it('accepts a template', function () {
var url = new Url({ labelTemplate: 'extension: {{ value }}' });
var $a = $(url.convert('php', 'html'));
expect($a.is('a')).to.be(true);
expect($a.size()).to.be(1);
expect($a.attr('href')).to.be('php');
expect($a.html()).to.be('extension: php');
});

it('uses the label template for text formating', function () {
var url = new Url({ labelTemplate: 'external {{value }}'});
expect(url.convert('url', 'text')).to.be('external url');
});

it('can use the raw value', function () {
var url = new Url({
labelTemplate: 'external {{value}}'
});
expect(url.convert('url?', 'text')).to.be('external url?');
});

it('can use the url', function () {
var url = new Url({
urlTemplate: 'http://google.com/{{value}}',
labelTemplate: 'external {{url}}'
});
expect(url.convert('url?', 'text')).to.be('external http://google.com/url%3F');
});
});

describe('templating', function () {
it('ignores unknown variables', function () {
var url = new Url({ urlTemplate: '{{ not really a var }}' });
expect(url.convert('url', 'text')).to.be('');
});

it('does not allow executing code in variable expressions', function () {
window.SHOULD_NOT_BE_TRUE = false;
var url = new Url({ urlTemplate: '{{ (window.SHOULD_NOT_BE_TRUE = true) && value }}' });
expect(url.convert('url', 'text')).to.be('');
});

describe('', function () {
before(function () {
Object.prototype.cantStopMeNow = {
toString: function () {
return 'fail';
},
cantStopMeNow: null
};
});

it('does not get values from the prototype chain', function () {
var url = new Url({ urlTemplate: '{{ cantStopMeNow }}' });
expect(url.convert('url', 'text')).to.be('');
});

after(function () {
delete Object.prototype.cantStopMeNow;
});
});
});
});
}];
});
Loading

0 comments on commit b3f6baf

Please sign in to comment.