project_new_spec.js 3.89 KB
Newer Older
1
import projectNew from '~/pages/projects/shared/project_new';
2 3 4 5 6 7 8

describe('New Project', () => {
  let $projectImportUrl;
  let $projectPath;

  beforeEach(() => {
    setFixtures(`
9 10 11 12 13 14
      <div class='toggle-import-form'>
        <div class='import-url-data'>
          <input id="project_import_url" />
          <input id="project_path" />
        </div>
      </div>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    `);

    $projectImportUrl = $('#project_import_url');
    $projectPath = $('#project_path');
  });

  describe('deriveProjectPathFromUrl', () => {
    const dummyImportUrl = `${gl.TEST_HOST}/dummy/import/url.git`;

    beforeEach(() => {
      projectNew.bindEvents();
      $projectPath.val('').keyup().val(dummyImportUrl);
    });

    it('does not change project path for disabled $projectImportUrl', () => {
      $projectImportUrl.attr('disabled', true);

32
      projectNew.deriveProjectPathFromUrl($projectImportUrl);
33 34 35 36 37 38 39 40 41 42 43 44

      expect($projectPath.val()).toEqual(dummyImportUrl);
    });

    describe('for enabled $projectImportUrl', () => {
      beforeEach(() => {
        $projectImportUrl.attr('disabled', false);
      });

      it('does not change project path if it is set by user', () => {
        $projectPath.keyup();

45
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
46 47 48 49 50 51 52

        expect($projectPath.val()).toEqual(dummyImportUrl);
      });

      it('does not change project path for empty $projectImportUrl', () => {
        $projectImportUrl.val('');

53
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
54 55 56 57 58 59 60

        expect($projectPath.val()).toEqual(dummyImportUrl);
      });

      it('does not change project path for whitespace $projectImportUrl', () => {
        $projectImportUrl.val('   ');

61
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
62 63 64 65 66 67 68

        expect($projectPath.val()).toEqual(dummyImportUrl);
      });

      it('does not change project path for $projectImportUrl without slashes', () => {
        $projectImportUrl.val('has-no-slash');

69
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
70 71 72 73 74 75 76

        expect($projectPath.val()).toEqual(dummyImportUrl);
      });

      it('changes project path to last $projectImportUrl component', () => {
        $projectImportUrl.val('/this/is/last');

77
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
78 79 80 81 82 83 84

        expect($projectPath.val()).toEqual('last');
      });

      it('ignores trailing slashes in $projectImportUrl', () => {
        $projectImportUrl.val('/has/trailing/slash/');

85
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
86 87 88 89 90 91 92

        expect($projectPath.val()).toEqual('slash');
      });

      it('ignores fragment identifier in $projectImportUrl', () => {
        $projectImportUrl.val('/this/has/a#fragment-identifier/');

93
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
94 95 96 97 98 99 100

        expect($projectPath.val()).toEqual('a');
      });

      it('ignores query string in $projectImportUrl', () => {
        $projectImportUrl.val('/url/with?query=string');

101
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
102 103 104 105 106 107 108

        expect($projectPath.val()).toEqual('with');
      });

      it('ignores trailing .git in $projectImportUrl', () => {
        $projectImportUrl.val('/repository.git');

109
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
110 111 112 113 114 115 116

        expect($projectPath.val()).toEqual('repository');
      });

      it('changes project path for HTTPS URL in $projectImportUrl', () => {
        $projectImportUrl.val('https://username:password@gitlab.company.com/group/project.git');

117
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
118 119 120 121 122 123 124

        expect($projectPath.val()).toEqual('project');
      });

      it('changes project path for SSH URL in $projectImportUrl', () => {
        $projectImportUrl.val('git@gitlab.com:gitlab-org/gitlab-ce.git');

125
        projectNew.deriveProjectPathFromUrl($projectImportUrl);
126 127 128 129 130 131

        expect($projectPath.val()).toEqual('gitlab-ce');
      });
    });
  });
});