2016-04-12 09:40:37 -07:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
ddescribe,
|
|
|
|
expect,
|
|
|
|
inject,
|
|
|
|
beforeEach,
|
|
|
|
beforeEachProviders,
|
|
|
|
SpyObject
|
|
|
|
} from 'angular2/testing_internal';
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2016-04-14 12:35:24 -07:00
|
|
|
import {Injector, provide, ReflectiveInjector} from 'angular2/core';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-11-17 10:47:59 -08:00
|
|
|
|
refactor(Location): out of router and into platform/common
closes https://github.com/angular/angular/issues/4943
BREAKING CHANGE:
`Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change.
Before:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/router';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
```
After:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/platform/common';
import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location';
```
Closes #7962
2016-04-08 00:31:20 -07:00
|
|
|
import {Location, LocationStrategy, APP_BASE_HREF} from 'angular2/platform/common';
|
2015-06-22 12:14:19 -07:00
|
|
|
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
|
2015-05-06 18:28:24 -07:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('Location', () => {
|
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
var locationStrategy, location;
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-10-10 22:11:13 -07:00
|
|
|
function makeLocation(baseHref: string = '/my/app', provider: any = CONST_EXPR([])): Location {
|
2015-06-22 12:14:19 -07:00
|
|
|
locationStrategy = new MockLocationStrategy();
|
|
|
|
locationStrategy.internalBaseHref = baseHref;
|
2016-04-14 12:35:24 -07:00
|
|
|
let injector = ReflectiveInjector.resolveAndCreate(
|
2015-10-12 11:30:34 -07:00
|
|
|
[Location, provide(LocationStrategy, {useValue: locationStrategy}), provider]);
|
2015-06-15 15:41:09 -07:00
|
|
|
return location = injector.get(Location);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(makeLocation);
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-06-12 11:29:31 -07:00
|
|
|
it('should not prepend urls with starting slash when an empty URL is provided',
|
2015-10-26 13:57:41 +00:00
|
|
|
() => { expect(location.prepareExternalUrl('')).toEqual(locationStrategy.getBaseHref()); });
|
2015-06-12 11:29:31 -07:00
|
|
|
|
2015-06-12 12:57:35 -07:00
|
|
|
it('should not prepend path with an extra slash when a baseHref has a trailing slash', () => {
|
2015-06-15 15:41:09 -07:00
|
|
|
let location = makeLocation('/my/slashed/app/');
|
2015-10-26 13:57:41 +00:00
|
|
|
expect(location.prepareExternalUrl('/page')).toEqual('/my/slashed/app/page');
|
2015-06-12 12:57:35 -07:00
|
|
|
});
|
|
|
|
|
2015-05-11 14:49:55 -07:00
|
|
|
it('should not append urls with leading slash on navigate', () => {
|
2015-05-06 18:28:24 -07:00
|
|
|
location.go('/my/app/user/btford');
|
2015-06-22 12:14:19 -07:00
|
|
|
expect(locationStrategy.path()).toEqual('/my/app/user/btford');
|
2015-05-06 18:28:24 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should normalize urls on popstate', inject([AsyncTestCompleter], (async) => {
|
2015-06-22 12:14:19 -07:00
|
|
|
locationStrategy.simulatePopState('/my/app/user/btford');
|
2015-05-29 14:58:41 -07:00
|
|
|
location.subscribe((ev) => {
|
|
|
|
expect(ev['url']).toEqual('/user/btford');
|
|
|
|
async.done();
|
|
|
|
})
|
2015-09-09 16:41:11 +02:00
|
|
|
}));
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-07-28 23:19:56 -04:00
|
|
|
it('should revert to the previous path when a back() operation is executed', () => {
|
|
|
|
var locationStrategy = new MockLocationStrategy();
|
|
|
|
var location = new Location(locationStrategy);
|
|
|
|
|
2015-07-28 01:46:09 -04:00
|
|
|
function assertUrl(path) { expect(location.path()).toEqual(path); }
|
|
|
|
|
2015-07-28 23:19:56 -04:00
|
|
|
location.go('/ready');
|
|
|
|
assertUrl('/ready');
|
|
|
|
|
|
|
|
location.go('/ready/set');
|
|
|
|
assertUrl('/ready/set');
|
|
|
|
|
|
|
|
location.go('/ready/set/go');
|
|
|
|
assertUrl('/ready/set/go');
|
|
|
|
|
|
|
|
location.back();
|
|
|
|
assertUrl('/ready/set');
|
|
|
|
|
|
|
|
location.back();
|
|
|
|
assertUrl('/ready');
|
|
|
|
});
|
2015-10-07 16:56:35 -07:00
|
|
|
|
|
|
|
it('should incorporate the provided query values into the location change', () => {
|
|
|
|
var locationStrategy = new MockLocationStrategy();
|
|
|
|
var location = new Location(locationStrategy);
|
|
|
|
|
2016-04-12 09:40:37 -07:00
|
|
|
location.go('/home', "key=value");
|
|
|
|
expect(location.path()).toEqual("/home?key=value");
|
2015-10-07 16:56:35 -07:00
|
|
|
});
|
2015-06-15 15:41:09 -07:00
|
|
|
});
|
2015-05-06 18:28:24 -07:00
|
|
|
}
|