debian-mirror-gitlab/spec/frontend/projects/project_new_spec.js

256 lines
7.7 KiB
JavaScript
Raw Normal View History

2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-03-08 18:12:59 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2020-10-24 23:57:45 +05:30
import projectNew from '~/projects/project_new';
2022-08-27 11:52:29 +05:30
import { mockTracking, triggerEvent, unmockTracking } from 'helpers/tracking_helper';
2017-09-10 17:25:29 +05:30
describe('New Project', () => {
let $projectImportUrl;
let $projectPath;
2018-11-20 20:47:30 +05:30
let $projectName;
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
const mockKeyup = (el) => el.dispatchEvent(new KeyboardEvent('keyup'));
const mockChange = (el) => el.dispatchEvent(new Event('change'));
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2022-08-27 11:52:29 +05:30
<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" />
<input id="project_path" />
2019-07-31 22:56:46 +05:30
</div>
2022-08-27 11:52:29 +05:30
<div class="js-user-readme-repo"></div>
<button class="js-create-project-button"/>
</form>
2018-03-17 18:26:18 +05:30
</div>
</div>
2017-09-10 17:25:29 +05:30
`);
2022-07-23 23:45:48 +05:30
$projectImportUrl = document.querySelector('#project_import_url');
$projectPath = document.querySelector('#project_path');
$projectName = document.querySelector('#project_name');
2017-09-10 17:25:29 +05:30
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2022-07-23 23:45:48 +05:30
const setValueAndTriggerEvent = (el, value, event) => {
event(el);
el.value = value;
};
2022-08-27 11:52:29 +05:30
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();
});
});
2017-09-10 17:25:29 +05:30
describe('deriveProjectPathFromUrl', () => {
2020-07-28 23:09:34 +05:30
const dummyImportUrl = `${TEST_HOST}/dummy/import/url.git`;
2017-09-10 17:25:29 +05:30
beforeEach(() => {
projectNew.bindEvents();
2022-07-23 23:45:48 +05:30
setValueAndTriggerEvent($projectPath, dummyImportUrl, mockKeyup);
2017-09-10 17:25:29 +05:30
});
it('does not change project path for disabled $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.setAttribute('disabled', true);
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual(dummyImportUrl);
2017-09-10 17:25:29 +05:30
});
describe('for enabled $projectImportUrl', () => {
beforeEach(() => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.setAttribute('disabled', false);
2017-09-10 17:25:29 +05:30
});
it('does not change project path if it is set by user', () => {
2022-07-23 23:45:48 +05:30
mockKeyup($projectPath);
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual(dummyImportUrl);
2017-09-10 17:25:29 +05:30
});
it('does not change project path for empty $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = '';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual(dummyImportUrl);
2017-09-10 17:25:29 +05:30
});
it('does not change project path for whitespace $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = ' ';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual(dummyImportUrl);
2017-09-10 17:25:29 +05:30
});
it('does not change project path for $projectImportUrl without slashes', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = 'has-no-slash';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual(dummyImportUrl);
2017-09-10 17:25:29 +05:30
});
it('changes project path to last $projectImportUrl component', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = '/this/is/last';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('last');
2017-09-10 17:25:29 +05:30
});
it('ignores trailing slashes in $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = '/has/trailing/slash/';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('slash');
2017-09-10 17:25:29 +05:30
});
it('ignores fragment identifier in $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = '/this/has/a#fragment-identifier/';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('a');
2017-09-10 17:25:29 +05:30
});
it('ignores query string in $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = '/url/with?query=string';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('with');
2017-09-10 17:25:29 +05:30
});
it('ignores trailing .git in $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = '/repository.git';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('repository');
2017-09-10 17:25:29 +05:30
});
it('changes project path for HTTPS URL in $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = 'https://gitlab.company.com/group/project.git';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('project');
2017-09-10 17:25:29 +05:30
});
it('changes project path for SSH URL in $projectImportUrl', () => {
2022-07-23 23:45:48 +05:30
$projectImportUrl.value = 'git@gitlab.com:gitlab-org/gitlab-ce.git';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
projectNew.deriveProjectPathFromUrl($projectImportUrl);
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('gitlab-ce');
2017-09-10 17:25:29 +05:30
});
});
});
2018-11-20 20:47:30 +05:30
describe('deriveSlugFromProjectName', () => {
beforeEach(() => {
projectNew.bindEvents();
2022-07-23 23:45:48 +05:30
setValueAndTriggerEvent($projectName, '', mockKeyup);
2018-11-20 20:47:30 +05:30
});
it('converts project name to lower case and dash-limited slug', () => {
const dummyProjectName = 'My Awesome Project';
2022-07-23 23:45:48 +05:30
$projectName.value = dummyProjectName;
2018-11-20 20:47:30 +05:30
projectNew.onProjectNameChange($projectName, $projectPath);
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('my-awesome-project');
2018-11-20 20:47:30 +05:30
});
it('does not add additional dashes in the slug if the project name already contains dashes', () => {
const dummyProjectName = 'My-Dash-Delimited Awesome Project';
2022-07-23 23:45:48 +05:30
$projectName.value = dummyProjectName;
2018-11-20 20:47:30 +05:30
projectNew.onProjectNameChange($projectName, $projectPath);
2022-07-23 23:45:48 +05:30
expect($projectPath.value).toEqual('my-dash-delimited-awesome-project');
2018-11-20 20:47:30 +05:30
});
});
2020-03-13 15:44:24 +05:30
describe('derivesProjectNameFromSlug', () => {
const dummyProjectPath = 'my-awesome-project';
const dummyProjectName = 'Original Awesome Project';
beforeEach(() => {
projectNew.bindEvents();
2022-07-23 23:45:48 +05:30
setValueAndTriggerEvent($projectPath, '', mockChange);
2020-03-13 15:44:24 +05:30
});
it('converts slug to humanized project name', () => {
2022-07-23 23:45:48 +05:30
$projectPath.value = dummyProjectPath;
mockChange($projectPath);
2020-03-13 15:44:24 +05:30
projectNew.onProjectPathChange($projectName, $projectPath);
2022-07-23 23:45:48 +05:30
expect($projectName.value).toEqual('My Awesome Project');
2020-03-13 15:44:24 +05:30
});
it('does not convert slug to humanized project name if a project name already exists', () => {
2022-07-23 23:45:48 +05:30
$projectName.value = dummyProjectName;
$projectPath.value = dummyProjectPath;
2020-03-13 15:44:24 +05:30
projectNew.onProjectPathChange(
$projectName,
$projectPath,
2022-07-23 23:45:48 +05:30
$projectName.value.trim().length > 0,
2020-03-13 15:44:24 +05:30
);
2022-07-23 23:45:48 +05:30
expect($projectName.value).toEqual(dummyProjectName);
2020-03-13 15:44:24 +05:30
});
});
2017-09-10 17:25:29 +05:30
});