aboutsummaryrefslogtreecommitdiffstats
path: root/test/widget/formSpec.js
blob: a30f21eb754d9b9e161d8a4f3618d52e339f9025 (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
'use strict';

describe('form', function() {
  var doc;

  afterEach(function() {
    dealoc(doc);
  });


  it('should attach form to DOM', inject(function($rootScope) {
    doc = angular.element('<form>');
    angular.compile(doc)($rootScope);
    expect(doc.data('$form')).toBeTruthy();
  }));


  it('should prevent form submission', inject(function($rootScope) {
    var startingUrl = '' + window.location;
    doc = angular.element('<form name="myForm"><input type=submit val=submit>');
    angular.compile(doc)($rootScope);
    browserTrigger(doc.find('input'));
    waitsFor(
        function() { return true; },
        'let browser breath, so that the form submision can manifest itself', 10);
    runs(function() {
      expect('' + window.location).toEqual(startingUrl);
    });
  }));


  it('should publish form to scope', inject(function($rootScope) {
    doc = angular.element('<form name="myForm"></form>');
    angular.compile(doc)($rootScope);
    expect($rootScope.myForm).toBeTruthy();
    expect(doc.data('$form')).toBeTruthy();
    expect(doc.data('$form')).toEqual($rootScope.myForm);
  }));


  it('should have ng-valide/ng-invalid style', inject(function($rootScope) {
    doc = angular.element('<form name="myForm"><input type=text ng:model=text required>');
    angular.compile(doc)($rootScope);
    $rootScope.text = 'misko';
    $rootScope.$digest();

    expect(doc.hasClass('ng-valid')).toBe(true);
    expect(doc.hasClass('ng-invalid')).toBe(false);

    $rootScope.text = '';
    $rootScope.$digest();
    expect(doc.hasClass('ng-valid')).toBe(false);
    expect(doc.hasClass('ng-invalid')).toBe(true);
  }));


  it('should chain nested forms', inject(function($rootScope) {
    doc = angular.element(
        '<ng:form name=parent>' +
          '<ng:form name=child>' +
            '<input type=text ng:model=text name=text>' +
          '</ng:form>' +
        '</ng:form>');
    angular.compile(doc)($rootScope);
    var parent = $rootScope.parent;
    var child = $rootScope.child;
    var input = child.text;

    input.$emit('$invalid', 'MyError');
    expect(parent.$error.MyError).toEqual([input]);
    expect(child.$error.MyError).toEqual([input]);

    input.$emit('$valid', 'MyError');
    expect(parent.$error.MyError).toBeUndefined();
    expect(child.$error.MyError).toBeUndefined();
  }));


  it('should chain nested forms in repeater', inject(function($rootScope) {
    doc = angular.element(
       '<ng:form name=parent>' +
        '<ng:form ng:repeat="f in forms" name=child>' +
          '<input type=text ng:model=text name=text>' +
         '</ng:form>' +
       '</ng:form>');
    angular.compile(doc)($rootScope);
    $rootScope.forms = [1];
    $rootScope.$digest();

    var parent = $rootScope.parent;
    var child = doc.find('input').scope().child;
    var input = child.text;
    expect(parent).toBeDefined();
    expect(child).toBeDefined();
    expect(input).toBeDefined();

    input.$emit('$invalid', 'myRule');
    expect(input.$error.myRule).toEqual(true);
    expect(child.$error.myRule).toEqual([input]);
    expect(parent.$error.myRule).toEqual([input]);

    input.$emit('$valid', 'myRule');
    expect(parent.$error.myRule).toBeUndefined();
    expect(child.$error.myRule).toBeUndefined();
  }));
});