fix(router): respect LocationStrategy when constructing hrefs in links

Note that this introduces more behavior for LocationStrategy which needs
yet more refactoring to test. See #4935.

Closes #4333
This commit is contained in:
Brian Ford
2015-10-26 13:57:41 +00:00
parent 280cd33f2e
commit 2a3e11d32d
11 changed files with 36 additions and 42 deletions

View File

@ -163,7 +163,8 @@ export function main() {
});
}));
describe('custom app base ref', () => {
// TODO(btford): mock out level lower than LocationStrategy once that level exists
xdescribe('custom app base ref', () => {
beforeEachBindings(() => { return [provide(APP_BASE_HREF, {useValue: '/my/app'})]; });
it('should bootstrap',
inject([AsyncTestCompleter, TestComponentBuilder],

View File

@ -33,17 +33,12 @@ export function main() {
beforeEach(makeLocation);
it('should normalize relative urls on navigate', () => {
location.go('user/btford');
expect(locationStrategy.path()).toEqual('/my/app/user/btford');
});
it('should not prepend urls with starting slash when an empty URL is provided',
() => { expect(location.normalizeAbsolutely('')).toEqual(locationStrategy.getBaseHref()); });
() => { expect(location.prepareExternalUrl('')).toEqual(locationStrategy.getBaseHref()); });
it('should not prepend path with an extra slash when a baseHref has a trailing slash', () => {
let location = makeLocation('/my/slashed/app/');
expect(location.normalizeAbsolutely('/page')).toEqual('/my/slashed/app/page');
expect(location.prepareExternalUrl('/page')).toEqual('/my/slashed/app/page');
});
it('should not append urls with leading slash on navigate', () => {
@ -51,12 +46,6 @@ export function main() {
expect(locationStrategy.path()).toEqual('/my/app/user/btford');
});
it('should remove index.html from base href', () => {
let location = makeLocation('/my/app/index.html');
location.go('user/btford');
expect(locationStrategy.path()).toEqual('/my/app/user/btford');
});
it('should normalize urls on popstate', inject([AsyncTestCompleter], (async) => {
locationStrategy.simulatePopState('/my/app/user/btford');
location.subscribe((ev) => {
@ -65,17 +54,6 @@ export function main() {
})
}));
it('should normalize location path', () => {
locationStrategy.internalPath = '/my/app/user/btford';
expect(location.path()).toEqual('/user/btford');
});
it('should use optional base href param', () => {
let location = makeLocation('/', provide(APP_BASE_HREF, {useValue: '/my/custom/href'}));
location.go('user/btford');
expect(locationStrategy.path()).toEqual('/my/custom/href/user/btford');
});
it('should throw when no base href is provided', () => {
var locationStrategy = new MockLocationStrategy();
locationStrategy.internalBaseHref = null;

View File

@ -53,7 +53,7 @@ export function main() {
.then((testComponent) => {
testComponent.detectChanges();
let anchorElement = testComponent.debugElement.query(By.css('a')).nativeElement;
expect(DOM.getAttribute(anchorElement, 'href')).toEqual('/detail');
expect(DOM.getAttribute(anchorElement, 'href')).toEqual('detail');
async.done();
});
}));
@ -99,7 +99,7 @@ class TestComponent {
function makeDummyLocation() {
var dl = new SpyLocation();
dl.spy('normalizeAbsolutely').andCallFake((url) => url);
dl.spy('prepareExternalUrl').andCallFake((url) => url);
return dl;
}