summaryrefslogtreecommitdiff
path: root/spec/frontend/snippets/components/edit_spec.js
blob: b818f98efb167868e2e1608dcc6346947c713024 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import VueApollo, { ApolloMutation } from 'vue-apollo';
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import GetSnippetQuery from 'shared_queries/snippet/snippet.query.graphql';
import { deprecatedCreateFlash as Flash } from '~/flash';
import * as urlUtils from '~/lib/utils/url_utility';
import SnippetEditApp from '~/snippets/components/edit.vue';
import SnippetDescriptionEdit from '~/snippets/components/snippet_description_edit.vue';
import SnippetVisibilityEdit from '~/snippets/components/snippet_visibility_edit.vue';
import SnippetBlobActionsEdit from '~/snippets/components/snippet_blob_actions_edit.vue';
import TitleField from '~/vue_shared/components/form/title.vue';
import FormFooterActions from '~/vue_shared/components/form/form_footer_actions.vue';
import {
  SNIPPET_VISIBILITY_PRIVATE,
  SNIPPET_VISIBILITY_INTERNAL,
  SNIPPET_VISIBILITY_PUBLIC,
} from '~/snippets/constants';
import UpdateSnippetMutation from '~/snippets/mutations/updateSnippet.mutation.graphql';
import CreateSnippetMutation from '~/snippets/mutations/createSnippet.mutation.graphql';
import { testEntries } from '../test_utils';

jest.mock('~/flash');

const TEST_UPLOADED_FILES = ['foo/bar.txt', 'alpha/beta.js'];
const TEST_API_ERROR = 'Ufff';
const TEST_MUTATION_ERROR = 'Bummer';

const TEST_ACTIONS = {
  NO_CONTENT: {
    ...testEntries.created.diff,
    content: '',
  },
  NO_PATH: {
    ...testEntries.created.diff,
    filePath: '',
  },
  VALID: {
    ...testEntries.created.diff,
  },
};

const TEST_WEB_URL = '/snippets/7';

const createTestSnippet = () => ({
  webUrl: TEST_WEB_URL,
  id: 7,
  title: 'Snippet Title',
  description: 'Lorem ipsum snippet desc',
  visibilityLevel: SNIPPET_VISIBILITY_PRIVATE,
});

