fix(platform-server): allow multiple instances of platformServer and platformDynamicServer
This commit is contained in:

committed by
Igor Minar

parent
0e2fd9d91a
commit
17486fd696
@ -25,126 +25,155 @@ class MyServerApp {
|
||||
class ExampleModule {
|
||||
}
|
||||
|
||||
@Component({selector: 'app', template: `Works too!`})
|
||||
class MyServerApp2 {
|
||||
}
|
||||
|
||||
@NgModule({declarations: [MyServerApp2], imports: [ServerModule], bootstrap: [MyServerApp2]})
|
||||
class ExampleModule2 {
|
||||
}
|
||||
|
||||
@Component({selector: 'app', template: '{{text}}'})
|
||||
class MyAsyncServerApp {
|
||||
text = '';
|
||||
|
||||
ngOnInit() {
|
||||
Promise.resolve(null).then(() => setTimeout(() => { this.text = 'Works!'; }, 10));
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule(
|
||||
{declarations: [MyAsyncServerApp], imports: [ServerModule], bootstrap: [MyAsyncServerApp]})
|
||||
class AsyncServerModule {
|
||||
}
|
||||
|
||||
export function main() {
|
||||
if (getDOM().supportsDOMEvents()) return; // NODE only
|
||||
|
||||
describe('platform-server integration', () => {
|
||||
|
||||
beforeEach(() => destroyPlatform());
|
||||
afterEach(() => destroyPlatform());
|
||||
beforeEach(() => {
|
||||
if (getPlatform()) destroyPlatform();
|
||||
});
|
||||
|
||||
it('should bootstrap', async(() => {
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.bootstrapModule(ExampleModule)
|
||||
.then((moduleRef) => {
|
||||
const doc = moduleRef.injector.get(DOCUMENT);
|
||||
expect(getDOM().getText(doc)).toEqual('Works!');
|
||||
});
|
||||
const platform = platformDynamicServer(
|
||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||
|
||||
platform.bootstrapModule(ExampleModule).then((moduleRef) => {
|
||||
const doc = moduleRef.injector.get(DOCUMENT);
|
||||
expect(getDOM().getText(doc)).toEqual('Works!');
|
||||
platform.destroy();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should allow multiple platform instances', async(() => {
|
||||
const platform = platformDynamicServer(
|
||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||
|
||||
const platform2 = platformDynamicServer(
|
||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||
|
||||
|
||||
platform.bootstrapModule(ExampleModule).then((moduleRef) => {
|
||||
const doc = moduleRef.injector.get(DOCUMENT);
|
||||
expect(getDOM().getText(doc)).toEqual('Works!');
|
||||
platform.destroy();
|
||||
});
|
||||
|
||||
platform2.bootstrapModule(ExampleModule2).then((moduleRef) => {
|
||||
const doc = moduleRef.injector.get(DOCUMENT);
|
||||
expect(getDOM().getText(doc)).toEqual('Works too!');
|
||||
platform2.destroy();
|
||||
});
|
||||
}));
|
||||
|
||||
describe('PlatformLocation', () => {
|
||||
it('is injectable', () => {
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.bootstrapModule(ExampleModule)
|
||||
.then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
expect(location.pathname).toBe('/');
|
||||
});
|
||||
});
|
||||
it('pushState causes the URL to update', () => {
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.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('is injectable', async(() => {
|
||||
const platform = platformDynamicServer(
|
||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||
platform.bootstrapModule(ExampleModule).then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
expect(location.pathname).toBe('/');
|
||||
platform.destroy();
|
||||
});
|
||||
}));
|
||||
it('pushState causes the URL to update', async(() => {
|
||||
const platform = platformDynamicServer(
|
||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||
platform.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');
|
||||
platform.destroy();
|
||||
});
|
||||
}));
|
||||
it('allows subscription to the hash state', done => {
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.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');
|
||||
});
|
||||
const platform =
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||
platform.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');
|
||||
platform.destroy();
|
||||
done();
|
||||
});
|
||||
location.pushState(null, 'Test', '/foo#bar');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Platform Server', () => {
|
||||
@Component({selector: 'app', template: '{{text}}'})
|
||||
class MyAsyncServerApp {
|
||||
text = '';
|
||||
describe('render', () => {
|
||||
let doc: string;
|
||||
let called: boolean;
|
||||
let expectedOutput =
|
||||
'<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>';
|
||||
|
||||
ngOnInit() {
|
||||
Promise.resolve(null).then(() => setTimeout(() => { this.text = 'Works!'; }, 10));
|
||||
}
|
||||
}
|
||||
beforeEach(() => {
|
||||
// PlatformConfig takes in a parsed document so that it can be cached across requests.
|
||||
doc = '<html><head></head><body><app></app></body></html>';
|
||||
called = false;
|
||||
});
|
||||
afterEach(() => { expect(called).toBe(true); });
|
||||
|
||||
@NgModule(
|
||||
{declarations: [MyAsyncServerApp], imports: [ServerModule], bootstrap: [MyAsyncServerApp]})
|
||||
class AsyncServerModule {
|
||||
}
|
||||
it('using long from should work', async(() => {
|
||||
const platform =
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: doc}}]);
|
||||
|
||||
let doc: string;
|
||||
let called: boolean;
|
||||
let expectedOutput =
|
||||
'<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>';
|
||||
platform.bootstrapModule(AsyncServerModule)
|
||||
.then((moduleRef) => {
|
||||
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
||||
return toPromise.call(first.call(
|
||||
filter.call(applicationRef.isStable, (isStable: boolean) => isStable)));
|
||||
})
|
||||
.then((b) => {
|
||||
expect(platform.injector.get(PlatformState).renderToString()).toBe(expectedOutput);
|
||||
platform.destroy();
|
||||
called = true;
|
||||
});
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
destroyPlatform();
|
||||
// PlatformConfig takes in a parsed document so that it can be cached across requests.
|
||||
doc = '<html><head></head><body><app></app></body></html>';
|
||||
called = false;
|
||||
it('using renderModule should work', async(() => {
|
||||
renderModule(AsyncServerModule, {document: doc}).then(output => {
|
||||
expect(output).toBe(expectedOutput);
|
||||
called = true;
|
||||
});
|
||||
}));
|
||||
|
||||
it('using renderModuleFactory should work',
|
||||
async(inject([PlatformRef], (defaultPlatform: PlatformRef) => {
|
||||
const compilerFactory: CompilerFactory =
|
||||
defaultPlatform.injector.get(CompilerFactory, null);
|
||||
const moduleFactory =
|
||||
compilerFactory.createCompiler().compileModuleSync(AsyncServerModule);
|
||||
renderModuleFactory(moduleFactory, {document: doc}).then(output => {
|
||||
expect(output).toBe(expectedOutput);
|
||||
called = true;
|
||||
});
|
||||
})));
|
||||
});
|
||||
afterEach(() => {
|
||||
expect(called).toBe(true);
|
||||
// Platform should have been destroyed at the end of rendering.
|
||||
expect(getPlatform()).toBeNull();
|
||||
});
|
||||
|
||||
it('PlatformState should render to string (Long form rendering)', async(() => {
|
||||
const platform =
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: doc}}]);
|
||||
|
||||
platform.bootstrapModule(AsyncServerModule)
|
||||
.then((moduleRef) => {
|
||||
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
||||
return toPromise.call(first.call(
|
||||
filter.call(applicationRef.isStable, (isStable: boolean) => isStable)));
|
||||
})
|
||||
.then((b) => {
|
||||
expect(platform.injector.get(PlatformState).renderToString()).toBe(expectedOutput);
|
||||
destroyPlatform();
|
||||
called = true;
|
||||
});
|
||||
}));
|
||||
|
||||
it('renderModule should render to string (short form rendering)', async(() => {
|
||||
renderModule(AsyncServerModule, {document: doc}).then(output => {
|
||||
expect(output).toBe(expectedOutput);
|
||||
called = true;
|
||||
});
|
||||
}));
|
||||
|
||||
it('renderModuleFactory should render to string (short form rendering)',
|
||||
async(inject([PlatformRef], (defaultPlatform: PlatformRef) => {
|
||||
const compilerFactory: CompilerFactory =
|
||||
defaultPlatform.injector.get(CompilerFactory, null);
|
||||
const moduleFactory =
|
||||
compilerFactory.createCompiler().compileModuleSync(AsyncServerModule);
|
||||
renderModuleFactory(moduleFactory, {document: doc}).then(output => {
|
||||
expect(output).toBe(expectedOutput);
|
||||
called = true;
|
||||
});
|
||||
})));
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user