feat(platform-server): Implement PlatformLocation for platformServer() (#14405)

This gives server-side apps a current URL including hash, but doesn't implement a state stack,
so back-and-forward navigation isn't possible.

PR Close #14405
This commit is contained in:
Alex Rickabaugh
2017-02-09 14:10:00 -08:00
committed by Miško Hevery
parent db700dfc71
commit 9e28568a8f
4 changed files with 128 additions and 16 deletions

View File

@ -7,6 +7,7 @@
*/
import {Component, NgModule, destroyPlatform} from '@angular/core';
import {PlatformLocation} from '@angular/common';
import {async} from '@angular/core/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {ServerModule, platformDynamicServer} from '@angular/platform-server';
@ -31,7 +32,7 @@ class ExampleModule {
export function main() {
if (getDOM().supportsDOMEvents()) return; // NODE only
describe('platform-server integration', () => {
fdescribe('platform-server integration', () => {
beforeEach(() => destroyPlatform());
afterEach(() => destroyPlatform());
@ -42,5 +43,38 @@ export function main() {
expect(getDOM().getText(body)).toEqual('Works!');
});
}));
describe('PlatformLocation', () => {
it('is injectable', () => {
const body = writeBody('<app></app>');
platformDynamicServer().bootstrapModule(ExampleModule).then(appRef => {
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
expect(location.pathname).toBe('/');
});
});
it('pushState causes the URL to update', () => {
const body = writeBody('<app></app>');
platformDynamicServer().bootstrapModule(ExampleModule).then(appRef => {
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
location.pushState(null, 'Test', '/foo#bar');
expect(location.pathname).toBe('/foo');
expect(location.hash).toBe('#bar');
});
});
it('allows subscription to the hash state', done => {
const body = writeBody('<app></app>');
platformDynamicServer().bootstrapModule(ExampleModule).then(appRef => {
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
expect(location.pathname).toBe('/');
location.onHashChange((e: any) => {
expect(e.type).toBe('hashchange');
expect(e.oldUrl).toBe('/');
expect(e.newUrl).toBe('/foo#bar');
done();
});
location.pushState(null, 'Test', '/foo#bar');
});
});
});
});
}