| 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
 | 'use strict';
describe('widget: input', function() {
  var compile = null, element = null, scope = null, defer = null;
  var $compile = null;
  var doc = null;
  beforeEach(inject(function($rootScope, $compile) {
    scope = $rootScope;
    set$compile($compile);
    element = null;
    compile = function(html, parent) {
      if (parent) {
        parent.html(html);
        element = parent.children();
      } else {
        element = jqLite(html);
      }
      $compile(element)(scope);
      scope.$apply();
      defer = scope.$service('$browser').defer;
      return scope;
    };
  }));
  function set$compile(c) { $compile = c; }
  afterEach(function() {
    dealoc(element);
    dealoc(doc);
  });
  describe('text', function() {
    var form = null,
        formElement = null,
        inputElement = null;
    function createInput(flags){
      var prefix = '';
      forEach(flags, function(value, key){
        prefix += key + '="' + value + '" ';
      });
      formElement = doc = angular.element('<form name="form"><input ' + prefix +
          'type="text" ng:model="name" name="name" ng:change="change()"></form>');
      inputElement = formElement.find('input');
      $compile(doc)(scope);
      form = formElement.inheritedData('$form');
    };
    it('should bind update scope from model', function() {
      createInput();
      expect(scope.form.name.$required).toBe(false);
      scope.name = 'misko';
      scope.$digest();
      expect(inputElement.val()).toEqual('misko');
    });
    it('should require', function() {
      createInput({required:''});
      expect(scope.form.name.$required).toBe(true);
      scope.$digest();
      expect(scope.form.name.$valid).toBe(false);
      scope.name = 'misko';
      scope.$digest();
      expect(scope.form.name.$valid).toBe(true);
    });
    it('should call $destroy on element remove', function() {
      createInput();
      var log = '';
      form.$on('$destroy', function() {
        log += 'destroy;';
      });
      inputElement.remove();
      expect(log).toEqual('destroy;');
    });
    it('should update the model and trim input', function() {
      createInput();
      var log = '';
      scope.change = function() {
        log += 'change();';
      };
      inputElement.val(' a ');
      browserTrigger(inputElement);
      scope.$service('$browser').defer.flush();
      expect(scope.name).toEqual('a');
      expect(log).toEqual('change();');
    });
    it('should change non-html5 types to text', inject(function($rootScope, $compile) {
      doc = angular.element('<form name="form"><input type="abc" ng:model="name"></form>');
      $compile(doc)($rootScope);
      expect(doc.find('input').attr('type')).toEqual('text');
    }));
    it('should not change html5 types to text', inject(function($rootScope, $compile) {
      doc = angular.element('<form name="form"><input type="number" ng:model="name"></form>');
      $compile(doc)($rootScope);
      expect(doc.find('input')[0].getAttribute('type')).toEqual('number');
    }));
  });
  describe("input", function() {
    describe("text", function() {
      it('should input-text auto init and handle keydown/change events', function() {
        compile('<input type="text" ng:model="name"/>');
        scope.name = 'Adam';
        scope.$digest();
        expect(element.val()).toEqual("Adam");
        element.val('Shyam');
        browserTrigger(element, 'keydown');
        // keydown event must be deferred
        expect(scope.name).toEqual('Adam');
        defer.flush();
        expect(scope.name).toEqual('Shyam');
        element.val('Kai');
        browserTrigger(element, 'change');
        scope.$service('$browser').defer.flush();
        expect(scope.name).toEqual('Kai');
      });
      it('should not trigger eval if value does not change', function() {
        compile('<input type="text" ng:model="name" ng:change="count = count + 1" ng:init="count=0"/>');
        scope.name = 'Misko';
        scope.$digest();
        expect(scope.name).toEqual("Misko");
        expect(scope.count).toEqual(0);
        browserTrigger(element, 'keydown');
        scope.$service('$browser').defer.flush();
        expect(scope.name).toEqual("Misko");
        expect(scope.count).toEqual(0);
      });
      it('should allow complex reference binding', function() {
        compile('<div>'+
                  '<input type="text" ng:model="obj[\'abc\'].name"/>'+
                '</div>');
        scope.obj = { abc: { name: 'Misko'} };
        scope.$digest();
        expect(scope.$element.find('input').val()).toEqual('Misko');
      });
      describe("ng:format", function() {
        it("should format text", function() {
          compile('<input type="list" ng:model="list"/>');
          scope.list = ['x', 'y', 'z'];
          scope.$digest();
          expect(element.val()).toEqual("x, y, z");
          element.val('1, 2, 3');
          browserTrigger(element);
          scope.$service('$browser').defer.flush();
          expect(scope.list).toEqual(['1', '2', '3']);
        });
        it("should render as blank if null", function() {
          compile('<input type="text" ng:model="age" ng:format="number" ng:init="age=null"/>');
          expect(scope.age).toBeNull();
          expect(scope.$element[0].value).toEqual('');
        });
        it("should show incorrect text while number does not parse", function() {
          compile('<input type="number" ng:model="age"/>');
          scope.age = 123;
          scope.$digest();
          expect(scope.$element.val()).toEqual('123');
          try {
            // to allow non-number values, we have to change type so that
            // the browser which have number validation will not interfere with
            // this test. IE8 won't allow it hence the catch.
            scope.$element[0].setAttribute('type', 'text');
          } catch (e){}
          scope.$element.val('123X');
          browserTrigger(scope.$element, 'change');
          scope.$service('$browser').defer.flush();
          expect(scope.$element.val()).toEqual('123X');
          expect(scope.age).toEqual(123);
          expect(scope.$element).toBeInvalid();
        });
        it("should not clobber text if model changes due to itself", function() {
          // When the user types 'a,b' the 'a,' stage parses to ['a'] but if the
          // $parseModel function runs it will change to 'a', in essence preventing
          // the user from ever typying ','.
          compile('<input type="list" ng:model="list"/>');
          scope.$element.val('a ');
          browserTrigger(scope.$element, 'change');
          scope.$service('$browser').defer.flush();
          expect(scope.$element.val()).toEqual('a ');
          expect(scope.list).toEqual(['a']);
          scope.$element.val('a ,');
          browserTrigger(scope.$element, 'change');
          scope.$service('$browser').defer.flush();
          expect(scope.$element.val()).toEqual('a ,');
          expect(scope.list).toEqual(['a']);
          scope.$element.val('a , ');
          browserTrigger(scope.$element, 'change');
          scope.$service('$browser').defer.flush();
          expect(scope.$element.val()).toEqual('a , ');
          expect(scope.list).toEqual(['a']);
          scope.$element.val('a , b');
          browserTrigger(scope.$element, 'change');
          scope.$service('$browser').defer.flush();
          expect(scope.$element.val()).toEqual('a , b');
          expect(scope.list).toEqual(['a', 'b']);
        });
        it("should come up blank when no value specified", function() {
          compile('<input type="number" ng:model="age"/>');
          scope.$digest();
          expect(scope.$element.val()).toEqual('');
          expect(scope.age).toEqual(null);
        });
      });
      describe("checkbox", function() {
        it("should format booleans", function() {
          compile('<input type="checkbox" ng:model="name" ng:init="name=false"/>');
          expect(scope.name).toBe(false);
          expect(scope.$element[0].checked).toBe(false);
        });
        it('should support type="checkbox" with non-standard capitalization', function() {
          compile('<input type="checkBox" ng:model="checkbox"/>');
          browserTrigger(element);
          expect(scope.checkbox).toBe(true);
          browserTrigger(element);
          expect(scope.checkbox).toBe(false);
        });
        it('should allow custom enumeration', function() {
          compile('<input type="checkbox" ng:model="name" ng:true-value="y" ng:false-value="n">');
          scope.name='y';
          scope.$digest();
          expect(scope.$element[0].checked).toBe(true);
          scope.name='n';
          scope.$digest();
          expect(scope.$element[0].checked).toBe(false);
          scope.name='abc';
          scope.$digest();
          expect(scope.$element[0].checked).toBe(false);
          browserTrigger(element);
          expect(scope.name).toEqual('y');
          browserTrigger(element);
          expect(scope.name).toEqual('n');
        });
        it('should fire ng:change when the value changes', function() {
          compile('<input type="checkbox" ng:model="foo" ng:change="changeFn()">');
          scope.changeFn = jasmine.createSpy('changeFn');
          scope.$digest();
          expect(scope.changeFn).not.toHaveBeenCalledOnce();
          browserTrigger(element);
          expect(scope.changeFn).toHaveBeenCalledOnce();
        });
      });
    });
    it("should process required", function() {
      compile('<input type="text" ng:model="price" name="p" required/>', jqLite(document.body));
      expect(scope.$service('$formFactory').rootForm.p.$required).toBe(true);
      expect(element.hasClass('ng-invalid')).toBeTruthy();
      scope.price = 'xxx';
      scope.$digest();
      expect(element.hasClass('ng-invalid')).toBeFalsy();
      element.val('');
      browserTrigger(element);
      scope.$service('$browser').defer.flush();
      expect(element.hasClass('ng-invalid')).toBeTruthy();
    });
    it('should allow bindings on ng:required', function() {
      compile('<input type="text" ng:model="price" ng:required="{{required}}"/>',
              jqLite(document.body));
      scope.price = '';
      scope.required = false;
      scope.$digest();
      expect(element).toBeValid();
      scope.price = 'xxx';
      scope.$digest();
      expect(element).toBeValid();
      scope.price = '';
      scope.required =  true;
      scope.$digest();
      expect(element).toBeInvalid();
      element.val('abc');
      browserTrigger(element);
      scope.$service('$browser').defer.flush();
      expect(element).toBeValid();
    });
    describe('textarea', function() {
      it("should process textarea", function() {
        compile('<textarea ng:model="name"></textarea>');
        scope.name = 'Adam';
        scope.$digest();
        expect(element.val()).toEqual("Adam");
        element.val('Shyam');
        browserTrigger(element);
        defer.flush();
        expect(scope.name).toEqual('Shyam');
        element.val('Kai');
        browserTrigger(element);
        defer.flush();
        expect(scope.name).toEqual('Kai');
      });
    });
    describe('radio', function() {
      it('should support type="radio"', function() {
        compile('<div>' +
            '<input type="radio" name="r" ng:model="chose" value="A"/>' +
            '<input type="radio" name="r" ng:model="chose" value="B"/>' +
            '<input type="radio" name="r" ng:model="chose" value="C"/>' +
        '</div>');
        var a = element[0].childNodes[0];
        var b = element[0].childNodes[1];
        expect(b.name.split('@')[1]).toEqual('r');
        scope.chose = 'A';
        scope.$digest();
        expect(a.checked).toBe(true);
        scope.chose = 'B';
        scope.$digest();
        expect(a.checked).toBe(false);
        expect(b.checked).toBe(true);
        expect(scope.clicked).not.toBeDefined();
        browserTrigger(a);
        expect(scope.chose).toEqual('A');
      });
      it('should honor model over html checked keyword after', function() {
        compile('<div ng:init="choose=\'C\'">' +
            '<input type="radio" ng:model="choose" value="A""/>' +
            '<input type="radio" ng:model="choose" value="B" checked/>' +
            '<input type="radio" ng:model="choose" value="C"/>' +
        '</div>');
        expect(scope.choose).toEqual('C');
        var inputs = scope.$element.find('input');
        expect(inputs[1].checked).toBe(false);
        expect(inputs[2].checked).toBe(true);
      });
      it('should honor model over html checked keyword before', function() {
        compile('<div ng:init="choose=\'A\'">' +
            '<input type="radio" ng:model="choose" value="A""/>' +
            '<input type="radio" ng:model="choose" value="B" checked/>' +
            '<input type="radio" ng:model="choose" value="C"/>' +
        '</div>');
        expect(scope.choose).toEqual('A');
        var inputs = scope.$element.find('input');
        expect(inputs[0].checked).toBe(true);
        expect(inputs[1].checked).toBe(false);
      });
      it('it should work with value attribute that is data-bound', function(){
        compile(
            '<li>'+
              '<input ng:repeat="item in [\'a\', \'b\']" ' +
              '       type="radio" ng:model="choice" value="{{item}}" name="choice">'+
            '</li>');
        var inputs = scope.$element.find('input');
        expect(inputs[0].checked).toBe(false);
        expect(inputs[1].checked).toBe(false);
        scope.choice = 'b';
        scope.$digest();
        expect(inputs[0].checked).toBe(false);
        expect(inputs[1].checked).toBe(true);
      });
    });
    describe('password', function () {
      it('should not change password type to text', function () {
        compile('<input type="password" ng:model="name" >');
        expect(element.attr('type')).toBe('password');
      });
    });
    it('should ignore text widget which have no name', function() {
      compile('<input type="text"/>');
      expect(scope.$element.attr('ng-exception')).toBeFalsy();
      expect(scope.$element.hasClass('ng-exception')).toBeFalsy();
    });
    it('should ignore checkbox widget which have no name', function() {
      compile('<input type="checkbox"/>');
      expect(scope.$element.attr('ng-exception')).toBeFalsy();
      expect(scope.$element.hasClass('ng-exception')).toBeFalsy();
    });
    it('should report error on assignment error', inject(function($log) {
      expect(function() {
        compile('<input type="text" ng:model="throw \'\'">');
      }).toThrow("Syntax Error: Token '''' is an unexpected token at column 7 of the expression [throw ''] starting at [''].");
      $log.error.logs.shift();
    }));
  });
  describe('scope declaration', function() {
    it('should read the declaration from scope', inject(function($rootScope, $compile) {
      var input, $formFactory;
      var element = angular.element('<input type="@MyType" ng:model="abc">');
      $rootScope.MyType = function($f, i) {
        input = i;
        $formFactory = $f;
      };
      $rootScope.MyType.$inject = ['$formFactory', '$element'];
      $compile(element)($rootScope);
      expect($formFactory).toBe($rootScope.$service('$formFactory'));
      expect(input[0]).toBe(element[0]);
    }));
    it('should throw an error of Controller not declared in scope', inject(function($rootScope, $compile) {
      var input, $formFactory;
      var element = angular.element('<input type="@DontExist" ng:model="abc">');
      var error;
      try {
        $compile(element)($rootScope);
        error = 'no error thrown';
      } catch (e) {
        error = e;
      }
      expect(error.message).toEqual("Argument 'DontExist' is not a function, got undefined");
    }));
  });
  describe('text subtypes', function() {
    function itShouldVerify(type, validList, invalidList, params, fn) {
      describe(type, function() {
        forEach(validList, function(value){
          it('should validate "' + value + '"', function() {
            setup(value);
            expect(scope.$element).toBeValid();
          });
        });
        forEach(invalidList, function(value){
          it('should NOT validate "' + value + '"', function() {
            setup(value);
            expect(scope.$element).toBeInvalid();
          });
        });
        function setup(value){
          var html = ['<input type="', type.split(' ')[0], '" '];
          forEach(params||{}, function(value, key){
            html.push(key + '="' + value + '" ');
          });
          html.push('ng:model="value">');
          compile(html.join(''));
          (fn||noop)(scope);
          scope.value = null;
          try {
            // to allow non-number values, we have to change type so that
            // the browser which have number validation will not interfere with
            // this test. IE8 won't allow it hence the catch.
            scope.$element[0].setAttribute('type', 'text');
          } catch (e){}
          if (value != undefined) {
            scope.$element.val(value);
            browserTrigger(element, 'keydown');
            scope.$service('$browser').defer.flush();
          }
          scope.$digest();
        }
      });
    }
    itShouldVerify('email', ['a@b.com'], ['a@B.c']);
    itShouldVerify('url', ['http://server:123/path'], ['a@b.c']);
    itShouldVerify('number',
        ['', '1', '12.34', '-4', '+13', '.1'],
        ['x', '12b', '-6', '101'],
        {min:-5, max:100});
    itShouldVerify('integer',
        [null, '', '1', '12', '-4', '+13'],
        ['x', '12b', '-6', '101', '1.', '1.2'],
        {min:-5, max:100});
    itShouldVerify('integer',
        [null, '', '0', '1'],
        ['-1', '2'],
        {min:0, max:1});
    itShouldVerify('text with inlined pattern constraint',
        ['', '000-00-0000', '123-45-6789'],
        ['x000-00-0000x', 'x000-00-0000', '000-00-0000x', 'x'],
        {'ng:pattern':'/^\\d\\d\\d-\\d\\d-\\d\\d\\d\\d$/'});
    itShouldVerify('text with pattern constraint on scope',
        ['', '000-00-0000', '123-45-6789'],
        ['x000-00-0000x', 'x'],
        {'ng:pattern':'regexp'}, function(scope){
          scope.regexp = /^\d\d\d-\d\d-\d\d\d\d$/;
        });
    itShouldVerify('text with ng:minlength limit',
        ['', 'aaa', 'aaaaa', 'aaaaaaaaa'],
        ['a', 'aa'],
        {'ng:minlength': 3});
    itShouldVerify('text with ng:maxlength limit',
        ['', 'a', 'aa', 'aaa'],
        ['aaaa', 'aaaaa', 'aaaaaaaaa'],
        {'ng:maxlength': 3});
    it('should throw an error when scope pattern can\'t be found', inject(function($rootScope, $compile) {
      var el = jqLite('<input ng:model="foo" ng:pattern="fooRegexp">');
      $compile(el)($rootScope);
      el.val('xx');
      browserTrigger(el, 'keydown');
      expect(function() { $rootScope.$service('$browser').defer.flush(); }).
        toThrow('Expected fooRegexp to be a RegExp but was undefined');
      dealoc(el);
    }));
  });
});
 |