github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/util/formatDate.spec.ts (about) 1 import { 2 readableRange, 3 formatAsOBject, 4 toUnixTimestamp, 5 } from '@webapp/util/formatDate'; 6 import * as dateFns from 'date-fns'; 7 import timezoneMock from 'timezone-mock'; 8 9 describe('FormatDate', () => { 10 describe('readableRange', () => { 11 const cases = [ 12 ['now-1m', 'now', 'Last 1 minute'], 13 ['now-5m', 'now', 'Last 5 minutes'], 14 ['now-15m', 'now', 'Last 15 minutes'], 15 ['now-1h', 'now', 'Last 1 hour'], 16 ['now-24h', 'now', 'Last 24 hours'], 17 ['now-1d', 'now', 'Last 1 day'], 18 ['now-2d', 'now', 'Last 2 days'], 19 ['now-30d', 'now', 'Last 30 days'], 20 ['now-1M', 'now', 'Last 1 month'], 21 ['now-6M', 'now', 'Last 6 months'], 22 ['now-1y', 'now', 'Last 1 year'], 23 ['now-2y', 'now', 'Last 2 years'], 24 ['1624278889', '1640090089', '2021-06-21 12:34 PM - 2021-12-21 12:34 PM'], 25 [ 26 '1624278889000', 27 '1640090089000', 28 '2021-06-21 12:34 PM - 2021-12-21 12:34 PM', 29 ], 30 [ 31 '1624278889000000', 32 '1640090089000000', 33 '2021-06-21 12:34 PM - 2021-12-21 12:34 PM', 34 ], 35 [ 36 '1624278889000000000', 37 '1640090089000000000', 38 '2021-06-21 12:34 PM - 2021-12-21 12:34 PM', 39 ], 40 41 // Return nothing when mixing absolute/relative 42 ['1624278889000000000', 'now-1h', ''], 43 ['now-1h', '1624278889000000000', ''], 44 ]; 45 46 test.each(cases)( 47 'readableRange(%s, %s) should be %s', 48 (from, until, expected) => { 49 expect(readableRange(from, until, new Date().getTimezoneOffset())).toBe( 50 expected 51 ); 52 } 53 ); 54 }); 55 56 describe('formatAsOBject', () => { 57 const mockDate = new Date('2021-12-21T12:44:01.741Z'); 58 beforeEach(() => { 59 jest.useFakeTimers().setSystemTime(mockDate.getTime()); 60 }); 61 62 afterEach(() => { 63 jest.restoreAllMocks(); 64 65 jest.useRealTimers(); 66 }); 67 68 it('works with "now"', () => { 69 // TODO 70 // not entirely sure this case is even possible to happen in the code 71 expect(formatAsOBject('now')).toEqual(mockDate); 72 }); 73 74 it('works with "now-1h"', () => { 75 const got = formatAsOBject('now-1h'); 76 77 expect(got).toEqual(dateFns.subHours(mockDate, 1)); 78 }); 79 80 it('works with "now-30m"', () => { 81 const got = formatAsOBject('now-30m'); 82 83 expect(got).toEqual(dateFns.subMinutes(mockDate, 30)); 84 }); 85 86 it('works with "now-1m"', () => { 87 const got = formatAsOBject('now-1m'); 88 expect(got).toEqual(dateFns.subMinutes(mockDate, 1)); 89 }); 90 91 it('works with absolute timestamps in seconds', () => { 92 expect(formatAsOBject('1624192489')).toEqual( 93 new Date('2021-06-20T12:34:49.000Z') 94 ); 95 }); 96 97 it('works with absolute timestamps in milliseconds', () => { 98 expect(formatAsOBject('1624192489001')).toEqual( 99 new Date('2021-06-20T12:34:49.001Z') 100 ); 101 }); 102 103 it('works with absolute timestamps in microseconds', () => { 104 expect(formatAsOBject('1624192489001999')).toEqual( 105 new Date('2021-06-20T12:34:49.002Z') 106 ); 107 }); 108 109 it('works with absolute timestamps in nanoseconds', () => { 110 expect(formatAsOBject('1624192489001999999')).toEqual( 111 new Date('2021-06-20T12:34:49.002Z') 112 ); 113 }); 114 }); 115 116 describe('toUnixTimestamp', () => { 117 it('works', () => { 118 expect(toUnixTimestamp(new Date('2021-06-20T12:34:49.002Z'))).toBe( 119 1624192489 120 ); 121 122 // epoch 123 expect(toUnixTimestamp(new Date(0))).toBe(0); 124 }); 125 }); 126 });