summaryrefslogtreecommitdiff
path: root/spec/javascripts/pretty_time_spec.js
blob: 158cd76dd13ae86d4253f5bf70dd9777009a10df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { parseSeconds, abbreviateTime, stringifyTime } from '~/lib/utils/pretty_time';

function assertTimeUnits(obj, minutes, hours, days, weeks) {
  expect(obj.minutes).toBe(minutes);
  expect(obj.hours).toBe(hours);
  expect(obj.days).toBe(days);
  expect(obj.weeks).toBe(weeks);
}

describe('prettyTime methods', () => {
  describe('parseSeconds', () => {
    it('should correctly parse a negative value', () => {
      const zeroSeconds = parseSeconds(-1000);

      assertTimeUnits(zeroSeconds, 16, 0, 0, 0);
    });

    it('should correctly parse a zero value', () => {
      const zeroSeconds = parseSeconds(0);

      assertTimeUnits(zeroSeconds, 0, 0, 0, 0);
    });

    it('should correctly parse a small non-zero second values', () => {
      const subOneMinute = parseSeconds(10);
      const aboveOneMinute = parseSeconds(100);
      const manyMinutes = parseSeconds(1000);

      assertTimeUnits(subOneMinute, 0, 0, 0, 0);
      assertTimeUnits(aboveOneMinute, 1, 0, 0, 0);
      assertTimeUnits(manyMinutes, 16, 0, 0, 0);
    });

    it('should correctly parse large second values', () => {
      const aboveOneHour = parseSeconds(4800);
      const aboveOneDay = parseSeconds(110000);
      const aboveOneWeek = parseSeconds(25000000);

      assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
      assertTimeUnits(aboveOneDay, 33, 6, 3, 0);
      assertTimeUnits(aboveOneWeek, 26, 0, 3, 173);
    });

    it('should correctly accept a custom param for hoursPerDay', () => {
      const config = { hoursPerDay: 24 };

      const aboveOneHour = parseSeconds(4800, config);
      const aboveOneDay = parseSeconds(110000, config);
      const aboveOneWeek = parseSeconds(25000000, config);

      assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
      assertTimeUnits(aboveOneDay, 33, 6, 1, 0);
      assertTimeUnits(aboveOneWeek, 26, 8, 4, 57);
    });

    it('should correctly accept a custom param for daysPerWeek', () => {
      const config = { daysPerWeek: 7 };

      const aboveOneHour = parseSeconds(4800, config);
      const aboveOneDay = parseSeconds(110000, config);
      const aboveOneWeek = parseSeconds(25000000, config);

      assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
      assertTimeUnits(aboveOneDay, 33, 6, 3, 0);
      assertTimeUnits(aboveOneWeek, 26, 0, 0, 124);
    });

    it('should correctly accept custom params for daysPerWeek and hoursPerDay', () => {
      const config = { daysPerWeek: 55, hoursPerDay: 14 };

      const aboveOneHour = parseSeconds(4800, config);
      const aboveOneDay = parseSeconds(110000, config);
      const aboveOneWeek = parseSeconds(25000000, config);

      assertTimeUnits(aboveOneHour, 20, 1, 0, 0);
      assertTimeUnits(aboveOneDay, 33, 2, 2, 0);
      assertTimeUnits(aboveOneWeek, 26, 0, 1, 9);
    });
  });

  describe('stringifyTime', () => {
    it('should stringify values with all non-zero units', () => {
      const timeObject = {
        weeks: 1,
        days: 4,
        hours: 7,
        minutes: 20,
      };

      const timeString = stringifyTime(timeObject);

      expect(timeString).toBe('1w 4d 7h 20m');
    });

    it('should stringify values with some non-zero units', () => {
      const timeObject = {
        weeks: 0,
        days: 4,
        hours: 0,
        minutes: 20,
      };

      const timeString = stringifyTime(timeObject);

      expect(timeString).toBe('4d 20m');
    });

    it('should stringify values with no non-zero units', () => {
      const timeObject = {
        weeks: 0,
        days: 0,
        hours: 0,
        minutes: 0,
      };

      const timeString = stringifyTime(timeObject);

      expect(timeString).toBe('0m');
    });
  });

  describe('abbreviateTime', () => {
    it('should abbreviate stringified times for weeks', () => {
      const fullTimeString = '1w 3d 4h 5m';

      expect(abbreviateTime(fullTimeString)).toBe('1w');
    });

    it('should abbreviate stringified times for non-weeks', () => {
      const fullTimeString = '0w 3d 4h 5m';

      expect(abbreviateTime(fullTimeString)).toBe('3d');
    });
  });
});