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

date_ranges_dropdown_spec.js « components « shared « analytics « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c622983cad8aa93e214f21f33a66d022137d4be (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
import { nextTick } from 'vue';
import { GlCollapsibleListbox, GlIcon } from '@gitlab/ui';
import DateRangesDropdown from '~/analytics/shared/components/date_ranges_dropdown.vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';

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

  const customDateRangeValue = 'custom';
  const lastWeekValue = 'lastWeek';
  const last30DaysValue = 'lastMonth';
  const mockLastWeek = {
    text: 'Last week',
    value: lastWeekValue,
    startDate: new Date('2023-09-08T00:00:00.000Z'),
    endDate: new Date('2023-09-14T00:00:00.000Z'),
  };
  const mockLast30Days = {
    text: 'Last month',
    value: last30DaysValue,
    startDate: new Date('2023-08-16T00:00:00.000Z'),
    endDate: new Date('2023-09-14T00:00:00.000Z'),
  };
  const mockCustomDateRangeItem = {
    text: 'Custom',
    value: customDateRangeValue,
  };
  const mockDateRanges = [mockLastWeek, mockLast30Days];
  const mockItems = mockDateRanges.map(({ text, value }) => ({ text, value }));
  const mockTooltipText = 'Max date range is 180 days';

  const createComponent = ({ props = {}, dateRangeOptions = mockDateRanges } = {}) => {
    wrapper = shallowMountExtended(DateRangesDropdown, {
      propsData: {
        dateRangeOptions,
        ...props,
      },
      directives: {
        GlTooltip: createMockDirective('gl-tooltip'),
      },
    });
  };

  const findListBox = () => wrapper.findComponent(GlCollapsibleListbox);
  const findDaysSelectedCount = () => wrapper.findByTestId('predefined-date-range-days-count');
  const findHelpIcon = () => wrapper.findComponent(GlIcon);

  describe('default state', () => {
    beforeEach(() => {
      createComponent();
    });

    it('should pass items to listbox `items` prop in correct order', () => {
      expect(findListBox().props('items')).toStrictEqual([...mockItems, mockCustomDateRangeItem]);
    });

    it('should display first option as selected', () => {
      expect(findListBox().props('selected')).toBe(lastWeekValue);
    });

    it('should not display info icon', () => {
      expect(findHelpIcon().exists()).toBe(false);
    });

    describe.each`
      dateRangeValue     | dateRangeItem
      ${lastWeekValue}   | ${mockLastWeek}
      ${last30DaysValue} | ${mockLast30Days}
    `('when $dateRangeValue date range is selected', ({ dateRangeValue, dateRangeItem }) => {
      beforeEach(async () => {
        findListBox().vm.$emit('select', dateRangeValue);

        await nextTick();
      });

      it('should emit `selected` event with value and date range', () => {
        const { text, ...dateRangeProps } = dateRangeItem;

        expect(wrapper.emitted('selected')).toEqual([[dateRangeProps]]);
      });

      it('should display days selected indicator', () => {
        expect(findDaysSelectedCount().exists()).toBe(true);
      });

      it('should not emit `customDateRangeSelected` event', () => {
        expect(wrapper.emitted('customDateRangeSelected')).toBeUndefined();
      });
    });

    describe('when the custom date range option is selected', () => {
      beforeEach(async () => {
        findListBox().vm.$emit('select', customDateRangeValue);

        await nextTick();
      });

      it('should emit `customDateRangeSelected` event', () => {
        expect(wrapper.emitted('customDateRangeSelected')).toHaveLength(1);
      });

      it('should hide days selected indicator', () => {
        expect(findDaysSelectedCount().exists()).toBe(false);
      });

      it('should not emit `selected` event', () => {
        expect(wrapper.emitted('selected')).toBeUndefined();
      });
    });
  });

  describe('when a date range is preselected', () => {
    beforeEach(() => {
      createComponent({ props: { selected: 'lastMonth' } });
    });

    it('should display preselected date range as selected in listbox', () => {
      expect(findListBox().props('selected')).toBe(last30DaysValue);
    });
  });

  describe('days selected indicator', () => {
    it.each`
      selected           | includeEndDateInDaysSelected | expectedDaysCount
      ${lastWeekValue}   | ${true}                      | ${7}
      ${last30DaysValue} | ${true}                      | ${30}
      ${lastWeekValue}   | ${false}                     | ${6}
      ${last30DaysValue} | ${false}                     | ${29}
    `(
      'should display correct days selected when includeEndDateInDaysSelected=$includeEndDateInDaysSelected',
      ({ selected, includeEndDateInDaysSelected, expectedDaysCount }) => {
        createComponent({ props: { selected, includeEndDateInDaysSelected } });

        expect(wrapper.findByText(`${expectedDaysCount} days selected`).exists()).toBe(true);
      },
    );

    it('should not rendered the indicator if disableSelectedDayCount is set', () => {
      createComponent({ props: { disableSelectedDayCount: true, selected: lastWeekValue } });
      expect(findDaysSelectedCount().exists()).toBe(false);
    });
  });

  describe('when the `tooltip` prop is set', () => {
    beforeEach(() => {
      createComponent({ props: { tooltip: mockTooltipText } });
    });

    it('should display info icon with tooltip', () => {
      const helpIcon = findHelpIcon();
      const tooltip = getBinding(helpIcon.element, 'gl-tooltip');

      expect(helpIcon.props('name')).toBe('information-o');
      expect(helpIcon.attributes('title')).toBe(mockTooltipText);

      expect(tooltip).toBeDefined();
    });
  });

  describe('when `includeCustomDateRangeOption` = false', () => {
    beforeEach(() => {
      createComponent({ props: { includeCustomDateRangeOption: false } });
    });

    it('should pass items without custom date range option to listbox `items` prop', () => {
      expect(findListBox().props('items')).toEqual(mockItems);
    });
  });
});