Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a057e08

Browse files
shahatapetebacondarwin
authored andcommitted
fix(cookieReader): safely access $document so it can be mocked
Closes #11373 Closes #11388
1 parent 948120e commit a057e08

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/ng/cookieReader.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @return {Object} a key/value map of the current cookies
1111
*/
1212
function $$CookieReader($document) {
13-
var rawDocument = $document[0];
13+
var rawDocument = $document[0] || {};
1414
var lastCookies = {};
1515
var lastCookieString = '';
1616

@@ -24,9 +24,10 @@ function $$CookieReader($document) {
2424

2525
return function() {
2626
var cookieArray, cookie, i, index, name;
27+
var currentCookieString = rawDocument.cookie || '';
2728

28-
if (rawDocument.cookie !== lastCookieString) {
29-
lastCookieString = rawDocument.cookie;
29+
if (currentCookieString !== lastCookieString) {
30+
lastCookieString = currentCookieString;
3031
cookieArray = lastCookieString.split('; ');
3132
lastCookies = {};
3233

test/ng/documentSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,24 @@ describe('$document', function() {
66
it("should inject $document", inject(function($document) {
77
expect($document).toEqual(jqLite(document));
88
}));
9+
10+
11+
it('should be able to mock $document object', function() {
12+
module({$document: {}});
13+
inject(function($httpBackend, $http) {
14+
$httpBackend.expectGET('/dummy').respond('dummy');
15+
$http.get('/dummy');
16+
$httpBackend.flush();
17+
});
18+
});
19+
20+
21+
it('should be able to mock $document array', function() {
22+
module({$document: [{}]});
23+
inject(function($httpBackend, $http) {
24+
$httpBackend.expectGET('/dummy').respond('dummy');
25+
$http.get('/dummy');
26+
$httpBackend.flush();
27+
});
28+
});
929
});

0 commit comments

Comments
 (0)