2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-15 16:44:01 -04:00
|
|
|
import {Component, NgModule, destroyPlatform} from '@angular/core';
|
2017-02-09 17:10:00 -05:00
|
|
|
import {PlatformLocation} from '@angular/common';
|
2016-07-11 19:04:32 -04:00
|
|
|
import {async} from '@angular/core/testing';
|
2016-06-14 22:49:25 -04:00
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
2016-08-15 16:44:01 -04:00
|
|
|
import {ServerModule, platformDynamicServer} from '@angular/platform-server';
|
2016-06-14 22:49:25 -04:00
|
|
|
|
|
|
|
function writeBody(html: string): any {
|
2016-11-12 08:08:58 -05:00
|
|
|
const dom = getDOM();
|
|
|
|
const doc = dom.defaultDoc();
|
|
|
|
const body = dom.querySelector(doc, 'body');
|
2016-06-14 22:49:25 -04:00
|
|
|
dom.setInnerHTML(body, html);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:44:01 -04:00
|
|
|
|
|
|
|
@Component({selector: 'app', template: `Works!`})
|
|
|
|
class MyServerApp {
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:51:01 -04:00
|
|
|
@NgModule({declarations: [MyServerApp], imports: [ServerModule], bootstrap: [MyServerApp]})
|
2016-08-15 16:44:01 -04:00
|
|
|
class ExampleModule {
|
|
|
|
}
|
|
|
|
|
2016-06-14 22:49:25 -04:00
|
|
|
export function main() {
|
|
|
|
if (getDOM().supportsDOMEvents()) return; // NODE only
|
|
|
|
|
2017-02-10 19:48:04 -05:00
|
|
|
describe('platform-server integration', () => {
|
2016-06-14 22:49:25 -04:00
|
|
|
|
2016-08-12 17:15:37 -04:00
|
|
|
beforeEach(() => destroyPlatform());
|
|
|
|
afterEach(() => destroyPlatform());
|
2016-06-14 22:49:25 -04:00
|
|
|
|
|
|
|
it('should bootstrap', async(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const body = writeBody('<app></app>');
|
2016-08-15 16:44:01 -04:00
|
|
|
platformDynamicServer().bootstrapModule(ExampleModule).then(() => {
|
|
|
|
expect(getDOM().getText(body)).toEqual('Works!');
|
|
|
|
});
|
2016-06-14 22:49:25 -04:00
|
|
|
}));
|
2017-02-09 17:10:00 -05:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-06-14 22:49:25 -04:00
|
|
|
});
|
|
|
|
}
|