chore: router move-only
This commit is contained in:
@ -0,0 +1,182 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Injector, provide} from '@angular/core';
|
||||
|
||||
import {PlatformLocation, APP_BASE_HREF, HashLocationStrategy} from '@angular/common';
|
||||
import {SpyPlatformLocation} from '../spies';
|
||||
|
||||
export function main() {
|
||||
describe('HashLocationStrategy', () => {
|
||||
var platformLocation: SpyPlatformLocation;
|
||||
var locationStrategy: HashLocationStrategy;
|
||||
|
||||
beforeEachProviders(
|
||||
() => [HashLocationStrategy, provide(PlatformLocation, {useClass: SpyPlatformLocation})]);
|
||||
|
||||
describe('without APP_BASE_HREF', () => {
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with just query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('?bar')).toEqual('#?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with neither leading nor trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: 'app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('#app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with leading slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('#/app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with both leading and trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app/'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('#/app/');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('hashLocationStrategy bugs', () => {
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should not include platform search', () => {
|
||||
platformLocation.search = '?donotinclude';
|
||||
expect(locationStrategy.path()).toEqual('');
|
||||
});
|
||||
|
||||
it('should not include platform search even with hash', () => {
|
||||
platformLocation.hash = '#hashPath';
|
||||
platformLocation.search = '?donotinclude';
|
||||
expect(locationStrategy.path()).toEqual('hashPath');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Injector, provide, ReflectiveInjector} from '@angular/core';
|
||||
import {Location, LocationStrategy, APP_BASE_HREF} from '@angular/common';
|
||||
import {MockLocationStrategy} from '@angular/common/testing';
|
||||
|
||||
export function main() {
|
||||
describe('Location', () => {
|
||||
|
||||
var locationStrategy, location;
|
||||
|
||||
function makeLocation(baseHref: string = '/my/app',
|
||||
provider: any = /*@ts2dart_const*/[]): Location {
|
||||
locationStrategy = new MockLocationStrategy();
|
||||
locationStrategy.internalBaseHref = baseHref;
|
||||
let injector = ReflectiveInjector.resolveAndCreate(
|
||||
[Location, provide(LocationStrategy, {useValue: locationStrategy}), provider]);
|
||||
return location = injector.get(Location);
|
||||
}
|
||||
|
||||
beforeEach(makeLocation);
|
||||
|
||||
it('should not prepend urls with starting slash when an empty URL is provided',
|
||||
() => { 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.prepareExternalUrl('/page')).toEqual('/my/slashed/app/page');
|
||||
});
|
||||
|
||||
it('should not append urls with leading slash on navigate', () => {
|
||||
location.go('/my/app/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) => {
|
||||
expect(ev['url']).toEqual('/user/btford');
|
||||
async.done();
|
||||
})
|
||||
}));
|
||||
|
||||
it('should revert to the previous path when a back() operation is executed', () => {
|
||||
var locationStrategy = new MockLocationStrategy();
|
||||
var location = new Location(locationStrategy);
|
||||
|
||||
function assertUrl(path) { expect(location.path()).toEqual(path); }
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
it('should incorporate the provided query values into the location change', () => {
|
||||
var locationStrategy = new MockLocationStrategy();
|
||||
var location = new Location(locationStrategy);
|
||||
|
||||
location.go('/home', "key=value");
|
||||
expect(location.path()).toEqual("/home?key=value");
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Injector, provide} from '@angular/core';
|
||||
import {
|
||||
PlatformLocation,
|
||||
LocationStrategy,
|
||||
PathLocationStrategy,
|
||||
APP_BASE_HREF
|
||||
} from '@angular/common';
|
||||
import {SpyPlatformLocation} from '../spies';
|
||||
|
||||
export function main() {
|
||||
describe('PathLocationStrategy', () => {
|
||||
var platformLocation, locationStrategy;
|
||||
|
||||
beforeEachProviders(() => [
|
||||
PathLocationStrategy,
|
||||
provide(PlatformLocation, {useFactory: makeSpyPlatformLocation})
|
||||
]);
|
||||
|
||||
it('should throw without a base element or APP_BASE_HREF', () => {
|
||||
platformLocation = new SpyPlatformLocation();
|
||||
platformLocation.pathname = '';
|
||||
platformLocation.spy('getBaseHrefFromDOM').andReturn(null);
|
||||
|
||||
expect(() => new PathLocationStrategy(platformLocation))
|
||||
.toThrowError(
|
||||
'No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.');
|
||||
});
|
||||
|
||||
describe('without APP_BASE_HREF', () => {
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', 'foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', 'foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with neither leading nor trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: 'app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', 'app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', 'app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', 'app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with leading slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('/app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with both leading and trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app/'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('/app/');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app/');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function makeSpyPlatformLocation() {
|
||||
var platformLocation = new SpyPlatformLocation();
|
||||
platformLocation.spy('getBaseHrefFromDOM').andReturn('');
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
return platformLocation;
|
||||
}
|
Reference in New Issue
Block a user