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
|
import { times } from 'lodash';
import { nextTick } from 'vue';
import { GlFormGroup } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import SnippetBlobActionsEdit from '~/snippets/components/snippet_blob_actions_edit.vue';
import SnippetBlobEdit from '~/snippets/components/snippet_blob_edit.vue';
import {
SNIPPET_MAX_BLOBS,
SNIPPET_BLOB_ACTION_CREATE,
SNIPPET_BLOB_ACTION_MOVE,
} from '~/snippets/constants';
import { s__ } from '~/locale';
import { testEntries, createBlobFromTestEntry } from '../test_utils';
const TEST_BLOBS = [
createBlobFromTestEntry(testEntries.updated),
createBlobFromTestEntry(testEntries.deleted),
];
const TEST_BLOBS_UNLOADED = TEST_BLOBS.map((blob) => ({ ...blob, content: '', isLoaded: false }));
describe('snippets/components/snippet_blob_actions_edit', () => {
let wrapper;
const createComponent = (props = {}) => {
wrapper = shallowMount(SnippetBlobActionsEdit, {
propsData: {
initBlobs: TEST_BLOBS,
...props,
},
});
};
const findLabel = () => wrapper.findComponent(GlFormGroup);
const findBlobEdits = () => wrapper.findAllComponents(SnippetBlobEdit);
const findBlobsData = () =>
findBlobEdits().wrappers.map((x) => ({
blob: x.props('blob'),
classes: x.classes(),
}));
const findFirstBlobEdit = () => findBlobEdits().at(0);
const findAddButton = () => wrapper.find('[data-testid="add_button"]');
const getLastActions = () => {
const events = wrapper.emitted().actions;
return events[events.length - 1]?.[0];
};
const buildBlobsDataExpectation = (blobs) =>
blobs.map((blob, index) => ({
blob: {
...blob,
id: expect.stringMatching('blob_local_'),
},
classes: index > 0 ? ['gl-mt-3'] : [],
}));
const triggerBlobDelete = (idx) => findBlobEdits().at(idx).vm.$emit('delete');
const triggerBlobUpdate = (idx, props) => findBlobEdits().at(idx).vm.$emit('blob-updated', props);
describe('multi-file snippets rendering', () => {
beforeEach(() => {
createComponent();
});
it('renders label', () => {
expect(findLabel().attributes('label')).toBe('Files');
});
it(`renders delete button (show=true)`, () => {
expect(findFirstBlobEdit().props()).toMatchObject({
showDelete: true,
canDelete: true,
});
});
it(`renders add button (show=true)`, () => {
expect(findAddButton().exists()).toBe(true);
});
});
describe('with default', () => {
beforeEach(() => {
createComponent();
});
it('emits no actions', () => {
expect(getLastActions()).toEqual([]);
});
it('shows blobs', () => {
expect(findBlobsData()).toEqual(buildBlobsDataExpectation(TEST_BLOBS_UNLOADED));
});
it('shows add button', () => {
const button = findAddButton();
expect(button.text()).toBe(`Add another file ${TEST_BLOBS.length}/${SNIPPET_MAX_BLOBS}`);
expect(button.props('disabled')).toBe(false);
});
describe('when add is clicked', () => {
beforeEach(() => {
findAddButton().vm.$emit('click');
});
it('adds blob with empty content', () => {
expect(findBlobsData()).toEqual(
buildBlobsDataExpectation([
...TEST_BLOBS_UNLOADED,
{
content: '',
isLoaded: true,
path: '',
},
]),
);
});
it('emits action', () => {
expect(getLastActions()).toEqual([
expect.objectContaining({
action: SNIPPET_BLOB_ACTION_CREATE,
}),
]);
});
});
describe('when blob is deleted', () => {
beforeEach(() => {
triggerBlobDelete(1);
});
it('removes blob', () => {
expect(findBlobsData()).toEqual(buildBlobsDataExpectation(TEST_BLOBS_UNLOADED.slice(0, 1)));
});
it('emits action', () => {
expect(getLastActions()).toEqual([
expect.objectContaining({
...testEntries.deleted.diff,
content: '',
}),
]);
});
});
describe('when blob changes path', () => {
beforeEach(() => {
triggerBlobUpdate(0, { path: 'new/path' });
});
it('renames blob', () => {
expect(findBlobsData()[0]).toMatchObject({
blob: {
path: 'new/path',
},
});
});
it('emits action', () => {
expect(getLastActions()).toMatchObject([
{
action: SNIPPET_BLOB_ACTION_MOVE,
filePath: 'new/path',
previousPath: testEntries.updated.diff.filePath,
},
]);
});
});
describe('when blob emits new content', () => {
const { content } = testEntries.updated.diff;
const originalContent = `${content}\noriginal content\n`;
beforeEach(() => {
triggerBlobUpdate(0, { content: originalContent });
});
it('loads new content', () => {
expect(findBlobsData()[0]).toMatchObject({
blob: {
content: originalContent,
isLoaded: true,
},
});
});
it('does not emit an action', () => {
expect(getLastActions()).toEqual([]);
});
it('emits an action when content changes again', async () => {
triggerBlobUpdate(0, { content });
await nextTick();
expect(getLastActions()).toEqual([testEntries.updated.diff]);
});
});
});
describe('with 1 blob', () => {
beforeEach(() => {
createComponent({ initBlobs: [createBlobFromTestEntry(testEntries.created)] });
});
it('disables delete button', () => {
expect(findBlobEdits()).toHaveLength(1);
expect(findBlobEdits().at(0).props()).toMatchObject({
showDelete: true,
canDelete: false,
});
});
describe(`when added ${SNIPPET_MAX_BLOBS} files`, () => {
let addButton;
beforeEach(() => {
addButton = findAddButton();
times(SNIPPET_MAX_BLOBS - 1, () => addButton.vm.$emit('click'));
});
it('should have blobs', () => {
expect(findBlobsData()).toHaveLength(SNIPPET_MAX_BLOBS);
});
it('should disable add button', () => {
expect(addButton.props('disabled')).toBe(true);
});
});
});
describe('with 0 init blob', () => {
beforeEach(() => {
createComponent({ initBlobs: [] });
});
it('shows 1 blob by default', () => {
expect(findBlobsData()).toEqual([
expect.objectContaining({
blob: {
id: expect.stringMatching('blob_local_'),
content: '',
path: '',
isLoaded: true,
},
}),
]);
});
it('emits create action', () => {
expect(getLastActions()).toEqual([
{
action: SNIPPET_BLOB_ACTION_CREATE,
content: '',
filePath: '',
previousPath: '',
},
]);
});
});
describe(`with ${SNIPPET_MAX_BLOBS} files`, () => {
beforeEach(() => {
const initBlobs = Array(SNIPPET_MAX_BLOBS)
.fill(1)
.map(() => createBlobFromTestEntry(testEntries.created));
createComponent({ initBlobs });
});
it('should have blobs', () => {
expect(findBlobsData()).toHaveLength(SNIPPET_MAX_BLOBS);
});
it('should disable add button', () => {
expect(findAddButton().props('disabled')).toBe(true);
});
});
describe('isValid prop', () => {
const validationMessage = s__(
"Snippets|Snippets can't contain empty files. Ensure all files have content, or delete them.",
);
describe('when not present', () => {
it('sets the label validation state to true', () => {
createComponent();
const label = findLabel();
expect(Boolean(label.attributes('state'))).toEqual(true);
expect(label.attributes('invalid-feedback')).toEqual(validationMessage);
});
});
describe('when present', () => {
it('sets the label validation state to the value', () => {
createComponent({ isValid: false });
const label = findLabel();
expect(Boolean(label.attributes('state'))).toEqual(false);
expect(label.attributes('invalid-feedback')).toEqual(validationMessage);
});
});
});
});
|