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

timelogs_app_spec.js « components « time_tracking « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cc719ee09f5e89d80086926c06353608dacc89d (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
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlDatepicker, GlLoadingIcon, GlKeysetPagination } from '@gitlab/ui';
import getTimelogsEmptyResponse from 'test_fixtures/graphql/get_timelogs_empty_response.json';
import getPaginatedTimelogsResponse from 'test_fixtures/graphql/get_paginated_timelogs_response.json';
import getNonPaginatedTimelogsResponse from 'test_fixtures/graphql/get_non_paginated_timelogs_response.json';
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import { createAlert } from '~/alert';
import { mountExtended, extendedWrapper } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import getTimelogsQuery from '~/time_tracking/components/queries/get_timelogs.query.graphql';
import TimelogsApp from '~/time_tracking/components/timelogs_app.vue';
import TimelogsTable from '~/time_tracking/components/timelogs_table.vue';

jest.mock('~/alert');
jest.mock('~/sentry/sentry_browser_wrapper');

describe('Timelogs app', () => {
  Vue.use(VueApollo);

  let wrapper;
  let fakeApollo;

  const findForm = () => wrapper.find('form');
  const findUsernameInput = () => extendedWrapper(findForm()).findByTestId('form-username');
  const findTableContainer = () => wrapper.findByTestId('table-container');
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findTotalTimeSpentContainer = () => wrapper.findByTestId('total-time-spent-container');
  const findTable = () => wrapper.findComponent(TimelogsTable);
  const findPagination = () => wrapper.findComponent(GlKeysetPagination);

  const findFormDatePicker = (testId) =>
    findForm()
      .findAllComponents(GlDatepicker)
      .filter((c) => c.attributes('data-testid') === testId);
  const findFromDatepicker = () => findFormDatePicker('form-from-date').at(0);
  const findToDatepicker = () => findFormDatePicker('form-to-date').at(0);

  const submitForm = () => findForm().trigger('submit');

  const resolvedEmptyListMock = jest.fn().mockResolvedValue(getTimelogsEmptyResponse);
  const resolvedPaginatedListMock = jest.fn().mockResolvedValue(getPaginatedTimelogsResponse);
  const resolvedNonPaginatedListMock = jest.fn().mockResolvedValue(getNonPaginatedTimelogsResponse);
  const rejectedMock = jest.fn().mockRejectedValue({});

  const mountComponent = ({ props, data } = {}, queryResolverMock = resolvedEmptyListMock) => {
    fakeApollo = createMockApollo([[getTimelogsQuery, queryResolverMock]]);

    wrapper = mountExtended(TimelogsApp, {
      data() {
        return {
          ...data,
        };
      },
      propsData: {
        limitToHours: false,
        ...props,
      },
      apolloProvider: fakeApollo,
    });
  };

  beforeEach(() => {
    createAlert.mockClear();
    Sentry.captureException.mockClear();
  });

  afterEach(() => {
    fakeApollo = null;
  });

  describe('the content', () => {
    it('shows the form and the loading icon when loading', () => {
      mountComponent();

      expect(findForm().exists()).toBe(true);
      expect(findLoadingIcon().exists()).toBe(true);
      expect(findTableContainer().exists()).toBe(false);
    });

    it('shows the form and the table container when finished loading', async () => {
      mountComponent();

      await waitForPromises();

      expect(findForm().exists()).toBe(true);
      expect(findLoadingIcon().exists()).toBe(false);
      expect(findTableContainer().exists()).toBe(true);
    });
  });

  describe('the filter form', () => {
    it('runs the query with the correct data', async () => {
      mountComponent();

      const username = 'johnsmith';
      const fromDateTime = new Date('2023-02-28');
      const toDateTime = new Date('2023-03-28');

      findUsernameInput().vm.$emit('input', username);
      findFromDatepicker().vm.$emit('input', fromDateTime);
      findToDatepicker().vm.$emit('input', toDateTime);

      resolvedEmptyListMock.mockClear();

      submitForm();

      await waitForPromises();

      expect(resolvedEmptyListMock).toHaveBeenCalledWith({
        username,
        startTime: fromDateTime,
        endTime: toDateTime,
        groupId: null,
        projectId: null,
        first: 20,
        last: null,
        after: null,
        before: null,
      });

      expect(`${wrapper.vm.queryVariables.startTime}`).toEqual(
        'Tue Feb 28 2023 00:00:00 GMT+0000 (Greenwich Mean Time)',
      );
      // should be 1 day ahead of the initial To Date value
      expect(`${wrapper.vm.queryVariables.endTime}`).toEqual(
        'Wed Mar 29 2023 00:00:00 GMT+0000 (Greenwich Mean Time)',
      );

      expect(createAlert).not.toHaveBeenCalled();
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    it('runs the query with the correct data after the date filters are cleared', async () => {
      mountComponent();

      const username = 'johnsmith';

      findUsernameInput().vm.$emit('input', username);
      findFromDatepicker().vm.$emit('clear');
      findToDatepicker().vm.$emit('clear');

      resolvedEmptyListMock.mockClear();

      submitForm();

      await waitForPromises();

      expect(resolvedEmptyListMock).toHaveBeenCalledWith({
        username,
        startTime: null,
        endTime: null,
        groupId: null,
        projectId: null,
        first: 20,
        last: null,
        after: null,
        before: null,
      });
      expect(createAlert).not.toHaveBeenCalled();
      expect(Sentry.captureException).not.toHaveBeenCalled();
    });

    it('shows an alert an logs to sentry when the mutation is rejected', async () => {
      mountComponent({}, rejectedMock);

      await waitForPromises();

      expect(createAlert).toHaveBeenCalledWith({
        message: 'Something went wrong. Please try again.',
      });
      expect(Sentry.captureException).toHaveBeenCalled();
    });
  });

  describe('the total time spent container', () => {
    it('is not visible when there are no timelogs', async () => {
      mountComponent();

      await waitForPromises();

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

    it('shows the correct value when `limitToHours` is false', async () => {
      mountComponent({}, resolvedNonPaginatedListMock);

      await waitForPromises();

      expect(findTotalTimeSpentContainer().exists()).toBe(true);
      expect(findTotalTimeSpentContainer().text()).toBe('3d');
    });

    it('shows the correct value when `limitToHours` is true', async () => {
      mountComponent({ props: { limitToHours: true } }, resolvedNonPaginatedListMock);

      await waitForPromises();

      expect(findTotalTimeSpentContainer().exists()).toBe(true);
      expect(findTotalTimeSpentContainer().text()).toBe('24h');
    });
  });

  describe('the table', () => {
    it('gets created with the right props when `limitToHours` is false', async () => {
      mountComponent({}, resolvedNonPaginatedListMock);

      await waitForPromises();

      expect(findTable().props()).toMatchObject({
        limitToHours: false,
        entries: getNonPaginatedTimelogsResponse.data.timelogs.nodes,
      });
    });

    it('gets created with the right props when `limitToHours` is true', async () => {
      mountComponent({ props: { limitToHours: true } }, resolvedNonPaginatedListMock);

      await waitForPromises();

      expect(findTable().props()).toMatchObject({
        limitToHours: true,
        entries: getNonPaginatedTimelogsResponse.data.timelogs.nodes,
      });
    });
  });

  describe('the pagination element', () => {
    it('is not visible whene there is no pagination data', async () => {
      mountComponent({}, resolvedNonPaginatedListMock);

      await waitForPromises();

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

    it('is visible whene there is pagination data', async () => {
      mountComponent({}, resolvedPaginatedListMock);

      await waitForPromises();
      await nextTick();

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