kbn.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { DecimalCount, TimeZone } from '@grafana/data';
  2. import kbn from './kbn';
  3. // Most of the methods in this file are deprecated
  4. // Stub the deprecation warning here to prevent polluting the test output
  5. jest.mock('@grafana/data', () => ({
  6. ...jest.requireActual('@grafana/data'),
  7. deprecationWarning: () => {},
  8. }));
  9. interface ValueFormatTest {
  10. id: string;
  11. decimals?: DecimalCount;
  12. timeZone?: TimeZone;
  13. value: number;
  14. result: string;
  15. }
  16. const formatTests: ValueFormatTest[] = [
  17. // Currency
  18. { id: 'currencyUSD', decimals: 2, value: 1532.82, result: '$1.53K' },
  19. { id: 'currencyKRW', decimals: 2, value: 1532.82, result: '₩1.53K' },
  20. { id: 'currencyIDR', decimals: 2, value: 1532.82, result: 'Rp1.53K' },
  21. // Typical
  22. { id: 'ms', decimals: 4, value: 0.0024, result: '0.0024 ms' },
  23. { id: 'ms', decimals: 0, value: 100, result: '100 ms' },
  24. { id: 'ms', decimals: 2, value: 1250, result: '1.25 s' },
  25. { id: 'ms', decimals: 1, value: 10000086.123, result: '2.8 hour' },
  26. { id: 'ms', decimals: 0, value: 1200, result: '1 s' },
  27. { id: 'short', decimals: 0, value: 98765, result: '99 K' },
  28. { id: 'short', decimals: 0, value: 9876543, result: '10 Mil' },
  29. { id: 'kbytes', decimals: 3, value: 10000000, result: '9.537 GiB' },
  30. { id: 'deckbytes', decimals: 3, value: 10000000, result: '10.000 GB' },
  31. { id: 'megwatt', decimals: 3, value: 1000, result: '1.000 GW' },
  32. { id: 'kohm', decimals: 3, value: 1000, result: '1.000 MΩ' },
  33. { id: 'Mohm', decimals: 3, value: 1000, result: '1.000 GΩ' },
  34. { id: 'farad', decimals: 3, value: 1000, result: '1.000 kF' },
  35. { id: 'µfarad', decimals: 3, value: 1000, result: '1.000 mF' },
  36. { id: 'nfarad', decimals: 3, value: 1000, result: '1.000 µF' },
  37. { id: 'pfarad', decimals: 3, value: 1000, result: '1.000 nF' },
  38. { id: 'ffarad', decimals: 3, value: 1000, result: '1.000 pF' },
  39. { id: 'henry', decimals: 3, value: 1000, result: '1.000 kH' },
  40. { id: 'mhenry', decimals: 3, value: 1000, result: '1.000 H' },
  41. { id: 'µhenry', decimals: 3, value: 1000, result: '1.000 mH' },
  42. ];
  43. describe('Chcek KBN value formats', () => {
  44. for (const test of formatTests) {
  45. describe(`value format: ${test.id}`, () => {
  46. it(`should translate ${test.value} as ${test.result}`, () => {
  47. const result = kbn.valueFormats[test.id](test.value, test.decimals);
  48. expect(result).toBe(test.result);
  49. });
  50. });
  51. }
  52. });
  53. describe('describe_interval', () => {
  54. it('falls back to seconds if input is a number', () => {
  55. expect(kbn.describeInterval('123')).toEqual({
  56. sec: 1,
  57. type: 's',
  58. count: 123,
  59. });
  60. });
  61. it('parses a valid time unt string correctly', () => {
  62. expect(kbn.describeInterval('123h')).toEqual({
  63. sec: 3600,
  64. type: 'h',
  65. count: 123,
  66. });
  67. });
  68. it('fails if input is invalid', () => {
  69. expect(() => kbn.describeInterval('123xyz')).toThrow();
  70. expect(() => kbn.describeInterval('xyz')).toThrow();
  71. });
  72. });
  73. describe('addSlashes', () => {
  74. it('properly escapes backslashes, single-quotes, double-quotes and the number zero', () => {
  75. expect(kbn.addSlashes('this is a \'test\' with "quotes" backslashes (\\) and zero (0)')).toEqual(
  76. 'this is a \\\'test\\\' with \\"quotes\\" backslashes (\\\\) and zero (\\0)'
  77. );
  78. });
  79. });