|
1 | | -import {getDowntimeFromDateFormatted, getUptimeFromDateFormatted} from '../dataFormatters'; |
| 1 | +import {EMPTY_DATA_PLACEHOLDER} from '../../constants'; |
| 2 | +import {UNBREAKABLE_GAP} from '../../utils'; |
| 3 | +import { |
| 4 | + formatUptimeInSeconds, |
| 5 | + getDowntimeFromDateFormatted, |
| 6 | + getUptimeFromDateFormatted, |
| 7 | +} from '../dataFormatters'; |
2 | 8 |
|
3 | 9 | describe('getUptimeFromDateFormatted', () => { |
4 | 10 | it('should calculate and format uptime', () => { |
5 | 11 | expect(getUptimeFromDateFormatted(3_600_000, 7_200_000)).toBe('1:00:00'); |
6 | 12 | }); |
7 | 13 | it('should return 0 if dateFrom after dateTo', () => { |
8 | | - expect(getUptimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0:00:00'); |
| 14 | + expect(getUptimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0s'); |
9 | 15 | }); |
10 | 16 | }); |
11 | 17 | describe('getDowntimeFromDateFormatted', () => { |
12 | 18 | it('should calculate and format downtime as -uptime', () => { |
13 | 19 | expect(getDowntimeFromDateFormatted(3_600_000, 7_200_000)).toBe('-1:00:00'); |
14 | 20 | }); |
15 | 21 | it('should not add sign if downtime is 0', () => { |
16 | | - expect(getDowntimeFromDateFormatted(3_600_000, 3_600_000)).toBe('0:00:00'); |
| 22 | + expect(getDowntimeFromDateFormatted(3_600_000, 3_600_000)).toBe('0s'); |
17 | 23 | }); |
18 | 24 | it('should return 0 if dateFrom after dateTo', () => { |
19 | | - expect(getDowntimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0:00:00'); |
| 25 | + expect(getDowntimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0s'); |
| 26 | + }); |
| 27 | +}); |
| 28 | +describe('formatUptimeInSeconds', () => { |
| 29 | + const M = 60; |
| 30 | + const H = 60 * M; |
| 31 | + const D = 24 * H; |
| 32 | + |
| 33 | + it('should return days if value is more than 24h', () => { |
| 34 | + expect(formatUptimeInSeconds(D)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00'); |
| 35 | + expect(formatUptimeInSeconds(D + H + M + 12)).toBe('1d' + UNBREAKABLE_GAP + '01:01:12'); |
| 36 | + expect(formatUptimeInSeconds(12 * D + 12 * H + 12 * M + 12)).toBe( |
| 37 | + '12d' + UNBREAKABLE_GAP + '12:12:12', |
| 38 | + ); |
| 39 | + expect(formatUptimeInSeconds(1234 * D + 12 * H + 12 * M + 12)).toBe( |
| 40 | + '1234d' + UNBREAKABLE_GAP + '12:12:12', |
| 41 | + ); |
| 42 | + }); |
| 43 | + it('should return hours if value is less than 24h', () => { |
| 44 | + expect(formatUptimeInSeconds(H + M + 12)).toBe('1:01:12'); |
| 45 | + expect(formatUptimeInSeconds(12 * H + 12 * M + 12)).toBe('12:12:12'); |
| 46 | + }); |
| 47 | + it('should return minutes if value is less than hour', () => { |
| 48 | + expect(formatUptimeInSeconds(M + 12)).toBe('1:12'); |
| 49 | + expect(formatUptimeInSeconds(12 * M + 2)).toBe('12:02'); |
| 50 | + }); |
| 51 | + it('should return second if value is less than hour', () => { |
| 52 | + expect(formatUptimeInSeconds(12)).toBe('12s'); |
| 53 | + expect(formatUptimeInSeconds(2)).toBe('2s'); |
| 54 | + }); |
| 55 | + it('should return empty placeholder on NaN', () => { |
| 56 | + expect(formatUptimeInSeconds(Number.NaN)).toBe(EMPTY_DATA_PLACEHOLDER); |
20 | 57 | }); |
21 | 58 | }); |
0 commit comments