describe('Snippet Edit app', () => {
  let wrapper;
  let fakeApollo;
  const relativeUrlRoot = '/foo/';
  const originalRelativeUrlRoot = gon.relative_url_root;
  const GetSnippetQuerySpy = jest.fn().mockResolvedValue({
    data: { snippets: { nodes: [createTestSnippet()] } },
  });

  const mutationTypes = {
    RESOLVE: jest.fn().mockResolvedValue({
      data: {
        updateSnippet: {
          errors: [],
          snippet: createTestSnippet(),
        },
      },
    }),
    RESOLVE_WITH_ERRORS: jest.fn().mockResolvedValue({
      data: {
        updateSnippet: {
          errors: [TEST_MUTATION_ERROR],
          snippet: createTestSnippet(),
        },
        createSnippet: {
          errors: [TEST_MUTATION_ERROR],
          snippet: null,
        },
      },
    }),
    REJECT: jest.fn().mockRejectedValue(TEST_API_ERROR),
  };

  function createComponent({
    props = {},
    loading = false,
    mutationRes = mutationTypes.RESOLVE,
    selectedLevel = SNIPPET_VISIBILITY_PRIVATE,
    withApollo = false,
  } = {}) {
    let componentData = {
      mocks: {
        $apollo: {
          queries: {
            snippet: { loading },
          },
          mutate: mutationRes,
        },
      },
    };

    if (withApollo) {
      const localVue = createLocalVue();
      localVue.use(VueApollo);

      const requestHandlers = [[GetSnippetQuery, GetSnippetQuerySpy]];
      fakeApollo = createMockApollo(requestHandlers);
      componentData = {
        localVue,
        apolloProvider: fakeApollo,
      };
    }

    wrapper = shallowMount(SnippetEditApp, {
      ...componentData,
      stubs: {
        ApolloMutation,
        FormFooterActions,
      },
      provide: {
        selectedLevel,
      },
      propsData: {
        snippetGid: 'gid://gitlab/PersonalSnippet/42',
        markdownPreviewPath: 'http://preview.foo.bar',
        markdownDocsPath: 'http://docs.foo.bar',
        ...props,
      },
    });
  }

  beforeEach(() => {
    gon.relative_url_root = relativeUrlRoot;
    jest.spyOn(urlUtils, 'redirectTo').mockImplementation();
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
    gon.relative_url_root = originalRelativeUrlRoot;
  });

  const findBlobActions = () => wrapper.find(SnippetBlobActionsEdit);
  const findSubmitButton = () => wrapper.find('[data-testid="snippet-submit-btn"]');
  const findCancelButton = () => wrapper.find('[data-testid="snippet-cancel-btn"]');
  const hasDisabledSubmit = () => Boolean(findSubmitButton().attributes('disabled'));

  const clickSubmitBtn = () => wrapper.find('[data-testid="snippet-edit-form"]').trigger('submit');
  const triggerBlobActions = (actions) => findBlobActions().vm.$emit('actions', actions);
  const setUploadFilesHtml = (paths) => {
    wrapper.vm.$el.innerHTML = paths
      .map((path) => `<input name="files[]" value="${path}">`)
      .join('');
  };
  const getApiData = ({
    id,
    title = '',
    description = '',
    visibilityLevel = SNIPPET_VISIBILITY_PRIVATE,
  } = {}) => ({
    id,
    title,
    description,
    visibilityLevel,
    blobActions: [],
  });

  // Ideally we wouldn't call this method directly, but we don't have a way to trigger
  // apollo responses yet.
  const loadSnippet = (...nodes) => {
    if (nodes.length) {
      wrapper.setData({
        snippet: nodes[0],
        newSnippet: false,
      });
    } else {
      wrapper.setData({
        newSnippet: true,
      });
    }
  };

  describe('rendering', () => {
    it('renders loader while the query is in flight', () => {
      createComponent({ loading: true });
      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });

    it.each([[{}], [{ snippetGid: '' }]])(
      'should render all required components with %s',
      (props) => {
        createComponent(props);

        expect(wrapper.find(TitleField).exists()).toBe(true);
        expect(wrapper.find(SnippetDescriptionEdit).exists()).toBe(true);
        expect(wrapper.find(SnippetVisibilityEdit).exists()).toBe(true);
        expect(wrapper.find(FormFooterActions).exists()).toBe(true);
        expect(findBlobActions().exists()).toBe(true);
      },
    );

    it.each`
      title    | actions                                          | shouldDisable
      ${''}    | ${[]}                                            | ${true}
      ${''}    | ${[TEST_ACTIONS.VALID]}                          | ${true}
      ${'foo'} | ${[]}                                            | ${false}
      ${'foo'} | ${[TEST_ACTIONS.VALID]}                          | ${false}
      ${'foo'} | ${[TEST_ACTIONS.VALID, TEST_ACTIONS.NO_CONTENT]} | ${true}
      ${'foo'} | ${[TEST_ACTIONS.VALID, TEST_ACTIONS.NO_PATH]}    | ${false}
    `(
      'should handle submit disable (title=$title, actions=$actions, shouldDisable=$shouldDisable)',
      async ({ title, actions, shouldDisable }) => {
        createComponent();

        loadSnippet({ title });
        triggerBlobActions(actions);

        await wrapper.vm.$nextTick();

        expect(hasDisabledSubmit()).toBe(shouldDisable);
      },
    );

    it.each`
      projectPath       | snippetArg               | expectation
      ${''}             | ${[]}                    | ${urlUtils.joinPaths('/', relativeUrlRoot, '-', 'snippets')}
      ${'project/path'} | ${[]}                    | ${urlUtils.joinPaths('/', relativeUrlRoot, 'project/path/-', 'snippets')}
      ${''}             | ${[createTestSnippet()]} | ${TEST_WEB_URL}
      ${'project/path'} | ${[createTestSnippet()]} | ${TEST_WEB_URL}
    `(
      'should set cancel href when (projectPath=$projectPath, snippet=$snippetArg)',
      async ({ projectPath, snippetArg, expectation }) => {
        createComponent({
          props: { projectPath },
        });

        loadSnippet(...snippetArg);

        await wrapper.vm.$nextTick();

        expect(findCancelButton().attributes('href')).toBe(expectation);
      },
    );
  });

  describe('functionality', () => {
    it('does not fetch snippet when create a new snippet', async () => {
      createComponent({ props: { snippetGid: '' }, withApollo: true });

      jest.runOnlyPendingTimers();
      await wrapper.vm.$nextTick();

      expect(GetSnippetQuerySpy).not.toHaveBeenCalled();
    });

    describe('default visibility', () => {
      it.each([SNIPPET_VISIBILITY_PRIVATE, SNIPPET_VISIBILITY_INTERNAL, SNIPPET_VISIBILITY_PUBLIC])(
        'marks %s visibility by default',
        async (visibility) => {
          createComponent({
            props: { snippetGid: '' },
            selectedLevel: visibility,
          });
          expect(wrapper.vm.snippet.visibilityLevel).toEqual(visibility);
        },
      );
    });

    describe('form submission handling', () => {
      it.each`
        snippetArg               | projectPath       | uploadedFiles          | input                                                                       | mutation
        ${[]}                    | ${'project/path'} | ${[]}                  | ${{ ...getApiData(), projectPath: 'project/path', uploadedFiles: [] }}      | ${CreateSnippetMutation}
        ${[]}                    | ${''}             | ${[]}                  | ${{ ...getApiData(), projectPath: '', uploadedFiles: [] }}                  | ${CreateSnippetMutation}
        ${[]}                    | ${''}             | ${TEST_UPLOADED_FILES} | ${{ ...getApiData(), projectPath: '', uploadedFiles: TEST_UPLOADED_FILES }} | ${CreateSnippetMutation}
        ${[createTestSnippet()]} | ${'project/path'} | ${[]}                  | ${getApiData(createTestSnippet())}                                          | ${UpdateSnippetMutation}
        ${[createTestSnippet()]} | ${''}             | ${[]}                  | ${getApiData(createTestSnippet())}                                          | ${UpdateSnippetMutation}
      `(
        'should submit mutation with (snippet=$snippetArg, projectPath=$projectPath, uploadedFiles=$uploadedFiles)',
        async ({ snippetArg, projectPath, uploadedFiles, mutation, input }) => {
          createComponent({
            props: {
              projectPath,
            },
          });
          loadSnippet(...snippetArg);
          setUploadFilesHtml(uploadedFiles);

          await wrapper.vm.$nextTick();

          clickSubmitBtn();

          expect(mutationTypes.RESOLVE).toHaveBeenCalledWith({
            mutation,
            variables: {
              input,
            },
          });
        },
      );

      it('should redirect to snippet view on successful mutation', async () => {
        createComponent();
        loadSnippet(createTestSnippet());

        clickSubmitBtn();

        await waitForPromises();

        expect(urlUtils.redirectTo).toHaveBeenCalledWith(TEST_WEB_URL);
      });

      it.each`
        snippetArg               | projectPath       | mutationRes                          | expectMessage
        ${[]}                    | ${'project/path'} | ${mutationTypes.RESOLVE_WITH_ERRORS} | ${`Can't create snippet: ${TEST_MUTATION_ERROR}`}
        ${[]}                    | ${''}             | ${mutationTypes.RESOLVE_WITH_ERRORS} | ${`Can't create snippet: ${TEST_MUTATION_ERROR}`}
        ${[]}                    | ${''}             | ${mutationTypes.REJECT}              | ${`Can't create snippet: ${TEST_API_ERROR}`}
        ${[createTestSnippet()]} | ${'project/path'} | ${mutationTypes.RESOLVE_WITH_ERRORS} | ${`Can't update snippet: ${TEST_MUTATION_ERROR}`}
        ${[createTestSnippet()]} | ${''}             | ${mutationTypes.RESOLVE_WITH_ERRORS} | ${`Can't update snippet: ${TEST_MUTATION_ERROR}`}
      `(
        'should flash error with (snippet=$snippetArg, projectPath=$projectPath)',
        async ({ snippetArg, projectPath, mutationRes, expectMessage }) => {
          createComponent({
            props: {
              projectPath,
            },
            mutationRes,
          });
          loadSnippet(...snippetArg);

          clickSubmitBtn();

          await waitForPromises();

          expect(urlUtils.redirectTo).not.toHaveBeenCalled();
          expect(Flash).toHaveBeenCalledWith(expectMessage);
        },
      );
    });

    describe('on before unload', () => {
      it.each`
        condition                       | expectPrevented | action
        ${'there are no actions'}       | ${false}        | ${() => triggerBlobActions([])}
        ${'there are actions'}          | ${true}         | ${() => triggerBlobActions([testEntries.updated.diff])}
        ${'the snippet is being saved'} | ${false}        | ${() => clickSubmitBtn()}
      `(
        'handles before unload prevent when $condition (expectPrevented=$expectPrevented)',
        ({ expectPrevented, action }) => {
          createComponent();
          loadSnippet();

          action();

          const event = new Event('beforeunload');
          const returnValueSetter = jest.spyOn(event, 'returnValue', 'set');

          window.dispatchEvent(event);

          if (expectPrevented) {
            expect(returnValueSetter).toHaveBeenCalledWith(
              'Are you sure you want to lose unsaved changes?',
            );
          } else {
            expect(returnValueSetter).not.toHaveBeenCalled();
          }
        },
      );
    });
  });
});