kbn.jest.ts 12 KB

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