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

utils_spec.js « notes « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3607c3c546c99c9ce1c73bd82a0e56df6f2a8dc8 (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
import { sprintf } from '~/locale';
import { createNoteErrorMessages, updateNoteErrorMessage } from '~/notes/utils';
import { HTTP_STATUS_UNPROCESSABLE_ENTITY, HTTP_STATUS_BAD_REQUEST } from '~/lib/utils/http_status';
import { COMMENT_FORM, UPDATE_COMMENT_FORM } from '~/notes/i18n';

describe('createNoteErrorMessages', () => {
  describe('when http status is not HTTP_STATUS_UNPROCESSABLE_ENTITY', () => {
    it('returns generic error', () => {
      const errorMessages = createNoteErrorMessages(
        { errors: ['unknown error'] },
        HTTP_STATUS_BAD_REQUEST,
      );

      expect(errorMessages).toStrictEqual([COMMENT_FORM.GENERIC_UNSUBMITTABLE_NETWORK]);
    });
  });

  describe('when http status is HTTP_STATUS_UNPROCESSABLE_ENTITY', () => {
    it('returns all errors', () => {
      const errorMessages = createNoteErrorMessages(
        { errors: 'error 1 and error 2' },
        HTTP_STATUS_UNPROCESSABLE_ENTITY,
      );

      expect(errorMessages).toStrictEqual([
        sprintf(COMMENT_FORM.error, { reason: 'error 1 and error 2' }),
      ]);
    });

    describe('when response contains commands_only errors', () => {
      it('only returns commands_only errors', () => {
        const errorMessages = createNoteErrorMessages(
          {
            errors: {
              commands_only: ['commands_only error 1', 'commands_only error 2'],
              base: ['base error 1'],
            },
          },
          HTTP_STATUS_UNPROCESSABLE_ENTITY,
        );

        expect(errorMessages).toStrictEqual(['commands_only error 1', 'commands_only error 2']);
      });
    });
  });
});

describe('updateNoteErrorMessage', () => {
  describe('with server error', () => {
    it('returns error message with server error', () => {
      const error = 'error 1 and error 2';
      const errorMessage = updateNoteErrorMessage({ response: { data: { errors: error } } });

      expect(errorMessage).toEqual(sprintf(UPDATE_COMMENT_FORM.error, { reason: error }));
    });
  });

  describe('without server error', () => {
    it('returns generic error message', () => {
      const errorMessage = updateNoteErrorMessage(null);

      expect(errorMessage).toEqual(UPDATE_COMMENT_FORM.defaultError);
    });
  });
});