aboutsummaryrefslogtreecommitdiffstats
path: root/test/AngularSpec.js
blob: b60b7bd82ed398aa545e807d46f9a373972b2bfa (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
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
beforeEach(function(){
  compileCache = {};
});

describe('Angular', function(){
  xit('should fire on updateEvents', function(){
    var onUpdateView = jasmine.createSpy();
    var scope = angular.compile("<div></div>", { onUpdateView: onUpdateView });
    expect(onUpdateView).wasNotCalled();
    scope.$init();
    scope.$eval();
    expect(onUpdateView).wasCalled();
  });
});

describe('case', function(){
  it('should change case', function(){
    expect(lowercase('ABC90')).toEqual('abc90');
    expect(manualLowercase('ABC90')).toEqual('abc90');
    expect(uppercase('abc90')).toEqual('ABC90');
    expect(manualUppercase('abc90')).toEqual('ABC90');
  });
});

describe("copy", function(){
  it("should return same object", function (){
    var obj = {};
    var arr = [];
    assertSame(obj, copy({}, obj));
    assertSame(arr, copy([], arr));
  });

  it("should copy Date", function(){
    var date = new Date(123);
    expect(copy(date) instanceof Date).toBeTruthy();
    expect(copy(date).getTime()).toEqual(123);
    expect(copy(date) === date).toBeFalsy();
  });

  it("should copy array", function(){
    var src = [1, {name:"value"}];
    var dst = [{key:"v"}];
    assertSame(dst, copy(src, dst));
    assertEquals([1, {name:"value"}], dst);
    assertEquals({name:"value"}, dst[1]);
    assertNotSame(src[1], dst[1]);
  });

  it('should copy empty array', function() {
    var src = [];
    var dst = [{key: "v"}];
    assertEquals([], copy(src, dst));
    assertEquals([], dst);
  });

  it("should copy object", function(){
    var src = {a:{name:"value"}};
    var dst = {b:{key:"v"}};
    assertSame(dst, copy(src, dst));
    assertEquals({a:{name:"value"}}, dst);
    assertEquals(src.a, dst.a);
    assertNotSame(src.a, dst.a);
  });

  it("should copy primitives", function(){
    expect(copy(null)).toEqual(null);
    expect(copy('')).toBe('');
    expect(copy('lala')).toBe('lala');
    expect(copy(123)).toEqual(123);
    expect(copy([{key:null}])).toEqual([{key:null}]);
  });

});

describe('equals', function(){
  it('should return true if same object', function(){
    var o = {};
    expect(equals(o, o)).toEqual(true);
    expect(equals(1, '1')).toEqual(true);
    expect(equals(1, '2')).toEqual(false);
  });

  it('should recurse into object', function(){
    expect(equals({}, {})).toEqual(true);
    expect(equals({name:'misko'}, {name:'misko'})).toEqual(true);
    expect(equals({name:'misko', age:1}, {name:'misko'})).toEqual(false);
    expect(equals({name:'misko'}, {name:'misko', age:1})).toEqual(false);
    expect(equals({name:'misko'}, {name:'adam'})).toEqual(false);
    expect(equals(['misko'], ['misko'])).toEqual(true);
    expect(equals(['misko'], ['adam'])).toEqual(false);
    expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
  });

  it('should ignore $ member variables', function(){
    expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
    expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
    expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
  });

  it('should ignore functions', function(){
    expect(equals({func: function() {}}, {bar: function() {}})).toEqual(true);
  });
});

describe('parseKeyValue', function() {
  it('should parse a string into key-value pairs', function() {
    expect(parseKeyValue('')).toEqual({});
    expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'});
    expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'});
    expect(parseKeyValue('escaped%20key=escaped%20value')).
      toEqual({'escaped key': 'escaped value'});
    expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''});
    expect(parseKeyValue('flag1&key=value&flag2')).
      toEqual({flag1: true, key: 'value', flag2: true});
  });
});

describe('toKeyValue', function() {
  it('should parse key-value pairs into string', function() {
    expect(toKeyValue({})).toEqual('');
    expect(toKeyValue({simple: 'pair'})).toEqual('simple=pair');
    expect(toKeyValue({first: '1', second: '2'})).toEqual('first=1&second=2');
    expect(toKeyValue({'escaped key': 'escaped value'})).
      toEqual('escaped%20key=escaped%20value');
    expect(toKeyValue({emptyKey: ''})).toEqual('emptyKey=');
  });

  it('should parse true values into flags', function() {
    expect(toKeyValue({flag1: true, key: 'value', flag2: true})).toEqual('flag1&key=value&flag2');
  });
});


