summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/components/time_tracking/time_tracker_spec.js
blob: e08bd80b18ee0cfc779a74c251e05f61b59bde13 (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
import { mount } from '@vue/test-utils';

import { stubTransition } from 'helpers/stub_transition';
import { createMockDirective } from 'helpers/vue_mock_directive';
import TimeTracker from '~/sidebar/components/time_tracking/time_tracker.vue';
import SidebarEventHub from '~/sidebar/event_hub';

import { issuableTimeTrackingResponse } from '../../mock_data';

describe('Issuable Time Tracker', () => {
  let wrapper;

  const findByTestId = (testId) => wrapper.find(`[data-testid=${testId}]`);
  const findComparisonMeter = () => findByTestId('compareMeter').attributes('title');
  const findCollapsedState = () => findByTestId('collapsedState');
  const findTimeRemainingProgress = () => findByTestId('timeRemainingProgress');
  const findReportLink = () => findByTestId('reportLink');

  const defaultProps = {
    limitToHours: false,
    fullPath: 'gitlab-org/gitlab-test',
    issuableIid: '1',
    initialTimeTracking: {
      ...issuableTimeTrackingResponse.data.workspace.issuable,
    },
  };

  const issuableTimeTrackingRefetchSpy = jest.fn();

  const mountComponent = ({ props = {}, issuableType = 'issue', loading = false } = {}) => {
    return mount(TimeTracker, {
      propsData: { ...defaultProps, ...props },
      directives: { GlTooltip: createMockDirective() },
      stubs: {
        transition: stubTransition(),
      },
      provide: {
        issuableType,
      },
      mocks: {
        $apollo: {
          queries: {
            issuableTimeTracking: {
              loading,
              refetch: issuableTimeTrackingRefetchSpy,
              query: jest.fn().mockResolvedValue(issuableTimeTrackingResponse),
            },
          },
        },
      },
    });
  };

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

  describe('Initialization', () => {
    beforeEach(() => {
      wrapper = mountComponent();
    });

    it('should return something defined', () => {
      expect(wrapper).toBeDefined();
    });

    it('should correctly render timeEstimate', () => {
      expect(findByTestId('timeTrackingComparisonPane').html()).toContain(
        defaultProps.initialTimeTracking.humanTimeEstimate,
      );
    });

    it('should correctly render totalTimeSpent', () => {
      expect(findByTestId('timeTrackingComparisonPane').html()).toContain(
        defaultProps.initialTimeTracking.humanTotalTimeSpent,
      );
    });
  });

  describe('Content panes', () => {
    describe('Collapsed state', () => {
      it('should render "time-tracking-collapsed-state" by default when "showCollapsed" prop is not specified', () => {
        wrapper = mountComponent();

        expect(findCollapsedState().exists()).toBe(true);
      });

      it('should not render "time-tracking-collapsed-state" when "showCollapsed" is false', () => {
        wrapper = mountComponent({
          props: {
            showCollapsed: false,
          },
        });

        expect(findCollapsedState().exists()).toBe(false);
      });
    });

    describe('Comparison pane', () => {
      beforeEach(() => {
        wrapper = mountComponent({
          props: {
            initialTimeTracking: {
              timeEstimate: 100_000, // 1d 3h
              totalTimeSpent: 5_000, // 1h 23m
              humanTimeEstimate: '1d 3h',
              humanTotalTimeSpent: '1h 23m',
            },
          },
        });
      });

      it('should show the "Comparison" pane when timeEstimate and time_spent are truthy', () => {
        const pane = findByTestId('timeTrackingComparisonPane');
        expect(pane.exists()).toBe(true);
        expect(pane.isVisible()).toBe(true);
      });

      it('should show full times when the sidebar is collapsed', () => {
        expect(findCollapsedState().text()).toBe('1h 23m / 1d 3h');
      });

      describe('Remaining meter', () => {
        it('should display the remaining meter with the correct width', () => {
          expect(findTimeRemainingProgress().attributes('value')).toBe('5');
        });

        it('should display the remaining meter with the correct background color when within estimate', () => {
          expect(findTimeRemainingProgress().attributes('variant')).toBe('primary');
        });

        it('should display the remaining meter with the correct background color when over estimate', () => {
          wrapper = mountComponent({
            props: {
              initialTimeTracking: {
                ...defaultProps.initialTimeTracking,
                timeEstimate: 10_000, // 2h 46m
                totalTimeSpent: 20_000_000, // 231 days
              },
            },
          });

          expect(findTimeRemainingProgress().attributes('variant')).toBe('danger');
        });
      });
    });

    describe('Comparison pane when limitToHours is true', () => {
      beforeEach(async () => {
        wrapper = mountComponent({
          props: {
            limitToHours: true,
            initialTimeTracking: {
              ...defaultProps.initialTimeTracking,
              timeEstimate: 100_000, // 1d 3h
            },
          },
        });
      });

      it('should show the correct tooltip text', async () => {
        expect(findByTestId('timeTrackingComparisonPane').exists()).toBe(true);
        await wrapper.vm.$nextTick();

        expect(findComparisonMeter()).toBe('Time remaining: 26h 23m');
      });
    });

    describe('Estimate only pane', () => {
      beforeEach(async () => {
        wrapper = mountComponent({
          props: {
            initialTimeTracking: {
              timeEstimate: 10_000, // 2h 46m
              totalTimeSpent: 0,
              humanTimeEstimate: '2h 46m',
              humanTotalTimeSpent: '',
            },
          },
        });
        await wrapper.vm.$nextTick();
      });

      it('should display the human readable version of time estimated', () => {
        const estimateText = findByTestId('estimateOnlyPane').text();
        expect(estimateText.trim()).toBe('Estimated: 2h 46m');
      });
    });

    describe('Spent only pane', () => {
      beforeEach(() => {
        wrapper = mountComponent({
          props: {
            initialTimeTracking: {
              timeEstimate: 0,
              totalTimeSpent: 5_000, // 1h 23m
              humanTimeEstimate: '2h 46m',
              humanTotalTimeSpent: '1h 23m',
            },
          },
        });
      });

      it('should display the human readable version of time spent', () => {
        const spentText = findByTestId('spentOnlyPane').text();
        expect(spentText.trim()).toBe('Spent: 1h 23m');
      });
    });

    describe('No time tracking pane', () => {
      beforeEach(() => {
        wrapper = mountComponent({
          props: {
            initialTimeTracking: {
              timeEstimate: 0,
              totalTimeSpent: 0,
              humanTimeEstimate: '',
              humanTotalTimeSpent: '',
            },
          },
        });
      });

      it('should only show the "No time tracking" pane when both timeEstimate and time_spent are falsey', () => {
        const pane = findByTestId('noTrackingPane');
        const correctText = 'No estimate or time spent';
        expect(pane.exists()).toBe(true);
        expect(pane.text().trim()).toBe(correctText);
      });
    });

    describe('Time tracking report', () => {
      describe('When no time spent', () => {
        beforeEach(() => {
          wrapper = mountComponent({
            props: {
              initialTimeTracking: {
                ...defaultProps.initialTimeTracking,
                totalTimeSpent: 0,
                humanTotalTimeSpent: '',
              },
            },
          });
        });

        it('link should not appear', () => {
          expect(findReportLink().exists()).toBe(false);
        });
      });

      describe('When time spent', () => {
        it('link should appear on issue', () => {
          wrapper = mountComponent();
          expect(findReportLink().exists()).toBe(true);
        });

        it('link should appear on merge request', () => {
          wrapper = mountComponent({ issuableType: 'merge_request' });
          expect(findReportLink().exists()).toBe(true);
        });

        it('link should not appear on milestone', () => {
          wrapper = mountComponent({ issuableType: 'milestone' });
          expect(findReportLink().exists()).toBe(false);
        });
      });
    });

    describe('Help pane', () => {
      const findHelpButton = () => findByTestId('helpButton');
      const findCloseHelpButton = () => findByTestId('closeHelpButton');

      beforeEach(async () => {
        wrapper = mountComponent({
          props: {
            initialTimeTracking: {
              timeEstimate: 0,
              totalTimeSpent: 0,
              humanTimeEstimate: '',
              humanTotalTimeSpent: '',
            },
          },
        });
        await wrapper.vm.$nextTick();
      });

      it('should not show the "Help" pane by default', () => {
        expect(findByTestId('helpPane').exists()).toBe(false);
      });

      it('should show the "Help" pane when help button is clicked', async () => {
        findHelpButton().trigger('click');

        await wrapper.vm.$nextTick();

        expect(findByTestId('helpPane').exists()).toBe(true);
      });

      it('should not show the "Help" pane when help button is clicked and then closed', async () => {
        findHelpButton().trigger('click');
        await wrapper.vm.$nextTick();

        expect(findByTestId('helpPane').exists()).toBe(true);

        findCloseHelpButton().trigger('click');
        await wrapper.vm.$nextTick();

        expect(findByTestId('helpPane').exists()).toBe(false);
      });
    });
  });

  describe('Event listeners', () => {
    it('refetches issuableTimeTracking query when eventHub emits `timeTracker:refresh` event', async () => {
      SidebarEventHub.$emit('timeTracker:refresh');

      await wrapper.vm.$nextTick();

      expect(issuableTimeTrackingRefetchSpy).toHaveBeenCalled();
    });
  });
});