refactor(router): refactor BrowserLocation into LocationStrategy
This makes it easy to mock browser location and paves the way to implementing hash routing.
This commit is contained in:
@@ -15,19 +15,19 @@ import {
|
||||
import {Injector, bind} from 'angular2/di';
|
||||
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
||||
import {Location, appBaseHrefToken} from 'angular2/src/router/location';
|
||||
import {BrowserLocation} from 'angular2/src/router/browser_location';
|
||||
import {DummyBrowserLocation} from 'angular2/src/mock/browser_location_mock';
|
||||
import {LocationStrategy} from 'angular2/src/router/location_strategy';
|
||||
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
|
||||
|
||||
export function main() {
|
||||
describe('Location', () => {
|
||||
|
||||
var browserLocation, location;
|
||||
var locationStrategy, location;
|
||||
|
||||
function makeLocation(baseHref: string = '/my/app', binding: any = CONST_EXPR([])): Location {
|
||||
browserLocation = new DummyBrowserLocation();
|
||||
browserLocation.internalBaseHref = baseHref;
|
||||
locationStrategy = new MockLocationStrategy();
|
||||
locationStrategy.internalBaseHref = baseHref;
|
||||
let injector = Injector.resolveAndCreate(
|
||||
[Location, bind(BrowserLocation).toValue(browserLocation), binding]);
|
||||
[Location, bind(LocationStrategy).toValue(locationStrategy), binding]);
|
||||
return location = injector.get(Location);
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ export function main() {
|
||||
|
||||
it('should normalize relative urls on navigate', () => {
|
||||
location.go('user/btford');
|
||||
expect(browserLocation.path()).toEqual('/my/app/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(browserLocation.getBaseHref()); });
|
||||
() => { expect(location.normalizeAbsolutely('')).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/');
|
||||
@@ -48,17 +48,17 @@ export function main() {
|
||||
|
||||
it('should not append urls with leading slash on navigate', () => {
|
||||
location.go('/my/app/user/btford');
|
||||
expect(browserLocation.path()).toEqual('/my/app/user/btford');
|
||||
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(browserLocation.path()).toEqual('/my/app/user/btford');
|
||||
expect(locationStrategy.path()).toEqual('/my/app/user/btford');
|
||||
});
|
||||
|
||||
it('should normalize urls on popstate', inject([AsyncTestCompleter], (async) => {
|
||||
browserLocation.simulatePopState('/my/app/user/btford');
|
||||
locationStrategy.simulatePopState('/my/app/user/btford');
|
||||
location.subscribe((ev) => {
|
||||
expect(ev['url']).toEqual('/user/btford');
|
||||
async.done();
|
||||
@@ -66,14 +66,14 @@ export function main() {
|
||||
}));
|
||||
|
||||
it('should normalize location path', () => {
|
||||
browserLocation.internalPath = '/my/app/user/btford';
|
||||
locationStrategy.internalPath = '/my/app/user/btford';
|
||||
expect(location.path()).toEqual('/user/btford');
|
||||
});
|
||||
|
||||
it('should use optional base href param', () => {
|
||||
let location = makeLocation('/', bind(appBaseHrefToken).toValue('/my/custom/href'));
|
||||
location.go('user/btford');
|
||||
expect(browserLocation.path()).toEqual('/my/custom/href/user/btford');
|
||||
expect(locationStrategy.path()).toEqual('/my/custom/href/user/btford');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ import {RouteConfig} from 'angular2/src/router/route_config_decorator';
|
||||
import {PromiseWrapper} from 'angular2/src/facade/async';
|
||||
import {BaseException} from 'angular2/src/facade/lang';
|
||||
import {routerInjectables, Router, appBaseHrefToken, routerDirectives} from 'angular2/router';
|
||||
import {BrowserLocation} from 'angular2/src/router/browser_location';
|
||||
import {DummyBrowserLocation} from 'angular2/src/mock/browser_location_mock';
|
||||
import {LocationStrategy} from 'angular2/src/router/location_strategy';
|
||||
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
|
||||
|
||||
export function main() {
|
||||
describe('router injectables', () => {
|
||||
@@ -32,12 +32,7 @@ export function main() {
|
||||
DOM.appendChild(fakeDoc.body, el);
|
||||
testBindings = [
|
||||
routerInjectables,
|
||||
bind(BrowserLocation)
|
||||
.toFactory(() => {
|
||||
var browserLocation = new DummyBrowserLocation();
|
||||
browserLocation.spy('pushState');
|
||||
return browserLocation;
|
||||
}),
|
||||
bind(LocationStrategy).toClass(MockLocationStrategy),
|
||||
bind(DOCUMENT_TOKEN).toValue(fakeDoc)
|
||||
];
|
||||
});
|
||||
@@ -48,7 +43,7 @@ export function main() {
|
||||
var router = applicationRef.hostComponent.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('outer { hello }');
|
||||
expect(applicationRef.hostComponent.location.path()).toEqual('/');
|
||||
expect(applicationRef.hostComponent.location.path()).toEqual('');
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
@@ -109,7 +104,7 @@ class HelloCmp {
|
||||
@View({template: "outer { <router-outlet></router-outlet> }", directives: routerDirectives})
|
||||
@RouteConfig([{path: '/', component: HelloCmp}])
|
||||
class AppCmp {
|
||||
constructor(public router: Router, public location: BrowserLocation) {}
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +119,7 @@ class ParentCmp {
|
||||
@View({template: `root { <router-outlet></router-outlet> }`, directives: routerDirectives})
|
||||
@RouteConfig([{path: '/parent/...', component: ParentCmp}])
|
||||
class HierarchyAppCmp {
|
||||
constructor(public router: Router, public location: BrowserLocation) {}
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({selector: 'oops-cmp'})
|
||||
@@ -137,5 +132,5 @@ class BrokenCmp {
|
||||
@View({template: "outer { <router-outlet></router-outlet> }", directives: routerDirectives})
|
||||
@RouteConfig([{path: '/cause-error', component: BrokenCmp}])
|
||||
class BrokenAppCmp {
|
||||
constructor(public router: Router, public location: BrowserLocation) {}
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user