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

Commit 97a9119

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 97a9119

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

test/ng/browserSpecs.js

+24-14
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,37 @@ function MockWindow(options) {
4545
});
4646
};
4747

48-
this.location = {
49-
get href() {
50-
return locationHref;
51-
},
52-
set href(value) {
48+
//IE8 hack. defineProperty doesn't work with POJS, just with certain DOM elements
49+
this.location = document.createElement('div');
50+
this.location.href = {};
51+
this.location.hash = {};
52+
this.location.replace = function(url) {
53+
locationHref = url;
54+
mockWindow.history.state = null;
55+
};
56+
Object.defineProperty(this.location, 'href', {
57+
enumerable: false,
58+
configurable: true,
59+
set: function(value) {
5360
locationHref = value;
5461
mockWindow.history.state = null;
5562
historyEntriesLength++;
5663
},
57-
get hash() {
58-
return getHash(locationHref);
59-
},
60-
set hash(value) {
64+
get: function() {
65+
return locationHref;
66+
}
67+
});
68+
69+
Object.defineProperty(this.location, 'hash', {
70+
enumerable: false,
71+
configurable: true,
72+
set: function(value) {
6173
locationHref = stripHash(locationHref) + '#' + value;
6274
},
63-
replace: function(url) {
64-
locationHref = url;
65-
mockWindow.history.state = null;
75+
get: function() {
76+
return getHash(locationHref);
6677
}
67-
};
78+
});
6879

6980
this.history = {
7081
replaceState: noop,
@@ -115,7 +126,6 @@ describe('browser', function() {
115126
warn: function() { logs.warn.push(slice.call(arguments)); },
116127
info: function() { logs.info.push(slice.call(arguments)); },
117128
error: function() { logs.error.push(slice.call(arguments)); }};
118-
119129
browser = new Browser(fakeWindow, fakeDocument, fakeLog, sniffer);
120130
});
121131

0 commit comments

Comments
 (0)