summaryrefslogtreecommitdiff
path: root/spec/frontend/new_branch_spec.js
blob: 7e6b8a78d4f5c3ead3e8e607943329005a346e5e (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import $ from 'jquery';
import NewBranchForm from '~/new_branch_form';

describe('Branch', () => {
  let testContext;

  beforeEach(() => {
    testContext = {};
  });

  describe('create a new branch', () => {
    preloadFixtures('branches/new_branch.html');

    function fillNameWith(value) {
      $('.js-branch-name').val(value).trigger('blur');
    }

    function expectToHaveError(error) {
      expect($('.js-branch-name-error span').text()).toEqual(error);
    }

    beforeEach(() => {
      loadFixtures('branches/new_branch.html');
      $('form').on('submit', (e) => e.preventDefault());
      testContext.form = new NewBranchForm($('.js-create-branch-form'), []);
    });

    it("can't start with a dot", () => {
      fillNameWith('.foo');
      expectToHaveError("can't start with '.'");
    });

    it("can't start with a slash", () => {
      fillNameWith('/foo');
      expectToHaveError("can't start with '/'");
    });

    it("can't have two consecutive dots", () => {
      fillNameWith('foo..bar');
      expectToHaveError("can't contain '..'");
    });

    it("can't have spaces anywhere", () => {
      fillNameWith(' foo');
      expectToHaveError("can't contain spaces");
      fillNameWith('foo bar');
      expectToHaveError("can't contain spaces");
      fillNameWith('foo ');
      expectToHaveError("can't contain spaces");
    });

    it("can't have ~ anywhere", () => {
      fillNameWith('~foo');
      expectToHaveError("can't contain '~'");
      fillNameWith('foo~bar');
      expectToHaveError("can't contain '~'");
      fillNameWith('foo~');
      expectToHaveError("can't contain '~'");
    });

    it("can't have tilde anwhere", () => {
      fillNameWith('~foo');
      expectToHaveError("can't contain '~'");
      fillNameWith('foo~bar');
      expectToHaveError("can't contain '~'");
      fillNameWith('foo~');
      expectToHaveError("can't contain '~'");
    });

    it("can't have caret anywhere", () => {
      fillNameWith('^foo');
      expectToHaveError("can't contain '^'");
      fillNameWith('foo^bar');
      expectToHaveError("can't contain '^'");
      fillNameWith('foo^');
      expectToHaveError("can't contain '^'");
    });

    it("can't have : anywhere", () => {
      fillNameWith(':foo');
      expectToHaveError("can't contain ':'");
      fillNameWith('foo:bar');
      expectToHaveError("can't contain ':'");
      fillNameWith(':foo');
      expectToHaveError("can't contain ':'");
    });

    it("can't have question mark anywhere", () => {
      fillNameWith('?foo');
      expectToHaveError("can't contain '?'");
      fillNameWith('foo?bar');
      expectToHaveError("can't contain '?'");
      fillNameWith('foo?');
      expectToHaveError("can't contain '?'");
    });

    it("can't have asterisk anywhere", () => {
      fillNameWith('*foo');
      expectToHaveError("can't contain '*'");
      fillNameWith('foo*bar');
      expectToHaveError("can't contain '*'");
      fillNameWith('foo*');
      expectToHaveError("can't contain '*'");
    });

    it("can't have open bracket anywhere", () => {
      fillNameWith('[foo');
      expectToHaveError("can't contain '['");
      fillNameWith('foo[bar');
      expectToHaveError("can't contain '['");
      fillNameWith('foo[');
      expectToHaveError("can't contain '['");
    });

    it("can't have a backslash anywhere", () => {
      fillNameWith('\\foo');
      expectToHaveError("can't contain '\\'");
      fillNameWith('foo\\bar');
      expectToHaveError("can't contain '\\'");
      fillNameWith('foo\\');
      expectToHaveError("can't contain '\\'");
    });

    it("can't contain a sequence @{ anywhere", () => {
      fillNameWith('@{foo');
      expectToHaveError("can't contain '@{'");
      fillNameWith('foo@{bar');
      expectToHaveError("can't contain '@{'");
      fillNameWith('foo@{');
      expectToHaveError("can't contain '@{'");
    });

    it("can't have consecutive slashes", () => {
      fillNameWith('foo//bar');
      expectToHaveError("can't contain consecutive slashes");
    });

    it("can't end with a slash", () => {
      fillNameWith('foo/');
      expectToHaveError("can't end in '/'");
    });

    it("can't end with a dot", () => {
      fillNameWith('foo.');
      expectToHaveError("can't end in '.'");
    });

    it("can't end with .lock", () => {
      fillNameWith('foo.lock');
      expectToHaveError("can't end in '.lock'");
    });

    it("can't be the single character @", () => {
      fillNameWith('@');
      expectToHaveError("can't be '@'");
    });

    it('concatenates all error messages', () => {
      fillNameWith('/foo bar?~.');
      expectToHaveError("can't start with '/', can't contain spaces, '?', '~', can't end in '.'");
    });

    it("doesn't duplicate error messages", () => {
      fillNameWith('?foo?bar?zoo?');
      expectToHaveError("can't contain '?'");
    });

    it('removes the error message when is a valid name', () => {
      fillNameWith('foo?bar');

      expect($('.js-branch-name-error span').length).toEqual(1);
      fillNameWith('foobar');

      expect($('.js-branch-name-error span').length).toEqual(0);
    });

    it('can have dashes anywhere', () => {
      fillNameWith('-foo-bar-zoo-');

      expect($('.js-branch-name-error span').length).toEqual(0);
    });

    it('can have underscores anywhere', () => {
      fillNameWith('_foo_bar_zoo_');

      expect($('.js-branch-name-error span').length).toEqual(0);
    });

    it('can have numbers anywhere', () => {
      fillNameWith('1foo2bar3zoo4');

      expect($('.js-branch-name-error span').length).toEqual(0);
    });

    it('can be only letters', () => {
      fillNameWith('foo');

      expect($('.js-branch-name-error span').length).toEqual(0);
    });
  });
});