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

Commit ce48f4d

Browse files
committed
test(browser): change mock location definition to use defineProperty
The original fix for which this mock location logic was written fixes a bug in master which also exists in 1.2.x. Cherry-picking the fix to the 1.2.x branch was difficult because the mock location object used ES5 get/set syntax, which is not supported in IE8. This fix changes the implementation to work with IE8 and modern browsers. IE8's defineProperty only works on certain types of objects, such as DOM elements. So the mock location is a div element in this implementation.
1 parent 9845cee commit ce48f4d

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

test/ng/browserSpecs.js

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
/* global getHash:true, stripHash:true */
4+
/* jshint evil:true */
45

56
var historyEntriesLength;
67
var sniffer = {};
@@ -45,26 +46,37 @@ function MockWindow(options) {
4546
});
4647
};
4748

48-
this.location = {
49-
get href() {
50-
return locationHref;
51-
},
52-
set href(value) {
49+
//IE8 hack. defineProperty doesn't work with POJS, just with certain DOM elements
50+
this.location = document.createElement('div');
51+
this.location.href = {};
52+
this.location.hash = {};
53+
this.location.replace = function(url) {
54+
locationHref = url;
55+
mockWindow.history.state = null;
56+
};
57+
Object.defineProperty(this.location, 'href', {
58+
enumerable: false,
59+
configurable: true,
60+
set: function(value) {
5361
locationHref = value;
5462
mockWindow.history.state = null;
5563
historyEntriesLength++;
5664
},
57-
get hash() {
58-
return getHash(locationHref);
59-
},
60-
set hash(value) {
65+
get: function() {
66+
return locationHref;
67+
}
68+
});
69+
70+
Object.defineProperty(this.location, 'hash', {
71+
enumerable: false,
72+
configurable: true,
73+
set: function(value) {
6174
locationHref = stripHash(locationHref) + '#' + value;
6275
},
63-
replace: function(url) {
64-
locationHref = url;
65-
mockWindow.history.state = null;
76+
get: function() {
77+
return getHash(locationHref);
6678
}
67-
};
79+
});
6880

6981
this.history = {
7082
replaceState: noop,
@@ -115,7 +127,6 @@ describe('browser', function() {
115127
warn: function() { logs.warn.push(slice.call(arguments)); },
116128
info: function() { logs.info.push(slice.call(arguments)); },
117129
error: function() { logs.error.push(slice.call(arguments)); }};
118-
119130
browser = new Browser(fakeWindow, fakeDocument, fakeLog, sniffer);
120131
});
121132

0 commit comments

Comments
 (0)