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

board_add_new_column_spec.js « components « boards « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a847d359009f0d760c80fb80198225e3ae58808 (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
import { GlCollapsibleListbox } from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
// eslint-disable-next-line no-restricted-imports
import Vuex from 'vuex';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import BoardAddNewColumn from '~/boards/components/board_add_new_column.vue';
import BoardAddNewColumnForm from '~/boards/components/board_add_new_column_form.vue';
import defaultState from '~/boards/stores/state';
import createBoardListMutation from 'ee_else_ce/boards/graphql/board_list_create.mutation.graphql';
import boardLabelsQuery from '~/boards/graphql/board_labels.query.graphql';
import * as cacheUpdates from '~/boards/graphql/cache_updates';
import {
  mockLabelList,
  createBoardListResponse,
  labelsQueryResponse,
  boardListsQueryResponse,
} from '../mock_data';

Vue.use(Vuex);
Vue.use(VueApollo);

describe('BoardAddNewColumn', () => {
  let wrapper;
  let mockApollo;

  const createBoardListQueryHandler = jest.fn().mockResolvedValue(createBoardListResponse);
  const labelsQueryHandler = jest.fn().mockResolvedValue(labelsQueryResponse);
  const errorMessage = 'Failed to create list';
  const createBoardListQueryHandlerFailure = jest.fn().mockRejectedValue(new Error(errorMessage));
  const errorMessageLabels = 'Failed to fetch labels';
  const labelsQueryHandlerFailure = jest.fn().mockRejectedValue(new Error(errorMessageLabels));

  const findDropdown = () => wrapper.findComponent(GlCollapsibleListbox);
  const findAddNewColumnForm = () => wrapper.findComponent(BoardAddNewColumnForm);
  const selectLabel = (id) => {
    findDropdown().vm.$emit('select', id);
  };

  const createStore = ({ actions = {}, getters = {}, state = {} } = {}) => {
    return new Vuex.Store({
      state: {
        ...defaultState,
        ...state,
      },
      actions,
      getters,
    });
  };

  const mountComponent = ({
    selectedId,
    labels = [],
    getListByLabelId = jest.fn(),
    actions = {},
    provide = {},
    lists = {},
    labelsHandler = labelsQueryHandler,
    createHandler = createBoardListQueryHandler,
  } = {}) => {
    mockApollo = createMockApollo([
      [boardLabelsQuery, labelsHandler],
      [createBoardListMutation, createHandler],
    ]);

    wrapper = shallowMountExtended(BoardAddNewColumn, {
      apolloProvider: mockApollo,
      propsData: {
        listQueryVariables: {
          isGroup: false,
          isProject: true,
          fullPath: 'gitlab-org/gitlab',
          boardId: 'gid://gitlab/Board/1',
          filters: {},
        },
        boardId: 'gid://gitlab/Board/1',
        lists,
      },
      data() {
        return {
          selectedId,
        };
      },
      store: createStore({
        actions: {
          fetchLabels: jest.fn(),
          ...actions,
        },
        getters: {
          getListByLabelId: () => getListByLabelId,
        },
        state: {
          labels,
          labelsLoading: false,
        },
      }),
      provide: {
        scopedLabelsAvailable: true,
        isEpicBoard: false,
        issuableType: 'issue',
        fullPath: 'gitlab-org/gitlab',
        boardType: 'project',
        isApolloBoard: false,
        ...provide,
      },
      stubs: {
        GlCollapsibleListbox,
      },
    });

    // trigger change event
    if (selectedId) {
      selectLabel(selectedId);
    }

    // Necessary for cache update
    mockApollo.clients.defaultClient.cache.readQuery = jest
      .fn()
      .mockReturnValue(boardListsQueryResponse.data);
    mockApollo.clients.defaultClient.cache.writeQuery = jest.fn();
  };

  beforeEach(() => {
    cacheUpdates.setError = jest.fn();
  });

  describe('Add list button', () => {
    it('calls addList', async () => {
      const getListByLabelId = jest.fn().mockReturnValue(null);
      const highlightList = jest.fn();
      const createList = jest.fn();

      mountComponent({
        labels: [mockLabelList.label],
        selectedId: mockLabelList.label.id,
        getListByLabelId,
        actions: {
          createList,
          highlightList,
        },
      });

      findAddNewColumnForm().vm.$emit('add-list');

      await nextTick();

      expect(highlightList).not.toHaveBeenCalled();
      expect(createList).toHaveBeenCalledWith(expect.anything(), {
        labelId: mockLabelList.label.id,
      });
    });

    it('highlights existing list if trying to re-add', async () => {
      const getListByLabelId = jest.fn().mockReturnValue(mockLabelList);
      const highlightList = jest.fn();
      const createList = jest.fn();

      mountComponent({
        labels: [mockLabelList.label],
        selectedId: mockLabelList.label.id,
        getListByLabelId,
        actions: {
          createList,
          highlightList,
        },
      });

      findAddNewColumnForm().vm.$emit('add-list');

      await nextTick();

      expect(highlightList).toHaveBeenCalledWith(expect.anything(), mockLabelList.id);
      expect(createList).not.toHaveBeenCalled();
    });
  });

  describe('Apollo boards', () => {
    describe('when list is new', () => {
      beforeEach(() => {
        mountComponent({ selectedId: mockLabelList.label.id, provide: { isApolloBoard: true } });
      });

      it('fetches labels and adds list', async () => {
        findDropdown().vm.$emit('show');

        await nextTick();
        expect(labelsQueryHandler).toHaveBeenCalled();

        selectLabel(mockLabelList.label.id);

        findAddNewColumnForm().vm.$emit('add-list');

        await nextTick();

        expect(wrapper.emitted('highlight-list')).toBeUndefined();
        expect(createBoardListQueryHandler).toHaveBeenCalledWith({
          labelId: mockLabelList.label.id,
          boardId: 'gid://gitlab/Board/1',
        });
      });
    });

    describe('when list already exists in board', () => {
      beforeEach(() => {
        mountComponent({
          lists: {
            [mockLabelList.id]: mockLabelList,
          },
          selectedId: mockLabelList.label.id,
          provide: { isApolloBoard: true },
        });
      });

      it('highlights existing list if trying to re-add', async () => {
        findDropdown().vm.$emit('show');

        await nextTick();
        expect(labelsQueryHandler).toHaveBeenCalled();

        selectLabel(mockLabelList.label.id);

        findAddNewColumnForm().vm.$emit('add-list');

        await waitForPromises();

        expect(wrapper.emitted('highlight-list')).toEqual([[mockLabelList.id]]);
        expect(createBoardListQueryHandler).not.toHaveBeenCalledWith();
      });
    });

    describe('when fetch labels query fails', () => {
      beforeEach(() => {
        mountComponent({
          provide: { isApolloBoard: true },
          labelsHandler: labelsQueryHandlerFailure,
        });
      });

      it('sets error', async () => {
        findDropdown().vm.$emit('show');

        await waitForPromises();
        expect(cacheUpdates.setError).toHaveBeenCalled();
      });
    });

    describe('when create list mutation fails', () => {
      beforeEach(() => {
        mountComponent({
          selectedId: mockLabelList.label.id,
          provide: { isApolloBoard: true },
          createHandler: createBoardListQueryHandlerFailure,
        });
      });

      it('sets error', async () => {
        findDropdown().vm.$emit('show');

        await nextTick();
        expect(labelsQueryHandler).toHaveBeenCalled();

        selectLabel(mockLabelList.label.id);

        findAddNewColumnForm().vm.$emit('add-list');

        await waitForPromises();

        expect(cacheUpdates.setError).toHaveBeenCalled();
      });
    });
  });
});