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

getters_spec.js « store « merge_conflicts « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7a26a2bba6a86fbce47c21ee9980d7bd9490ddd7 (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
import {
  CONFLICT_TYPES,
  EDIT_RESOLVE_MODE,
  INTERACTIVE_RESOLVE_MODE,
} from '~/merge_conflicts/constants';
import * as getters from '~/merge_conflicts/store/getters';
import realState from '~/merge_conflicts/store/state';

describe('Merge Conflicts getters', () => {
  let state;

  beforeEach(() => {
    state = realState();
  });

  describe('getConflictsCount', () => {
    it('returns zero when there are no files', () => {
      state.conflictsData.files = [];

      expect(getters.getConflictsCount(state)).toBe(0);
    });

    it(`counts the number of sections in files of type ${CONFLICT_TYPES.TEXT}`, () => {
      state.conflictsData.files = [
        { sections: [{ conflict: true }], type: CONFLICT_TYPES.TEXT },
        { sections: [{ conflict: true }, { conflict: true }], type: CONFLICT_TYPES.TEXT },
      ];
      expect(getters.getConflictsCount(state)).toBe(3);
    });

    it(`counts the number of file in files  not of type ${CONFLICT_TYPES.TEXT}`, () => {
      state.conflictsData.files = [
        { sections: [{ conflict: true }], type: '' },
        { sections: [{ conflict: true }, { conflict: true }], type: '' },
      ];
      expect(getters.getConflictsCount(state)).toBe(2);
    });
  });

  describe('getConflictsCountText', () => {
    it('with one conflicts', () => {
      const getConflictsCount = 1;

      expect(getters.getConflictsCountText(state, { getConflictsCount })).toBe('1 conflict');
    });

    it('with more than one conflicts', () => {
      const getConflictsCount = 3;

      expect(getters.getConflictsCountText(state, { getConflictsCount })).toBe('3 conflicts');
    });
  });

  describe('isReadyToCommit', () => {
    it('return false when isSubmitting is true', () => {
      state.conflictsData.files = [];
      state.isSubmitting = true;
      state.conflictsData.commitMessage = 'foo';

      expect(getters.isReadyToCommit(state)).toBe(false);
    });

    it('returns false when has no commit message', () => {
      state.conflictsData.files = [];
      state.isSubmitting = false;
      state.conflictsData.commitMessage = '';

      expect(getters.isReadyToCommit(state)).toBe(false);
    });

    it('returns true when all conflicts are resolved and is not submitting and we have a commitMessage', () => {
      state.conflictsData.files = [
        {
          resolveMode: INTERACTIVE_RESOLVE_MODE,
          type: CONFLICT_TYPES.TEXT,
          sections: [{ conflict: true }],
          resolutionData: { foo: 'bar' },
        },
      ];
      state.isSubmitting = false;
      state.conflictsData.commitMessage = 'foo';

      expect(getters.isReadyToCommit(state)).toBe(true);
    });

    describe('unresolved', () => {
      it(`files with resolvedMode set to ${EDIT_RESOLVE_MODE} and empty count as unresolved`, () => {
        state.conflictsData.files = [
          { content: '', resolveMode: EDIT_RESOLVE_MODE },
          { content: 'foo' },
        ];
        state.isSubmitting = false;
        state.conflictsData.commitMessage = 'foo';

        expect(getters.isReadyToCommit(state)).toBe(false);
      });

      it(`in files with resolvedMode = ${INTERACTIVE_RESOLVE_MODE} we count resolvedConflicts vs unresolved ones`, () => {
        state.conflictsData.files = [
          {
            resolveMode: INTERACTIVE_RESOLVE_MODE,
            type: CONFLICT_TYPES.TEXT,
            sections: [{ conflict: true }],
            resolutionData: {},
          },
        ];
        state.isSubmitting = false;
        state.conflictsData.commitMessage = 'foo';

        expect(getters.isReadyToCommit(state)).toBe(false);
      });
    });
  });

  describe('getCommitButtonText', () => {
    it('when is submitting', () => {
      state.isSubmitting = true;
      expect(getters.getCommitButtonText(state)).toBe('Committing...');
    });

    it('when is not submitting', () => {
      expect(getters.getCommitButtonText(state)).toBe('Commit to source branch');
    });
  });

  describe('getCommitData', () => {
    it('returns commit data', () => {
      const baseFile = {
        new_path: 'new_path',
        old_path: 'new_path',
      };

      state.conflictsData.commitMessage = 'foo';
      state.conflictsData.files = [
        {
          ...baseFile,
          resolveMode: INTERACTIVE_RESOLVE_MODE,
          type: CONFLICT_TYPES.TEXT,
          sections: [{ conflict: true }],
          resolutionData: { bar: 'baz' },
        },
        {
          ...baseFile,
          resolveMode: EDIT_RESOLVE_MODE,
          type: CONFLICT_TYPES.TEXT,
          content: 'resolve_mode_content',
        },
        {
          ...baseFile,
          type: CONFLICT_TYPES.TEXT_EDITOR,
          content: 'text_editor_content',
        },
      ];

      expect(getters.getCommitData(state)).toStrictEqual({
        commit_message: 'foo',
        files: [
          { ...baseFile, sections: { bar: 'baz' } },
          { ...baseFile, content: 'resolve_mode_content' },
          { ...baseFile, content: 'text_editor_content' },
        ],
      });
    });
  });

  describe('fileTextTypePresent', () => {
    it(`returns true if there is a file with type ${CONFLICT_TYPES.TEXT}`, () => {
      state.conflictsData.files = [{ type: CONFLICT_TYPES.TEXT }];

      expect(getters.fileTextTypePresent(state)).toBe(true);
    });
    it(`returns false if there is no file with type ${CONFLICT_TYPES.TEXT}`, () => {
      state.conflictsData.files = [{ type: CONFLICT_TYPES.TEXT_EDITOR }];

      expect(getters.fileTextTypePresent(state)).toBe(false);
    });
  });

  describe('getFileIndex', () => {
    it(`returns the index of a file from it's blob path`, () => {
      const blobPath = 'blobPath/foo';
      state.conflictsData.files = [{ foo: 'bar' }, { baz: 'foo', blobPath }];

      expect(getters.getFileIndex(state)({ blobPath })).toBe(1);
    });
  });
});