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

Commit 21263c9

Browse files
committed
test($browser): update MockWindow to normalize URLs similar to real window.location
1 parent 2de162e commit 21263c9

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

test/ng/browserSpecs.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function MockWindow(options) {
1111
}
1212
var events = {};
1313
var timeouts = this.timeouts = [];
14-
var locationHref = 'http://server/';
15-
var committedHref = 'http://server/';
14+
var locationHref = document.createElement('a');
15+
var committedHref = document.createElement('a');
16+
locationHref.href = committedHref.href = 'http://server/';
1617
var mockWindow = this;
1718
var msie = options.msie;
1819
var ieState;
@@ -60,28 +61,28 @@ function MockWindow(options) {
6061

6162
this.location = {
6263
get href() {
63-
return committedHref;
64+
return committedHref.href;
6465
},
6566
set href(value) {
66-
locationHref = value;
67+
locationHref.href = value;
6768
mockWindow.history.state = null;
6869
historyEntriesLength++;
6970
if (!options.updateAsync) this.flushHref();
7071
},
7172
get hash() {
72-
return getHash(committedHref);
73+
return getHash(committedHref.href);
7374
},
7475
set hash(value) {
75-
locationHref = replaceHash(locationHref, value);
76+
locationHref.href = replaceHash(locationHref.href, value);
7677
if (!options.updateAsync) this.flushHref();
7778
},
7879
replace: function(url) {
79-
locationHref = url;
80+
locationHref.href = url;
8081
mockWindow.history.state = null;
8182
if (!options.updateAsync) this.flushHref();
8283
},
8384
flushHref: function() {
84-
committedHref = locationHref;
85+
committedHref.href = locationHref.href;
8586
}
8687
};
8788

@@ -91,13 +92,13 @@ function MockWindow(options) {
9192
historyEntriesLength++;
9293
},
9394
replaceState: function(state, title, url) {
94-
locationHref = url;
95-
if (!options.updateAsync) committedHref = locationHref;
95+
locationHref.href = url;
96+
if (!options.updateAsync) committedHref.href = locationHref.href;
9697
mockWindow.history.state = copy(state);
9798
if (!options.updateAsync) this.flushHref();
9899
},
99100
flushHref: function() {
100-
committedHref = locationHref;
101+
committedHref.href = locationHref.href;
101102
}
102103
};
103104
// IE 10-11 deserialize history.state on each read making subsequent reads
@@ -285,10 +286,10 @@ describe('browser', function() {
285286

286287
it('should return current location.href', function() {
287288
fakeWindow.location.href = 'http://test.com';
288-
expect(browser.url()).toEqual('http://test.com');
289+
expect(browser.url()).toEqual('http://test.com/');
289290

290291
fakeWindow.location.href = 'https://another.com';
291-
expect(browser.url()).toEqual('https://another.com');
292+
expect(browser.url()).toEqual('https://another.com/');
292293
});
293294

294295
it('should use history.pushState when available', function() {
@@ -319,7 +320,7 @@ describe('browser', function() {
319320
sniffer.history = false;
320321
browser.url('http://new.org');
321322

322-
expect(fakeWindow.location.href).toEqual('http://new.org');
323+
expect(fakeWindow.location.href).toEqual('http://new.org/');
323324

324325
expect(pushState).not.toHaveBeenCalled();
325326
expect(replaceState).not.toHaveBeenCalled();
@@ -386,9 +387,9 @@ describe('browser', function() {
386387
it('should not set URL when the URL is already set', function() {
387388
var current = fakeWindow.location.href;
388389
sniffer.history = false;
389-
fakeWindow.location.href = 'dontchange';
390+
fakeWindow.location.href = 'http://dontchange/';
390391
browser.url(current);
391-
expect(fakeWindow.location.href).toBe('dontchange');
392+
expect(fakeWindow.location.href).toBe('http://dontchange/');
392393
});
393394

394395
it('should not read out location.href if a reload was triggered but still allow to change the url', function() {
@@ -691,7 +692,7 @@ describe('browser', function() {
691692
it('should not fire urlChange if changed by browser.url method', function() {
692693
sniffer.history = false;
693694
browser.onUrlChange(callback);
694-
browser.url('http://new.com');
695+
browser.url('http://new.com/');
695696

696697
fakeWindow.fire('hashchange');
697698
expect(callback).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)