-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stringify/tests] seperated tests, added label template tests
- Loading branch information
Spencer Alger
committed
May 5, 2015
1 parent
a2b63fc
commit b3f6baf
Showing
7 changed files
with
293 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}; | ||
} | ||
}]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
}]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}); | ||
}); | ||
}]; | ||
}); |
Oops, something went wrong.