Skip to content

Commit 9a993e5

Browse files
committed
Merge pull request #13520 from jasonmit/location-none-fail-tests
[Bugfix] fixes bugs around baseURL and rootURL in HistoryLocation and introduces rootURL to NoneLocation
2 parents 65dc1e8 + 6312609 commit 9a993e5

File tree

5 files changed

+146
-10
lines changed

5 files changed

+146
-10
lines changed

packages/ember-application/lib/system/application-instance.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,13 @@ const ApplicationInstance = EngineInstance.extend({
278278
}
279279
};
280280

281+
let location = get(router, 'location');
282+
281283
// Keeps the location adapter's internal URL in-sync
282-
get(router, 'location').setURL(url);
284+
location.setURL(url);
283285

284-
return router.handleURL(url).then(handleResolve, handleReject);
286+
// getURL returns the set url with the rootURL stripped off
287+
return router.handleURL(location.getURL()).then(handleResolve, handleReject);
285288
}
286289
});
287290

packages/ember-routing/lib/location/history_location.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ export default EmberObject.extend({
7474
rootURL = rootURL.replace(/\/$/, '');
7575
baseURL = baseURL.replace(/\/$/, '');
7676

77-
// remove baseURL and rootURL from path
78-
var url = path.replace(baseURL, '').replace(rootURL, '');
77+
// remove baseURL and rootURL from start of path
78+
var url = path
79+
.replace(new RegExp('^' + baseURL), '')
80+
.replace(new RegExp('^' + rootURL), '');
7981

8082
var search = location.search || '';
8183
url += search;

packages/ember-routing/lib/location/none_location.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { assert } from 'ember-metal/debug';
12
import { get } from 'ember-metal/property_get';
23
import { set } from 'ember-metal/property_set';
34
import EmberObject from 'ember-runtime/system/object';
@@ -22,15 +23,38 @@ export default EmberObject.extend({
2223
implementation: 'none',
2324
path: '',
2425

26+
detect() {
27+
let rootURL = this.rootURL;
28+
29+
assert('rootURL must end with a trailing forward slash e.g. "/app/"',
30+
rootURL.charAt(rootURL.length - 1) === '/');
31+
},
32+
33+
/**
34+
Will be pre-pended to path.
35+
36+
@private
37+
@property rootURL
38+
@default '/'
39+
*/
40+
rootURL: '/',
41+
2542
/**
26-
Returns the current path.
43+
Returns the current path without `rootURL`.
2744
2845
@private
2946
@method getURL
3047
@return {String} path
3148
*/
3249
getURL() {
33-
return get(this, 'path');
50+
let path = get(this, 'path');
51+
let rootURL = get(this, 'rootURL');
52+
53+
// remove trailing slashes if they exists
54+
rootURL = rootURL.replace(/\/$/, '');
55+
56+
// remove rootURL from url
57+
return path.replace(new RegExp('^' + rootURL), '');
3458
},
3559

3660
/**
@@ -83,9 +107,13 @@ export default EmberObject.extend({
83107
@return {String} url
84108
*/
85109
formatURL(url) {
86-
// The return value is not overly meaningful, but we do not want to throw
87-
// errors when test code renders templates containing {{action href=true}}
88-
// helpers.
89-
return url;
110+
let rootURL = get(this, 'rootURL');
111+
112+
if (url !== '') {
113+
// remove trailing slashes if they exists
114+
rootURL = rootURL.replace(/\/$/, '');
115+
}
116+
117+
return rootURL + url;
90118
}
91119
});

packages/ember-routing/tests/location/history_location_test.js

+34
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,40 @@ QUnit.test('HistoryLocation.getURL() returns the current url, excluding both roo
172172
equal(location.getURL(), '/foo/bar');
173173
});
174174

175+
QUnit.test('HistoryLocation.getURL() returns the current url, does not remove rootURL if its not at start of url', function() {
176+
expect(1);
177+
178+
HistoryTestLocation.reopen({
179+
init() {
180+
this._super(...arguments);
181+
182+
set(this, 'location', mockBrowserLocation('/foo/bar/baz'));
183+
set(this, 'rootURL', '/bar/');
184+
}
185+
});
186+
187+
createLocation();
188+
189+
equal(location.getURL(), '/foo/bar/baz');
190+
});
191+
192+
QUnit.test('HistoryLocation.getURL() returns the current url, does not remove baseURL if its not at start of url', function() {
193+
expect(1);
194+
195+
HistoryTestLocation.reopen({
196+
init() {
197+
this._super(...arguments);
198+
199+
set(this, 'location', mockBrowserLocation('/foo/bar/baz'));
200+
set(this, 'baseURL', '/bar/');
201+
}
202+
});
203+
204+
createLocation();
205+
206+
equal(location.getURL(), '/foo/bar/baz');
207+
});
208+
175209
QUnit.test('HistoryLocation.getURL() includes location.search', function() {
176210
expect(1);
177211

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { set } from 'ember-metal/property_set';
2+
import run from 'ember-metal/run_loop';
3+
import NoneLocation from 'ember-routing/location/none_location';
4+
5+
var NoneTestLocation, location;
6+
7+
function createLocation(options) {
8+
if (!options) { options = {}; }
9+
location = NoneTestLocation.create(options);
10+
}
11+
12+
QUnit.module('Ember.NoneLocation', {
13+
setup() {
14+
NoneTestLocation = NoneLocation.extend({});
15+
},
16+
17+
teardown() {
18+
run(function() {
19+
if (location) { location.destroy(); }
20+
});
21+
}
22+
});
23+
24+
QUnit.test('NoneLocation.formatURL() returns the current url always appending rootURL', function() {
25+
expect(1);
26+
27+
NoneTestLocation.reopen({
28+
init() {
29+
this._super(...arguments);
30+
set(this, 'rootURL', '/en/');
31+
}
32+
});
33+
34+
createLocation();
35+
36+
equal(location.formatURL('/foo/bar'), '/en/foo/bar');
37+
});
38+
39+
QUnit.test('NoneLocation.getURL() returns the current path minus rootURL', function() {
40+
expect(1);
41+
42+
NoneTestLocation.reopen({
43+
init() {
44+
this._super(...arguments);
45+
set(this, 'rootURL', '/foo/');
46+
set(this, 'path', '/foo/bar');
47+
}
48+
});
49+
50+
createLocation();
51+
52+
equal(location.getURL(), '/bar');
53+
});
54+
55+
QUnit.test('NonoLocation.getURL() will remove the rootURL only from the beginning of a url', function() {
56+
expect(1);
57+
58+
NoneTestLocation.reopen({
59+
init() {
60+
this._super(...arguments);
61+
set(this, 'rootURL', '/bar/');
62+
set(this, 'path', '/foo/bar/baz');
63+
}
64+
});
65+
66+
createLocation();
67+
68+
equal(location.getURL(), '/foo/bar/baz');
69+
});

0 commit comments

Comments
 (0)