kbn.jest.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import kbn from '../utils/kbn';
  2. import * as dateMath from '../utils/datemath';
  3. import moment from 'moment';
  4. describe('unit format menu', function() {
  5. var menu = kbn.getUnitFormats();
  6. menu.map(function(submenu) {
  7. describe('submenu ' + submenu.text, function() {
  8. it('should have a title', function() {
  9. expect(typeof submenu.text).toBe('string');
  10. });
  11. it('should have a submenu', function() {
  12. expect(Array.isArray(submenu.submenu)).toBe(true);
  13. });
  14. submenu.submenu.map(function(entry) {
  15. describe('entry ' + entry.text, function() {
  16. it('should have a title', function() {
  17. expect(typeof entry.text).toBe('string');
  18. });
  19. it('should have a format', function() {
  20. expect(typeof entry.value).toBe('string');
  21. });
  22. it('should have a valid format', function() {
  23. expect(typeof kbn.valueFormats[entry.value]).toBe('function');
  24. });
  25. });
  26. });
  27. });
  28. });
  29. });
  30. function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
  31. describe('value format: ' + desc, function() {
  32. it('should translate ' + value + ' as ' + result, function() {
  33. var scaledDecimals = tickDecimals - Math.floor(Math.log(tickSize) / Math.LN10);
  34. var str = kbn.valueFormats[desc](value, tickDecimals, scaledDecimals);
  35. expect(str).toBe(result);
  36. });
  37. });
  38. }
  39. describeValueFormat('ms', 0.0024, 0.0005, 4, '0.0024 ms');
  40. describeValueFormat('ms', 100, 1, 0, '100 ms');
  41. describeValueFormat('ms', 1250, 10, 0, '1.25 s');
  42. describeValueFormat('ms', 1250, 300, 0, '1.3 s');
  43. describeValueFormat('ms', 65150, 10000, 0, '1.1 min');
  44. describeValueFormat('ms', 6515000, 1500000, 0, '1.8 hour');
  45. describeValueFormat('ms', 651500000, 150000000, 0, '8 day');
  46. describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
  47. describeValueFormat('none', 0, 0, 2, '0');
  48. describeValueFormat('dB', 10, 1000, 2, '10.00 dB');
  49. describeValueFormat('percent', 0, 0, 0, '0%');
  50. describeValueFormat('percent', 53, 0, 1, '53.0%');
  51. describeValueFormat('percentunit', 0.0, 0, 0, '0%');
  52. describeValueFormat('percentunit', 0.278, 0, 1, '27.8%');
  53. describeValueFormat('percentunit', 1.0, 0, 0, '100%');
  54. describeValueFormat('currencyUSD', 7.42, 10000, 2, '$7.42');
  55. describeValueFormat('currencyUSD', 1532.82, 1000, 1, '$1.53K');
  56. describeValueFormat('currencyUSD', 18520408.7, 10000000, 0, '$19M');
  57. describeValueFormat('bytes', -1.57e308, -1.57e308, 2, 'NA');
  58. describeValueFormat('ns', 25, 1, 0, '25 ns');
  59. describeValueFormat('ns', 2558, 50, 0, '2.56 µs');
  60. describeValueFormat('ops', 123, 1, 0, '123 ops');
  61. describeValueFormat('rps', 456000, 1000, -1, '456K rps');
  62. describeValueFormat('rps', 123456789, 1000000, 2, '123.457M rps');
  63. describeValueFormat('wps', 789000000, 1000000, -1, '789M wps');
  64. describeValueFormat('iops', 11000000000, 1000000000, -1, '11B iops');
  65. describeValueFormat('s', 1.23456789e-7, 1e-10, 8, '123.5 ns');
  66. describeValueFormat('s', 1.23456789e-4, 1e-7, 5, '123.5 µs');
  67. describeValueFormat('s', 1.23456789e-3, 1e-6, 4, '1.235 ms');
  68. describeValueFormat('s', 1.23456789e-2, 1e-5, 3, '12.35 ms');
  69. describeValueFormat('s', 1.23456789e-1, 1e-4, 2, '123.5 ms');
  70. describeValueFormat('s', 24, 1, 0, '24 s');
  71. describeValueFormat('s', 246, 1, 0, '4.1 min');
  72. describeValueFormat('s', 24567, 100, 0, '6.82 hour');
  73. describeValueFormat('s', 24567890, 10000, 0, '40.62 week');
  74. describeValueFormat('s', 24567890000, 1000000, 0, '778.53 year');
  75. describeValueFormat('m', 24, 1, 0, '24 min');
  76. describeValueFormat('m', 246, 10, 0, '4.1 hour');
  77. describeValueFormat('m', 6545, 10, 0, '4.55 day');
  78. describeValueFormat('m', 24567, 100, 0, '2.44 week');
  79. describeValueFormat('m', 24567892, 10000, 0, '46.7 year');
  80. describeValueFormat('h', 21, 1, 0, '21 hour');
  81. describeValueFormat('h', 145, 1, 0, '6.04 day');
  82. describeValueFormat('h', 1234, 100, 0, '7.3 week');
  83. describeValueFormat('h', 9458, 1000, 0, '1.08 year');
  84. describeValueFormat('d', 3, 1, 0, '3 day');
  85. describeValueFormat('d', 245, 100, 0, '35 week');
  86. describeValueFormat('d', 2456, 10, 0, '6.73 year');
  87. describe('date time formats', function() {
  88. it('should format as iso date', function() {
  89. var str = kbn.valueFormats.dateTimeAsIso(1505634997920, 1);
  90. expect(str).toBe(moment(1505634997920).format('YYYY-MM-DD HH:mm:ss'));
  91. });
  92. it('should format as iso date and skip date when today', function() {
  93. var now = moment();
  94. var str = kbn.valueFormats.dateTimeAsIso(now.valueOf(), 1);
  95. expect(str).toBe(now.format('HH:mm:ss'));
  96. });
  97. it('should format as US date', function() {
  98. var str = kbn.valueFormats.dateTimeAsUS(1505634997920, 1);
  99. expect(str).toBe(moment(1505634997920).format('MM/DD/YYYY h:mm:ss a'));
  100. });
  101. it('should format as US date and skip date when today', function() {
  102. var now = moment();
  103. var str = kbn.valueFormats.dateTimeAsUS(now.valueOf(), 1);
  104. expect(str).toBe(now.format('h:mm:ss a'));
  105. });
  106. it('should format as from now with days', function() {
  107. var daysAgo = moment().add(-7, 'd');
  108. var str = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), 1);
  109. expect(str).toBe('7 days ago');
  110. });
  111. it('should format as from now with minutes', function() {
  112. var daysAgo = moment().add(-2, 'm');
  113. var str = kbn.valueFormats.dateTimeFromNow(daysAgo.valueOf(), 1);
  114. expect(str).toBe('2 minutes ago');
  115. });
  116. });
  117. describe('kbn.toFixed and negative decimals', function() {
  118. it('should treat as zero decimals', function() {
  119. var str = kbn.toFixed(186.123, -2);
  120. expect(str).toBe('186');
  121. });
  122. });
  123. describe('kbn ms format when scaled decimals is null do not use it', function() {
  124. it('should use specified decimals', function() {
  125. var str = kbn.valueFormats['ms'](10000086.123, 1, null);
  126. expect(str).toBe('2.8 hour');
  127. });
  128. });
  129. describe('kbn kbytes format when scaled decimals is null do not use it', function() {
  130. it('should use specified decimals', function() {
  131. var str = kbn.valueFormats['kbytes'](10000000, 3, null);
  132. expect(str).toBe('9.537 GiB');
  133. });
  134. });
  135. describe('kbn deckbytes format when scaled decimals is null do not use it', function() {
  136. it('should use specified decimals', function() {
  137. var str = kbn.valueFormats['deckbytes'](10000000, 3, null);
  138. expect(str).toBe('10.000 GB');
  139. });
  140. });
  141. describe('kbn roundValue', function() {
  142. it('should should handle null value', function() {
  143. var str = kbn.roundValue(null, 2);
  144. expect(str).toBe(null);
  145. });
  146. it('should round value', function() {
  147. var str = kbn.roundValue(200.877, 2);
  148. expect(str).toBe(200.88);
  149. });
  150. });
  151. describe('calculateInterval', function() {
  152. it('1h 100 resultion', function() {
  153. var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
  154. var res = kbn.calculateInterval(range, 100, null);
  155. expect(res.interval).toBe('30s');
  156. });
  157. it('10m 1600 resolution', function() {
  158. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  159. var res = kbn.calculateInterval(range, 1600, null);
  160. expect(res.interval).toBe('500ms');
  161. expect(res.intervalMs).toBe(500);
  162. });
  163. it('fixed user min interval', function() {
  164. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  165. var res = kbn.calculateInterval(range, 1600, '10s');
  166. expect(res.interval).toBe('10s');
  167. expect(res.intervalMs).toBe(10000);
  168. });
  169. it('short time range and user low limit', function() {
  170. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  171. var res = kbn.calculateInterval(range, 1600, '>10s');
  172. expect(res.interval).toBe('10s');
  173. });
  174. it('large time range and user low limit', function() {
  175. var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') };
  176. var res = kbn.calculateInterval(range, 1000, '>10s');
  177. expect(res.interval).toBe('20m');
  178. });
  179. it('10s 900 resolution and user low limit in ms', function() {
  180. var range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') };
  181. var res = kbn.calculateInterval(range, 900, '>15ms');
  182. expect(res.interval).toBe('15ms');
  183. });
  184. it('1d 1 resolution', function() {
  185. var range = { from: dateMath.parse('now-1d'), to: dateMath.parse('now') };
  186. var res = kbn.calculateInterval(range, 1, null);
  187. expect(res.interval).toBe('1d');
  188. expect(res.intervalMs).toBe(86400000);
  189. });
  190. it('86399s 1 resolution', function() {
  191. var range = {
  192. from: dateMath.parse('now-86390s'),
  193. to: dateMath.parse('now'),
  194. };
  195. var res = kbn.calculateInterval(range, 1, null);
  196. expect(res.interval).toBe('12h');
  197. expect(res.intervalMs).toBe(43200000);
  198. });
  199. });
  200. describe('hex', function() {
  201. it('positive integer', function() {
  202. var str = kbn.valueFormats.hex(100, 0);
  203. expect(str).toBe('64');
  204. });
  205. it('negative integer', function() {
  206. var str = kbn.valueFormats.hex(-100, 0);
  207. expect(str).toBe('-64');
  208. });
  209. it('null', function() {
  210. var str = kbn.valueFormats.hex(null, 0);
  211. expect(str).toBe('');
  212. });
  213. it('positive float', function() {
  214. var str = kbn.valueFormats.hex(50.52, 1);
  215. expect(str).toBe('32.8');
  216. });
  217. it('negative float', function() {
  218. var str = kbn.valueFormats.hex(-50.333, 2);
  219. expect(str).toBe('-32.547AE147AE14');
  220. });
  221. });
  222. describe('hex 0x', function() {
  223. it('positive integeter', function() {
  224. var str = kbn.valueFormats.hex0x(7999, 0);
  225. expect(str).toBe('0x1F3F');
  226. });
  227. it('negative integer', function() {
  228. var str = kbn.valueFormats.hex0x(-584, 0);
  229. expect(str).toBe('-0x248');
  230. });
  231. it('null', function() {
  232. var str = kbn.valueFormats.hex0x(null, 0);
  233. expect(str).toBe('');
  234. });
  235. it('positive float', function() {
  236. var str = kbn.valueFormats.hex0x(74.443, 3);
  237. expect(str).toBe('0x4A.716872B020C4');
  238. });
  239. it('negative float', function() {
  240. var str = kbn.valueFormats.hex0x(-65.458, 1);
  241. expect(str).toBe('-0x41.8');
  242. });
  243. });
  244. describe('duration', function() {
  245. it('null', function() {
  246. var str = kbn.toDuration(null, 0, 'millisecond');
  247. expect(str).toBe('');
  248. });
  249. it('0 milliseconds', function() {
  250. var str = kbn.toDuration(0, 0, 'millisecond');
  251. expect(str).toBe('0 milliseconds');
  252. });
  253. it('1 millisecond', function() {
  254. var str = kbn.toDuration(1, 0, 'millisecond');
  255. expect(str).toBe('1 millisecond');
  256. });
  257. it('-1 millisecond', function() {
  258. var str = kbn.toDuration(-1, 0, 'millisecond');
  259. expect(str).toBe('1 millisecond ago');
  260. });
  261. it('seconds', function() {
  262. var str = kbn.toDuration(1, 0, 'second');
  263. expect(str).toBe('1 second');
  264. });
  265. it('minutes', function() {
  266. var str = kbn.toDuration(1, 0, 'minute');
  267. expect(str).toBe('1 minute');
  268. });
  269. it('hours', function() {
  270. var str = kbn.toDuration(1, 0, 'hour');
  271. expect(str).toBe('1 hour');
  272. });
  273. it('days', function() {
  274. var str = kbn.toDuration(1, 0, 'day');
  275. expect(str).toBe('1 day');
  276. });
  277. it('weeks', function() {
  278. var str = kbn.toDuration(1, 0, 'week');
  279. expect(str).toBe('1 week');
  280. });
  281. it('months', function() {
  282. var str = kbn.toDuration(1, 0, 'month');
  283. expect(str).toBe('1 month');
  284. });
  285. it('years', function() {
  286. var str = kbn.toDuration(1, 0, 'year');
  287. expect(str).toBe('1 year');
  288. });
  289. it('decimal days', function() {
  290. var str = kbn.toDuration(1.5, 2, 'day');
  291. expect(str).toBe('1 day, 12 hours, 0 minutes');
  292. });
  293. it('decimal months', function() {
  294. var str = kbn.toDuration(1.5, 3, 'month');
  295. expect(str).toBe('1 month, 2 weeks, 1 day, 0 hours');
  296. });
  297. it('no decimals', function() {
  298. var str = kbn.toDuration(38898367008, 0, 'millisecond');
  299. expect(str).toBe('1 year');
  300. });
  301. it('1 decimal', function() {
  302. var str = kbn.toDuration(38898367008, 1, 'millisecond');
  303. expect(str).toBe('1 year, 2 months');
  304. });
  305. it('too many decimals', function() {
  306. var str = kbn.toDuration(38898367008, 20, 'millisecond');
  307. expect(str).toBe('1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds, 8 milliseconds');
  308. });
  309. it('floating point error', function() {
  310. var str = kbn.toDuration(36993906007, 8, 'millisecond');
  311. expect(str).toBe('1 year, 2 months, 0 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds');
  312. });
  313. });
  314. describe('volume', function() {
  315. it('1000m3', function() {
  316. var str = kbn.valueFormats['m3'](1000, 1, null);
  317. expect(str).toBe('1000.0 m3');
  318. });
  319. });
  320. describe('hh:mm:ss', function() {
  321. it('00:04:06', function() {
  322. var str = kbn.valueFormats['dthms'](246, 1);
  323. expect(str).toBe('00:04:06');
  324. });
  325. it('24:00:00', function() {
  326. var str = kbn.valueFormats['dthms'](86400, 1);
  327. expect(str).toBe('24:00:00');
  328. });
  329. it('6824413:53:20', function() {
  330. var str = kbn.valueFormats['dthms'](24567890000, 1);
  331. expect(str).toBe('6824413:53:20');
  332. });
  333. });