forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBrowserSpecs.js
516 lines (398 loc) · 15.7 KB
/
BrowserSpecs.js
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
describe('browser', function(){
var browser, fakeWindow, xhr, logs, scripts, setTimeoutQueue;
function fakeSetTimeout(fn) {
setTimeoutQueue.push(fn);
return Math.random();
}
fakeSetTimeout.flush = function() {
forEach(setTimeoutQueue, function(fn) {
fn();
});
};
beforeEach(function(){
setTimeoutQueue = [];
scripts = [];
xhr = null;
fakeWindow = {
location: {href:"http://server"},
setTimeout: fakeSetTimeout
};
var fakeBody = {append: function(node){scripts.push(node);}};
var FakeXhr = function(){
xhr = this;
this.open = function(method, url, async){
xhr.method = method;
xhr.url = url;
xhr.async = async;
xhr.headers = {};
};
this.setRequestHeader = function(key, value){
xhr.headers[key] = value;
};
this.send = function(post){
xhr.post = post;
};
};
logs = {log:[], warn:[], info:[], error:[]};
var fakeLog = {log: function() { logs.log.push(slice.call(arguments)); },
warn: function() { logs.warn.push(slice.call(arguments)); },
info: function() { logs.info.push(slice.call(arguments)); },
error: function() { logs.error.push(slice.call(arguments)); }};
browser = new Browser(fakeWindow, jqLite(window.document), fakeBody, FakeXhr,
fakeLog);
});
it('should contain cookie cruncher', function() {
expect(browser.cookies).toBeDefined();
});
describe('outstading requests', function(){
it('should process callbacks immedietly with no outstanding requests', function(){
var callback = jasmine.createSpy('callback');
browser.notifyWhenNoOutstandingRequests(callback);
expect(callback).wasCalled();
});
it('should queue callbacks with outstanding requests', function(){
var callback = jasmine.createSpy('callback');
browser.xhr('GET', '/url', null, noop);
browser.notifyWhenNoOutstandingRequests(callback);
expect(callback).not.wasCalled();
xhr.readyState = 4;
xhr.onreadystatechange();
expect(callback).wasCalled();
});
});
describe('xhr', function(){
describe('JSON', function(){
it('should add script tag for request', function() {
var callback = jasmine.createSpy('callback');
var log = "";
browser.xhr('JSON', 'http://example.org/path?cb=JSON_CALLBACK', null, function(code, data){
log += code + ':' + data + ';';
});
browser.notifyWhenNoOutstandingRequests(callback);
expect(callback).not.wasCalled();
expect(scripts.length).toEqual(1);
var script = scripts[0];
script.remove = function(){
log += 'remove();';
};
var url = script.attr('src').split('?cb=');
expect(url[0]).toEqual('http://example.org/path');
expect(typeof fakeWindow[url[1]]).toEqual($function);
fakeWindow[url[1]]('data');
expect(callback).wasCalled();
expect(log).toEqual('remove();200:data;');
expect(typeof fakeWindow[url[1]]).toEqual('undefined');
});
});
it('should set headers for all requests', function(){
var code, response, headers = {};
browser.xhr('GET', 'URL', 'POST', function(c,r){
code = c;
response = r;
}, {'X-header': 'value'});
expect(xhr.method).toEqual('GET');
expect(xhr.url).toEqual('URL');
expect(xhr.post).toEqual('POST');
expect(xhr.headers).toEqual({
"Accept": "application/json, text/plain, */*",
"X-Requested-With": "XMLHttpRequest",
"X-header":"value"
});
xhr.status = 202;
xhr.responseText = 'RESPONSE';
xhr.readyState = 4;
xhr.onreadystatechange();
expect(code).toEqual(202);
expect(response).toEqual('RESPONSE');
});
it('should not set Content-type header for GET requests', function() {
browser.xhr('GET', 'URL', 'POST-DATA', function(c, r) {});
expect(xhr.headers['Content-Type']).not.toBeDefined();
});
it('should set Content-type header for POST requests', function() {
browser.xhr('POST', 'URL', 'POST-DATA', function(c, r) {});
expect(xhr.headers['Content-Type']).toBeDefined();
expect(xhr.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
});
it('should set default headers for custom methods', function() {
browser.xhr('CUSTOM', 'URL', 'POST-DATA', function(c, r) {});
expect(xhr.headers['Accept']).toEqual('application/json, text/plain, */*');
expect(xhr.headers['X-Requested-With']).toEqual('XMLHttpRequest');
});
});
describe('defer', function() {
it('should execute fn asynchroniously via setTimeout', function() {
var counter = 0;
browser.defer(function() {counter++;});
expect(counter).toBe(0);
fakeSetTimeout.flush();
expect(counter).toBe(1);
});
it('should update outstandingRequests counter', function() {
var callback = jasmine.createSpy('callback');
browser.defer(callback);
expect(callback).not.wasCalled();
fakeSetTimeout.flush();
expect(callback).wasCalled();
});
});
describe('cookies', function() {
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
beforeEach(function() {
deleteAllCookies();
expect(document.cookie).toEqual('');
});
afterEach(function() {
deleteAllCookies();
expect(document.cookie).toEqual('');
});
describe('remove all via (null)', function() {
it('should do nothing when no cookies are set', function() {
browser.cookies(null);
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});
});
describe('remove via cookies(cookieName, undefined)', function() {
it('should remove a cookie when it is present', function() {
document.cookie = 'foo=bar';
browser.cookies('foo', undefined);
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});
it('should do nothing when an nonexisting cookie is being removed', function() {
browser.cookies('doesntexist', undefined);
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});
});
describe('put via cookies(cookieName, string)', function() {
it('should create and store a cookie', function() {
browser.cookies('cookieName', 'cookie=Value');
expect(document.cookie).toMatch(/cookieName=cookie%3DValue;? ?/);
expect(browser.cookies()).toEqual({'cookieName':'cookie=Value'});
});
it('should overwrite an existing unsynced cookie', function() {
document.cookie = "cookie=new";
var oldVal = browser.cookies('cookie', 'newer');
expect(document.cookie).toEqual('cookie=newer');
expect(browser.cookies()).toEqual({'cookie':'newer'});
expect(oldVal).not.toBeDefined();
});
it('should escape both name and value', function() {
browser.cookies('cookie1=', 'val;ue');
browser.cookies('cookie2=bar;baz', 'val=ue');
var rawCookies = document.cookie.split("; "); //order is not guaranteed, so we need to parse
expect(rawCookies.length).toEqual(2);
expect(rawCookies).toContain('cookie1%3D=val%3Bue');
expect(rawCookies).toContain('cookie2%3Dbar%3Bbaz=val%3Due');
});
it('should log warnings when 4kb per cookie storage limit is reached', function() {
var i, longVal = '', cookieStr;
for(i=0; i<4092; i++) {
longVal += '+';
}
cookieStr = document.cookie;
browser.cookies('x', longVal); //total size 4094-4096, so it should go through
expect(document.cookie).not.toEqual(cookieStr);
expect(browser.cookies()['x']).toEqual(longVal);
expect(logs.warn).toEqual([]);
browser.cookies('x', longVal + 'xxx'); //total size 4097-4099, a warning should be logged
expect(logs.warn).toEqual(
[[ "Cookie 'x' possibly not set or overflowed because it was too large (4097 > 4096 " +
"bytes)!" ]]);
//force browser to dropped a cookie and make sure that the cache is not out of sync
browser.cookies('x', 'shortVal');
expect(browser.cookies().x).toEqual('shortVal'); //needed to prime the cache
cookieStr = document.cookie;
browser.cookies('x', longVal + longVal + longVal); //should be too long for all browsers
if (document.cookie !== cookieStr) {
fail("browser didn't drop long cookie when it was expected. make the cookie in this " +
"test longer");
}
expect(browser.cookies().x).toEqual('shortVal');
});
it('should log warnings when 20 cookies per domain storage limit is reached', function() {
var i, str, cookieStr;
for (i=0; i<20; i++) {
str = '' + i;
browser.cookies(str, str);
}
i=0;
for (str in browser.cookies()) {
i++;
}
expect(i).toEqual(20);
expect(logs.warn).toEqual([]);
cookieStr = document.cookie;
browser.cookies('one', 'more');
expect(logs.warn).toEqual([]);
//if browser dropped a cookie (very likely), make sure that the cache is not out of sync
if (document.cookie === cookieStr) {
expect(size(browser.cookies())).toEqual(20);
} else {
expect(size(browser.cookies())).toEqual(21);
}
});
});
describe('get via cookies()[cookieName]', function() {
it('should return undefined for nonexistent cookie', function() {
expect(browser.cookies().nonexistent).not.toBeDefined();
});
it ('should return a value for an existing cookie', function() {
document.cookie = "foo=bar=baz";
expect(browser.cookies().foo).toEqual('bar=baz');
});
it ('should unescape cookie values that were escaped by puts', function() {
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due";
expect(browser.cookies()['cookie2=bar;baz']).toEqual('val=ue');
});
it('should preserve leading & trailing spaces in names and values', function() {
browser.cookies(' cookie name ', ' cookie value ');
expect(browser.cookies()[' cookie name ']).toEqual(' cookie value ');
expect(browser.cookies()['cookie name']).not.toBeDefined();
});
});
describe('getAll via cookies()', function() {
it('should return cookies as hash', function() {
document.cookie = "foo1=bar1";
document.cookie = "foo2=bar2";
expect(browser.cookies()).toEqual({'foo1':'bar1', 'foo2':'bar2'});
});
it('should return empty hash if no cookies exist', function() {
expect(browser.cookies()).toEqual({});
});
});
it('should pick up external changes made to browser cookies', function() {
browser.cookies('oatmealCookie', 'drool');
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
document.cookie = 'oatmealCookie=changed';
expect(browser.cookies().oatmealCookie).toEqual('changed');
});
it('should initialize cookie cache with existing cookies', function() {
document.cookie = "existingCookie=existingValue";
expect(browser.cookies()).toEqual({'existingCookie':'existingValue'});
});
});
describe('poller', function(){
beforeEach(function() {
spyOn(browser, 'startPoller');
});
it('should call all fns on poll', function(){
var log = '';
browser.addPollFn(function(){log+='a';});
browser.addPollFn(function(){log+='b';});
expect(log).toEqual('');
browser.poll();
expect(log).toEqual('ab');
browser.poll();
expect(log).toEqual('abab');
});
it('should startPoller', function(){
var log = '';
var setTimeoutSpy = jasmine.createSpy('setTimeout');
browser.addPollFn(function(){log+='.';});
browser.startPoller.andCallThrough();
browser.startPoller(50, setTimeoutSpy);
expect(log).toEqual('.');
expect(setTimeoutSpy.mostRecentCall.args[1]).toEqual(50);
setTimeoutSpy.mostRecentCall.args[0]();
expect(log).toEqual('..');
});
it('should return fn that was passed into addPollFn', function() {
var fn = function() { return 1; };
var returnedFn = browser.addPollFn(fn);
expect(returnedFn).toBe(fn);
});
it('should auto start poller when first fn registered', function() {
browser.addPollFn(function() {});
expect(browser.startPoller).toHaveBeenCalled();
expect(browser.startPoller.callCount).toBe(1);
});
it('should auto start poller only when first fn registered', function() {
browser.startPoller.andCallThrough();
browser.addPollFn(function() {});
browser.addPollFn(function() {});
browser.addPollFn(function() {});
expect(browser.startPoller.callCount).toBe(1);
});
});
describe('url api', function() {
it('should use $browser poller to detect url changes when onhashchange event is unsupported',
function() {
fakeWindow = {
location: {href:"http://server"},
document: {}
};
browser = new Browser(fakeWindow, {}, {});
browser.startPoller = function() {};
var events = [];
browser.onHashChange(function() {
events.push('x');
});
fakeWindow.location.href = "http://server/#newHash";
expect(events).toEqual([]);
browser.poll();
expect(events).toEqual(['x']);
//don't do anything if url hasn't changed
events = [];
browser.poll();
expect(events).toEqual([]);
});
it('should use onhashchange events to detect url changes when supported by browser',
function() {
var onHashChngListener;
fakeWindow = {location: {href:"http://server"},
addEventListener: function(type, listener) {
expect(type).toEqual('hashchange');
onHashChngListener = listener;
},
attachEvent: function(type, listener) {
expect(type).toEqual('onhashchange');
onHashChngListener = listener;
},
removeEventListener: angular.noop,
detachEvent: angular.noop,
document: {}
};
fakeWindow.onhashchange = true;
browser = new Browser(fakeWindow, {}, {});
var events = [],
event = {type: "hashchange"};
browser.onHashChange(function(e) {
events.push(e);
});
expect(events).toEqual([]);
onHashChngListener(event);
expect(events.length).toBe(1);
expect(events[0].originalEvent || events[0]).toBe(event); // please jQuery and jqLite
// clean up the jqLite cache so that the global afterEach doesn't complain
if (!jQuery) {
jqLite(fakeWindow).dealoc();
}
});
// asynchronous test
it('should fire onHashChange when location.hash change', function() {
var callback = jasmine.createSpy('onHashChange');
browser = new Browser(window, {}, {});
browser.onHashChange(callback);
window.location.hash = 'new-hash';
browser.startPoller(100, setTimeout);
waitsFor(function() {
return callback.callCount;
}, 'onHashChange callback to be called', 1000);
runs(function() {
if (!jQuery) jqLite(window).dealoc();
window.location.hash = '';
});
});
});
});