describe ('rngScript', function() {
  it('should match angular.js', function() {
    expect('angular.js'.match(rngScript)).not.toBeNull();
    expect('../angular.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular.js'.match(rngScript)).not.toBeNull();

    expect('foo.js'.match(rngScript)).toBeNull();
    expect('foo/foo.js'.match(rngScript)).toBeNull();
    expect('my-angular-app.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app.js'.match(rngScript)).toBeNull();
  });

  it('should match angular.min.js', function() {
    expect('angular.min.js'.match(rngScript)).not.toBeNull();
    expect('../angular.min.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular.min.js'.match(rngScript)).not.toBeNull();

    expect('my-angular-app.min.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app.min.js'.match(rngScript)).toBeNull();
  });

  it('should match angular-bootstrap.js', function() {
    expect('angular-bootstrap.js'.match(rngScript)).not.toBeNull();
    expect('../angular-bootstrap.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular-bootstrap.js'.match(rngScript)).not.toBeNull();

    expect('my-angular-app-bootstrap.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app-bootstrap.js'.match(rngScript)).toBeNull();
  });

  it('should match angular-0.9.0.js', function() {
    expect('angular-0.9.0.js'.match(rngScript)).not.toBeNull();
    expect('../angular-0.9.0.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular-0.9.0.js'.match(rngScript)).not.toBeNull();

    expect('my-angular-app-0.9.0.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app-0.9.0.js'.match(rngScript)).toBeNull();
  });

  it('should match angular-0.9.0.min.js', function() {
    expect('angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
    expect('../angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();

    expect('my-angular-app-0.9.0.min.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app-0.9.0.min.js'.match(rngScript)).toBeNull();
  });

  it('should match angular-0.9.0-de0a8612.js', function() {
    expect('angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
    expect('../angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();

    expect('my-angular-app-0.9.0-de0a8612.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app-0.9.0-de0a8612.js'.match(rngScript)).toBeNull();
  });

  it('should match angular-0.9.0-de0a8612.min.js', function() {
    expect('angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
    expect('../angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
    expect('foo/angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();

    expect('my-angular-app-0.9.0-de0a8612.min.js'.match(rngScript)).toBeNull();
    expect('foo/../my-angular-app-0.9.0-de0a8612.min.js'.match(rngScript)).toBeNull();
  });
});


describe('angularJsConfig', function() {
  it('should find angular.js script tag and config', function() {
    var doc = { getElementsByTagName: function(tagName) {
                  expect(tagName).toEqual('script');
                  return [{nodeName: 'SCRIPT', src: 'random.js'},
                          {nodeName: 'SCRIPT', src: 'angular.js'},
                          {nodeName: 'SCRIPT', src: 'my-angular-app.js'}];
                }
              };

    expect(angularJsConfig(doc)).toEqual({base_url: '',
                                          ie_compat: 'angular-ie-compat.js',
                                          ie_compat_id: 'ng-ie-compat'});
  });


  it('should extract angular config from the ng: attributes', function() {
    var doc = { getElementsByTagName: function(tagName) {
                  expect(lowercase(tagName)).toEqual('script');
                  return [{nodeName: 'SCRIPT',
                           src: 'angularjs/angular.js',
                           attributes: [{name: 'ng:autobind', value:undefined},
                                        {name: 'ng:css', value: 'css/my_custom_angular.css'},
                                        {name: 'ng:ie-compat', value: 'myjs/angular-ie-compat.js'},
                                        {name: 'ng:ie-compat-id', value: 'ngcompat'}] }];
               }};

    expect(angularJsConfig(doc)).toEqual({base_url: 'angularjs/',
                                          autobind: true,
                                          css: 'css/my_custom_angular.css',
                                          ie_compat: 'myjs/angular-ie-compat.js',
                                          ie_compat_id: 'ngcompat'});
  });


  it("should default to versioned ie-compat file if angular file is versioned", function() {
    var doc = { getElementsByTagName: function(tagName) {
                  expect(lowercase(tagName)).toEqual('script');
                  return [{nodeName: 'SCRIPT',
                           src: 'js/angular-0.9.0.js'}];
               }};

    expect(angularJsConfig(doc)).toEqual({base_url: 'js/',
                                          ie_compat: 'js/angular-ie-compat-0.9.0.js',
                                          ie_compat_id: 'ng-ie-compat'});
  });


  it("should default to versioned ie-compat file if angular file is versioned and minified", function() {
    var doc = { getElementsByTagName: function(tagName) {
                  expect(lowercase(tagName)).toEqual('script');
                  return [{nodeName: 'SCRIPT',
                           src: 'js/angular-0.9.0-cba23f00.min.js'}];
               }};

    expect(angularJsConfig(doc)).toEqual({base_url: 'js/',
                                          ie_compat: 'js/angular-ie-compat-0.9.0-cba23f00.js',
                                          ie_compat_id: 'ng-ie-compat'});
  });
});