]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.spec.ts
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / time-diff.service.spec.ts
1 import { TestBed } from '@angular/core/testing';
2
3 import { configureTestBed } from '../../../testing/unit-test-helper';
4 import { TimeDiffService } from './time-diff.service';
5
6 describe('TimeDiffService', () => {
7 let service: TimeDiffService;
8 const baseTime = new Date('2022-02-22T00:00:00');
9
10 configureTestBed({
11 providers: [TimeDiffService]
12 });
13
14 beforeEach(() => {
15 service = TestBed.get(TimeDiffService);
16 });
17
18 it('should be created', () => {
19 expect(service).toBeTruthy();
20 });
21
22 it('calculates a new date that happens after the given date', () => {
23 expect(service.calculateDate(new Date('2022-02-28T04:05:00'), '2h')).toEqual(
24 new Date('2022-02-28T06:05:00')
25 );
26 expect(service.calculateDate(baseTime, '15m')).toEqual(new Date('2022-02-22T00:15'));
27 expect(service.calculateDate(baseTime, '5d 23h')).toEqual(new Date('2022-02-27T23:00'));
28 });
29
30 it('calculates a new date that happens before the given date', () => {
31 expect(service.calculateDate(new Date('2022-02-22T02:00:00'), '2h', true)).toEqual(baseTime);
32 });
33
34 it('calculates the difference of two dates', () => {
35 expect(
36 service.calculateDuration(new Date('2022-02-22T00:45:00'), new Date('2022-02-22T02:00:00'))
37 ).toBe('1h 15m');
38 expect(service.calculateDuration(baseTime, new Date('2022-02-28T04:05:00'))).toBe('6d 4h 5m');
39 });
40
41 it('should return an empty string if time diff is less then a minute', () => {
42 const ts = 1568361327000;
43 expect(service.calculateDuration(new Date(ts), new Date(ts + 120))).toBe('');
44 });
45
46 describe('testing duration calculation in detail', () => {
47 const minutes = 60 * 1000;
48 const hours = 60 * minutes;
49 const days = 24 * hours;
50
51 it('should allow different writings', () => {
52 const expectDurationToBeMs = (duration: string, ms: number) =>
53 expect(service['getDurationMs'](duration)).toBe(ms);
54 expectDurationToBeMs('2h', 2 * hours);
55 expectDurationToBeMs('4 Days', 4 * days);
56 expectDurationToBeMs('3 minutes', 3 * minutes);
57 expectDurationToBeMs('4 Days 2h 3 minutes', 4 * days + 2 * hours + 3 * minutes);
58 expectDurationToBeMs('5d3h120m', 5 * days + 5 * hours);
59 });
60
61 it('should create duration string from ms', () => {
62 const expectMsToBeDuration = (ms: number, duration: string) =>
63 expect(service['getDuration'](ms)).toBe(duration);
64 expectMsToBeDuration(2 * hours, '2h');
65 expectMsToBeDuration(4 * days, '4d');
66 expectMsToBeDuration(3 * minutes, '3m');
67 expectMsToBeDuration(4 * days + 2 * hours + 3 * minutes, '4d 2h 3m');
68 expectMsToBeDuration(service['getDurationMs']('5d3h120m'), '5d 5h');
69 });
70 });
71 });