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

Commit 33492fe

Browse files
committed
test($browser): update MockWindow to normalize URLs similar to real window.location
1 parent b4e409b commit 33492fe

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

test/ng/browserSpecs.js

+22-21
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 = window.document.createElement('a');
15+
var committedHref = window.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
@@ -398,18 +399,18 @@ describe('browser', function() {
398399

399400
it('should return current location.href', function() {
400401
fakeWindow.location.href = 'http://test.com';
401-
expect(browser.url()).toEqual('http://test.com');
402+
expect(browser.url()).toEqual('http://test.com/');
402403

403404
fakeWindow.location.href = 'https://another.com';
404-
expect(browser.url()).toEqual('https://another.com');
405+
expect(browser.url()).toEqual('https://another.com/');
405406
});
406407

407408
it('should strip an empty hash fragment', function() {
408-
fakeWindow.location.href = 'http://test.com#';
409-
expect(browser.url()).toEqual('http://test.com');
409+
fakeWindow.location.href = 'http://test.com/#';
410+
expect(browser.url()).toEqual('http://test.com/');
410411

411-
fakeWindow.location.href = 'https://another.com#foo';
412-
expect(browser.url()).toEqual('https://another.com#foo');
412+
fakeWindow.location.href = 'https://another.com/#foo';
413+
expect(browser.url()).toEqual('https://another.com/#foo');
413414
});
414415

415416
it('should use history.pushState when available', function() {
@@ -440,7 +441,7 @@ describe('browser', function() {
440441
sniffer.history = false;
441442
browser.url('http://new.org');
442443

443-
expect(fakeWindow.location.href).toEqual('http://new.org');
444+
expect(fakeWindow.location.href).toEqual('http://new.org/');
444445

445446
expect(pushState).not.toHaveBeenCalled();
446447
expect(replaceState).not.toHaveBeenCalled();
@@ -507,9 +508,9 @@ describe('browser', function() {
507508
it('should not set URL when the URL is already set', function() {
508509
var current = fakeWindow.location.href;
509510
sniffer.history = false;
510-
fakeWindow.location.href = 'dontchange';
511+
fakeWindow.location.href = 'http://dontchange/';
511512
browser.url(current);
512-
expect(fakeWindow.location.href).toBe('dontchange');
513+
expect(fakeWindow.location.href).toBe('http://dontchange/');
513514
});
514515

515516
it('should not read out location.href if a reload was triggered but still allow to change the url', function() {
@@ -812,7 +813,7 @@ describe('browser', function() {
812813
it('should not fire urlChange if changed by browser.url method', function() {
813814
sniffer.history = false;
814815
browser.onUrlChange(callback);
815-
browser.url('http://new.com');
816+
browser.url('http://new.com/');
816817

817818
fakeWindow.fire('hashchange');
818819
expect(callback).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)