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

date_time_picker_spec.js « date_time_picker « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa41df438d2ae003383ec692a4bd13dfe291ec62 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { mount } from '@vue/test-utils';
import timezoneMock from 'timezone-mock';
import { nextTick } from 'vue';
import DateTimePicker from '~/vue_shared/components/date_time_picker/date_time_picker.vue';
import {
  defaultTimeRanges,
  defaultTimeRange,
} from '~/vue_shared/components/date_time_picker/date_time_picker_lib';

const optionsCount = defaultTimeRanges.length;

describe('DateTimePicker', () => {
  let wrapper;

  const dropdownToggle = () => wrapper.find('.dropdown-toggle');
  const dropdownMenu = () => wrapper.find('.dropdown-menu');
  const cancelButton = () => wrapper.find('[data-testid="cancelButton"]');
  const applyButtonElement = () => wrapper.find('button.btn-confirm').element;
  const findQuickRangeItems = () => wrapper.findAll('.dropdown-item');

  const createComponent = (props) => {
    wrapper = mount(DateTimePicker, {
      propsData: {
        ...props,
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  it('renders dropdown toggle button with selected text', async () => {
    createComponent();
    await nextTick();
    expect(dropdownToggle().text()).toBe(defaultTimeRange.label);
  });

  it('renders dropdown toggle button with selected text and utc label', async () => {
    createComponent({ utc: true });
    await nextTick();
    expect(dropdownToggle().text()).toContain(defaultTimeRange.label);
    expect(dropdownToggle().text()).toContain('UTC');
  });

  it('renders dropdown with 2 custom time range inputs', async () => {
    createComponent();
    await nextTick();
    expect(wrapper.findAll('input').length).toBe(2);
  });

  describe('renders label with h/m/s truncated if possible', () => {
    [
      {
        start: '2019-10-10T00:00:00.000Z',
        end: '2019-10-10T00:00:00.000Z',
        label: '2019-10-10 to 2019-10-10',
      },
      {
        start: '2019-10-10T00:00:00.000Z',
        end: '2019-10-14T00:10:00.000Z',
        label: '2019-10-10 to 2019-10-14 00:10:00',
      },
      {
        start: '2019-10-10T00:00:00.000Z',
        end: '2019-10-10T00:00:01.000Z',
        label: '2019-10-10 to 2019-10-10 00:00:01',
      },
      {
        start: '2019-10-10T00:00:01.000Z',
        end: '2019-10-10T00:00:01.000Z',
        label: '2019-10-10 00:00:01 to 2019-10-10 00:00:01',
      },
      {
        start: '2019-10-10T00:00:01.000Z',
        end: '2019-10-10T00:00:01.000Z',
        utc: true,
        label: '2019-10-10 00:00:01 to 2019-10-10 00:00:01 UTC',
      },
    ].forEach(({ start, end, utc, label }) => {
      it(`for start ${start}, end ${end}, and utc ${utc}, label is ${label}`, async () => {
        createComponent({
          value: { start, end },
          utc,
        });
        await nextTick();
        expect(dropdownToggle().text()).toBe(label);
      });
    });
  });

  it(`renders dropdown with ${optionsCount} (default) items in quick range`, async () => {
    createComponent();
    dropdownToggle().trigger('click');
    await nextTick();
    expect(findQuickRangeItems().length).toBe(optionsCount);
  });

  it('renders dropdown with a default quick range item selected', async () => {
    createComponent();
    dropdownToggle().trigger('click');
    await nextTick();
    expect(wrapper.find('.dropdown-item.active').exists()).toBe(true);
    expect(wrapper.find('.dropdown-item.active').text()).toBe(defaultTimeRange.label);
  });

  it('renders a disabled apply button on wrong input', () => {
    createComponent({
      start: 'invalid-input-date',
    });

    expect(applyButtonElement().getAttribute('disabled')).toBe('disabled');
  });

  describe('user input', () => {
    const fillInputAndBlur = async (input, val) => {
      wrapper.find(input).setValue(val);
      await nextTick();
      wrapper.find(input).trigger('blur');
      await nextTick();
    };

    beforeEach(async () => {
      createComponent();
      await nextTick();
    });

    it('displays inline error message if custom time range inputs are invalid', async () => {
      await fillInputAndBlur('#custom-time-from', '2019-10-01abc');
      await fillInputAndBlur('#custom-time-to', '2019-10-10abc');
      expect(wrapper.findAll('.invalid-feedback').length).toBe(2);
    });

    it('keeps apply button disabled with invalid custom time range inputs', async () => {
      await fillInputAndBlur('#custom-time-from', '2019-10-01abc');
      await fillInputAndBlur('#custom-time-to', '2019-09-19');
      expect(applyButtonElement().getAttribute('disabled')).toBe('disabled');
    });

    it('enables apply button with valid custom time range inputs', async () => {
      await fillInputAndBlur('#custom-time-from', '2019-10-01');
      await fillInputAndBlur('#custom-time-to', '2019-10-19');
      expect(applyButtonElement().getAttribute('disabled')).toBeNull();
    });

    describe('when "apply" is clicked', () => {
      it('emits iso dates', async () => {
        await fillInputAndBlur('#custom-time-from', '2019-10-01 00:00:00');
        await fillInputAndBlur('#custom-time-to', '2019-10-19 00:00:00');
        applyButtonElement().click();

        expect(wrapper.emitted().input).toHaveLength(1);
        expect(wrapper.emitted().input[0]).toEqual([
          {
            end: '2019-10-19T00:00:00Z',
            start: '2019-10-01T00:00:00Z',
          },
        ]);
      });

      it('emits iso dates, for dates without time of day', async () => {
        await fillInputAndBlur('#custom-time-from', '2019-10-01');
        await fillInputAndBlur('#custom-time-to', '2019-10-19');
        applyButtonElement().click();

        expect(wrapper.emitted().input).toHaveLength(1);
        expect(wrapper.emitted().input[0]).toEqual([
          {
            end: '2019-10-19T00:00:00Z',
            start: '2019-10-01T00:00:00Z',
          },
        ]);
      });

      describe('when timezone is different', () => {
        beforeAll(() => {
          timezoneMock.register('US/Pacific');
        });
        afterAll(() => {
          timezoneMock.unregister();
        });

        it('emits iso dates', async () => {
          await fillInputAndBlur('#custom-time-from', '2019-10-01 00:00:00');
          await fillInputAndBlur('#custom-time-to', '2019-10-19 12:00:00');
          applyButtonElement().click();

          expect(wrapper.emitted().input).toHaveLength(1);
          expect(wrapper.emitted().input[0]).toEqual([
            {
              start: '2019-10-01T07:00:00Z',
              end: '2019-10-19T19:00:00Z',
            },
          ]);
        });

        it('emits iso dates with utc format', async () => {
          wrapper.setProps({ utc: true });
          await nextTick();
          await fillInputAndBlur('#custom-time-from', '2019-10-01 00:00:00');
          await fillInputAndBlur('#custom-time-to', '2019-10-19 12:00:00');
          applyButtonElement().click();

          expect(wrapper.emitted().input).toHaveLength(1);
          expect(wrapper.emitted().input[0]).toEqual([
            {
              start: '2019-10-01T00:00:00Z',
              end: '2019-10-19T12:00:00Z',
            },
          ]);
        });
      });
    });

    it('unchecks quick range when text is input is clicked', async () => {
      const findActiveItems = () =>
        findQuickRangeItems().filter((w) => w.classes().includes('active'));

      expect(findActiveItems().length).toBe(1);

      await fillInputAndBlur('#custom-time-from', '2019-10-01');
      expect(findActiveItems().length).toBe(0);
    });

    it('emits dates in an object when a  is clicked', () => {
      findQuickRangeItems()
        .at(3) // any item
        .trigger('click');

      expect(wrapper.emitted().input).toHaveLength(1);
      expect(wrapper.emitted().input[0][0]).toMatchObject({
        duration: {
          seconds: expect.any(Number),
        },
      });
    });

    it('hides the popover with cancel button', async () => {
      dropdownToggle().trigger('click');

      await nextTick();
      cancelButton().trigger('click');

      await nextTick();
      expect(dropdownMenu().classes('show')).toBe(false);
    });
  });

  describe('when using non-default time windows', () => {
    const MOCK_NOW = Date.UTC(2020, 0, 23, 20);

    const otherTimeRanges = [
      {
        label: '1 minute',
        duration: { seconds: 60 },
      },
      {
        label: '2 minutes',
        duration: { seconds: 60 * 2 },
        default: true,
      },
      {
        label: '5 minutes',
        duration: { seconds: 60 * 5 },
      },
    ];

    beforeEach(() => {
      jest.spyOn(Date, 'now').mockImplementation(() => MOCK_NOW);
    });

    it('renders dropdown with a label in the quick range', async () => {
      createComponent({
        value: {
          duration: { seconds: 60 * 5 },
        },
        options: otherTimeRanges,
      });
      dropdownToggle().trigger('click');
      await nextTick();
      expect(dropdownToggle().text()).toBe('5 minutes');
    });

    it('renders dropdown with a label in the quick range and utc label', async () => {
      createComponent({
        value: {
          duration: { seconds: 60 * 5 },
        },
        utc: true,
        options: otherTimeRanges,
      });
      dropdownToggle().trigger('click');
      await nextTick();
      expect(dropdownToggle().text()).toBe('5 minutes UTC');
    });

    it('renders dropdown with quick range items', async () => {
      createComponent({
        value: {
          duration: { seconds: 60 * 2 },
        },
        options: otherTimeRanges,
      });
      dropdownToggle().trigger('click');
      await nextTick();
      const items = findQuickRangeItems();

      expect(items.length).toBe(Object.keys(otherTimeRanges).length);
      expect(items.at(0).text()).toBe('1 minute');
      expect(items.at(0).classes()).not.toContain('active');

      expect(items.at(1).text()).toBe('2 minutes');
      expect(items.at(1).classes()).toContain('active');

      expect(items.at(2).text()).toBe('5 minutes');
      expect(items.at(2).classes()).not.toContain('active');
    });

    it('renders dropdown with a label not in the quick range', async () => {
      createComponent({
        value: {
          duration: { seconds: 60 * 4 },
        },
      });
      dropdownToggle().trigger('click');
      await nextTick();
      expect(dropdownToggle().text()).toBe('2020-01-23 19:56:00 to 2020-01-23 20:00:00');
    });
  });
});