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
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { TEST_HOST } from 'helpers/test_constants';
import projectNew from '~/projects/project_new';
import { checkRules } from '~/projects/project_name_rules';
import { mockTracking, triggerEvent, unmockTracking } from 'helpers/tracking_helper';
describe('New Project', () => {
let $projectImportUrl;
let $projectPath;
let $projectName;
let $projectNameError;
let $projectNameDescription;
const mockKeyup = (el) => el.dispatchEvent(new KeyboardEvent('keyup'));
const mockChange = (el) => el.dispatchEvent(new Event('change'));
const mockSubmit = () =>
document.getElementById('new_project').dispatchEvent(new Event('submit'));
beforeEach(() => {
setHTMLFixture(`
<div class="tab-pane active">
<div class='toggle-import-form'>
<form id="new_project">
<div class='import-url-data'>
<div class="form-group">
<input id="project_import_url" />
</div>
<div id="import-url-auth-method">
<div class="form-group">
<input id="project-import-url-user" />
</div>
<div class="form-group">
<input id="project_import_url_password" />
</div>
</div>
<input id="project_name" />
<small id="js-project-name-description" />
<div class="gl-field-error gl-display-none" id="js-project-name-error" />
<input id="project_path" />
</div>
<div class="js-user-readme-repo"></div>
<button class="js-create-project-button"/>
</form>
</div>
</div>
`);
$projectImportUrl = document.querySelector('#project_import_url');
$projectPath = document.querySelector('#project_path');
$projectName = document.querySelector('#project_name');
$projectNameError = document.querySelector('#js-project-name-error');
$projectNameDescription = document.querySelector('#js-project-name-description');
});
afterEach(() => {
resetHTMLFixture();
});
const setValueAndTriggerEvent = (el, value, event) => {
event(el);
el.value = value;
};
describe('tracks manual path input', () => {
let trackingSpy;
beforeEach(() => {
trackingSpy = mockTracking('_category_', undefined, jest.spyOn);
projectNew.bindEvents();
$projectPath.oldInputValue = '_old_value_';
});
afterEach(() => {
unmockTracking();
});
it('tracks the event', () => {
$projectPath.value = '_new_value_';
triggerEvent($projectPath, 'blur');
expect(trackingSpy).toHaveBeenCalledWith(undefined, 'user_input_path_slug', {
label: 'new_project_form',
});
});
it('does not track the event when there has been no change', () => {
$projectPath.value = '_old_value_';
triggerEvent($projectPath, 'blur');
expect(trackingSpy).not.toHaveBeenCalled();
});
});
describe('tracks manual name input', () => {
beforeEach(() => {
projectNew.bindEvents();
});
afterEach(() => {
unmockTracking();
});
it('no error message by default', () => {
expect($projectNameError.classList.contains('gl-display-none')).toBe(true);
});
it('show error message if name is validate', () => {
$projectName.value = '.validate!Name';
triggerEvent($projectName, 'change');
expect($projectNameError.innerText).toBe(
'Name must start with a letter, digit, emoji, or underscore.',
);
expect($projectNameError.classList.contains('gl-display-none')).toBe(false);
expect($projectNameDescription.classList.contains('gl-display-none')).toBe(true);
});
});
describe('project name rule', () => {
describe("Name must start with a letter, digit, emoji, or '_'", () => {
const errormsg = 'Name must start with a letter, digit, emoji, or underscore.';
it("'.foo' should error", () => {
const text = '.foo';
expect(checkRules(text)).toBe(errormsg);
});
it('_foo should passed', () => {
const text = '_foo';
expect(checkRules(text)).toBe('');
});
});
describe("Name can contain only letters, digits, emojis, '_', '.', '+', dashes, or spaces", () => {
const errormsg =
'Name can contain only lowercase or uppercase letters, digits, emojis, spaces, dots, underscores, dashes, or pluses.';
it("'foo(#^.^#)foo' should error", () => {
const text = 'foo(#^.^#)foo';
expect(checkRules(text)).toBe(errormsg);
});
it("'foo123😊_.+- ' should passed", () => {
const text = 'foo123😊_.+- ';
expect(checkRules(text)).toBe('');
});
});
});
describe('deriveProjectPathFromUrl', () => {
const dummyImportUrl = `${TEST_HOST}/dummy/import/url.git`;
beforeEach(() => {
projectNew.bindEvents();
setValueAndTriggerEvent($projectPath, dummyImportUrl, mockKeyup);
});
it('does not change project path for disabled $projectImportUrl', () => {
$projectImportUrl.setAttribute('disabled', true);
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual(dummyImportUrl);
});
describe('for enabled $projectImportUrl', () => {
beforeEach(() => {
$projectImportUrl.setAttribute('disabled', false);
});
it('does not change project path if it is set by user', () => {
mockKeyup($projectPath);
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual(dummyImportUrl);
});
it('does not change project path for empty $projectImportUrl', () => {
$projectImportUrl.value = '';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual(dummyImportUrl);
});
it('does not change project path for whitespace $projectImportUrl', () => {
$projectImportUrl.value = ' ';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual(dummyImportUrl);
});
it('does not change project path for $projectImportUrl without slashes', () => {
$projectImportUrl.value = 'has-no-slash';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual(dummyImportUrl);
});
it('changes project path to last $projectImportUrl component', () => {
$projectImportUrl.value = '/this/is/last';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('last');
});
it('ignores trailing slashes in $projectImportUrl', () => {
$projectImportUrl.value = '/has/trailing/slash/';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('slash');
});
it('ignores fragment identifier in $projectImportUrl', () => {
$projectImportUrl.value = '/this/has/a#fragment-identifier/';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('a');
});
it('ignores query string in $projectImportUrl', () => {
$projectImportUrl.value = '/url/with?query=string';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('with');
});
it('ignores trailing .git in $projectImportUrl', () => {
$projectImportUrl.value = '/repository.git';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('repository');
});
it('changes project path for HTTPS URL in $projectImportUrl', () => {
$projectImportUrl.value = 'https://gitlab.company.com/group/project.git';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('project');
});
it('changes project path for SSH URL in $projectImportUrl', () => {
$projectImportUrl.value = 'git@gitlab.com:gitlab-org/gitlab-ce.git';
projectNew.deriveProjectPathFromUrl($projectImportUrl);
expect($projectPath.value).toEqual('gitlab-ce');
});
});
});
describe('deriveSlugFromProjectName', () => {
beforeEach(() => {
projectNew.bindEvents();
setValueAndTriggerEvent($projectName, '', mockKeyup);
});
it('converts project name to lower case and dash-limited slug', () => {
const dummyProjectName = 'My Awesome Project';
$projectName.value = dummyProjectName;
projectNew.onProjectNameChange($projectName, $projectPath);
expect($projectPath.value).toEqual('my-awesome-project');
});
it('does not add additional dashes in the slug if the project name already contains dashes', () => {
const dummyProjectName = 'My-Dash-Delimited Awesome Project';
$projectName.value = dummyProjectName;
projectNew.onProjectNameChange($projectName, $projectPath);
expect($projectPath.value).toEqual('my-dash-delimited-awesome-project');
});
});
describe('derivesProjectNameFromSlug', () => {
const dummyProjectPath = 'my-awesome-project';
const dummyProjectName = 'Original Awesome Project';
beforeEach(() => {
projectNew.bindEvents();
setValueAndTriggerEvent($projectPath, '', mockChange);
});
it('converts slug to humanized project name', () => {
$projectPath.value = dummyProjectPath;
mockChange($projectPath);
projectNew.onProjectPathChange($projectName, $projectPath);
expect($projectName.value).toEqual('My Awesome Project');
});
it('does not convert slug to humanized project name if a project name already exists', () => {
$projectName.value = dummyProjectName;
$projectPath.value = dummyProjectPath;
projectNew.onProjectPathChange(
$projectName,
$projectPath,
$projectName.value.trim().length > 0,
);
expect($projectName.value).toEqual(dummyProjectName);
});
});
describe('project path trimming', () => {
beforeEach(() => {
projectNew.bindEvents();
});
describe('when the project path field is filled in', () => {
const dirtyProjectPath = ' my-awesome-project ';
const cleanProjectPath = dirtyProjectPath.trim();
beforeEach(() => {
$projectPath.value = dirtyProjectPath;
mockSubmit();
});
it('trims the project path on submit', () => {
expect($projectPath.value).not.toBe(dirtyProjectPath);
expect($projectPath.value).toBe(cleanProjectPath);
});
});
describe('when the project path field is left empty', () => {
beforeEach(() => {
mockSubmit();
});
it('leaves the field empty', () => {
expect($projectPath.value).toBe('');
});
});
});
});
|