Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timeago_utility_spec.js « datetime « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44db4cf88a2aa3a4f8f41a41e9adfa002d6ff55e (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { DATE_ONLY_FORMAT } from '~/lib/utils/datetime/constants';
import { getTimeago, localTimeAgo, timeFor, duration } from '~/lib/utils/datetime/timeago_utility';
import { s__ } from '~/locale';
import '~/commons/bootstrap';

describe('TimeAgo utils', () => {
  describe('getTimeago', () => {
    describe('with User Setting timeDisplayRelative: true', () => {
      beforeEach(() => {
        window.gon = { time_display_relative: true };
      });

      it.each([
        [new Date().toISOString(), 'just now'],
        [new Date().getTime(), 'just now'],
        [new Date(), 'just now'],
        [null, 'just now'],
      ])('formats date `%p` as `%p`', (date, result) => {
        expect(getTimeago().format(date)).toEqual(result);
      });
    });

    describe('with User Setting timeDisplayRelative: false', () => {
      beforeEach(() => {
        window.gon = { time_display_relative: false };
      });

      const defaultFormatExpectations = [
        [new Date().toISOString(), 'Jul 6, 2020, 12:00 AM'],
        [new Date(), 'Jul 6, 2020, 12:00 AM'],
        [new Date().getTime(), 'Jul 6, 2020, 12:00 AM'],
        // Slightly different behaviour when `null` is passed :see_no_evil`
        [null, 'Jan 1, 1970, 12:00 AM'],
      ];

      it.each(defaultFormatExpectations)('formats date `%p` as `%p`', (date, result) => {
        expect(getTimeago().format(date)).toEqual(result);
      });

      describe('with unknown format', () => {
        it.each(defaultFormatExpectations)(
          'uses default format and formats date `%p` as `%p`',
          (date, result) => {
            expect(getTimeago('non_existent').format(date)).toEqual(result);
          },
        );
      });

      describe('with DATE_ONLY_FORMAT', () => {
        it.each([
          [new Date().toISOString(), 'Jul 6, 2020'],
          [new Date(), 'Jul 6, 2020'],
          [new Date().getTime(), 'Jul 6, 2020'],
          [null, 'Jan 1, 1970'],
        ])('formats date `%p` as `%p`', (date, result) => {
          expect(getTimeago(DATE_ONLY_FORMAT).format(date)).toEqual(result);
        });
      });
    });
  });

  describe('timeFor', () => {
    it('returns localize `past due` when in past', () => {
      const date = new Date();
      date.setFullYear(date.getFullYear() - 1);

      expect(timeFor(date)).toBe(s__('Timeago|Past due'));
    });

    it('returns localized remaining time when in the future', () => {
      const date = new Date();
      date.setFullYear(date.getFullYear() + 1);

      // Add a day to prevent a transient error. If date is even 1 second
      // short of a full year, timeFor will return '11 months remaining'
      date.setDate(date.getDate() + 1);

      expect(timeFor(date)).toBe(s__('Timeago|1 year remaining'));
    });
  });

  describe('duration', () => {
    const ONE_DAY = 24 * 60 * 60;

    it.each`
      secs                 | formatted
      ${0}                 | ${'0 seconds'}
      ${30}                | ${'30 seconds'}
      ${59}                | ${'59 seconds'}
      ${60}                | ${'1 minute'}
      ${-60}               | ${'1 minute'}
      ${2 * 60}            | ${'2 minutes'}
      ${60 * 60}           | ${'1 hour'}
      ${2 * 60 * 60}       | ${'2 hours'}
      ${ONE_DAY}           | ${'1 day'}
      ${2 * ONE_DAY}       | ${'2 days'}
      ${7 * ONE_DAY}       | ${'1 week'}
      ${14 * ONE_DAY}      | ${'2 weeks'}
      ${31 * ONE_DAY}      | ${'1 month'}
      ${61 * ONE_DAY}      | ${'2 months'}
      ${365 * ONE_DAY}     | ${'1 year'}
      ${365 * 2 * ONE_DAY} | ${'2 years'}
    `('formats $secs as "$formatted"', ({ secs, formatted }) => {
      const ms = secs * 1000;

      expect(duration(ms)).toBe(formatted);
    });

    // `duration` can be used to format Rails month durations.
    // Ensure formatting for quantities such as `2.months.to_i`
    // based on ActiveSupport::Duration::SECONDS_PER_MONTH.
    // See: https://api.rubyonrails.org/classes/ActiveSupport/Duration.html
    const SECONDS_PER_MONTH = 2629746; // 1.month.to_i

    it.each`
      duration      | secs                     | formatted
      ${'1.month'}  | ${SECONDS_PER_MONTH}     | ${'1 month'}
      ${'2.months'} | ${SECONDS_PER_MONTH * 2} | ${'2 months'}
      ${'3.months'} | ${SECONDS_PER_MONTH * 3} | ${'3 months'}
    `(
      'formats ActiveSupport::Duration of `$duration` ($secs) as "$formatted"',
      ({ secs, formatted }) => {
        const ms = secs * 1000;

        expect(duration(ms)).toBe(formatted);
      },
    );
  });

  describe('localTimeAgo', () => {
    beforeEach(() => {
      document.body.innerHTML =
        '<time title="some time" datetime="2020-02-18T22:22:32Z">1 hour ago</time>';
    });

    describe.each`
      timeDisplayRelative | text
      ${true}             | ${'4 months ago'}
      ${false}            | ${'Feb 18, 2020, 10:22 PM'}
    `(
      `With User Setting timeDisplayRelative: $timeDisplayRelative`,
      ({ timeDisplayRelative, text }) => {
        it.each`
          updateTooltip | title
          ${false}      | ${'some time'}
          ${true}       | ${'Feb 18, 2020 10:22pm UTC'}
        `(
          `has content: '${text}' and tooltip: '$title' with updateTooltip = $updateTooltip`,
          ({ updateTooltip, title }) => {
            window.gon = { time_display_relative: timeDisplayRelative };

            const element = document.querySelector('time');
            localTimeAgo([element], updateTooltip);

            jest.runAllTimers();

            expect(element.getAttribute('title')).toBe(title);
            expect(element.innerText).toBe(text);
          },
        );
      },
    );

    describe('With User Setting Time Format', () => {
      it.each`
        timeDisplayFormat | display      | text
        ${0}              | ${'System'}  | ${'Feb 18, 2020, 10:22 PM'}
        ${1}              | ${'12-hour'} | ${'Feb 18, 2020, 10:22 PM'}
        ${2}              | ${'24-hour'} | ${'Feb 18, 2020, 22:22'}
      `(`'$display' renders as '$text'`, ({ timeDisplayFormat, text }) => {
        gon.time_display_relative = false;
        gon.time_display_format = timeDisplayFormat;

        const element = document.querySelector('time');
        localTimeAgo([element]);

        jest.runAllTimers();

        expect(element.innerText).toBe(text);
      });
    });
  });
});