test: move platform-server integration test (#22810)
Now it lives in our standard location for tests against npm packages PR Close #22810
This commit is contained in:
20
integration/platform-server/src/helloworld/app.server.ts
Normal file
20
integration/platform-server/src/helloworld/app.server.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {ServerModule} from '@angular/platform-server';
|
||||
|
||||
import {HelloWorldModule} from './app';
|
||||
import {HelloWorldComponent} from './hello-world.component';
|
||||
|
||||
@NgModule({
|
||||
bootstrap: [HelloWorldComponent],
|
||||
imports: [HelloWorldModule, ServerModule],
|
||||
})
|
||||
export class HelloWorldServerModule {
|
||||
}
|
20
integration/platform-server/src/helloworld/app.ts
Normal file
20
integration/platform-server/src/helloworld/app.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
|
||||
import {HelloWorldComponent} from './hello-world.component';
|
||||
|
||||
@NgModule({
|
||||
declarations: [HelloWorldComponent],
|
||||
bootstrap: [HelloWorldComponent],
|
||||
imports: [BrowserModule.withServerTransition({appId: 'hlw'})],
|
||||
})
|
||||
export class HelloWorldModule {
|
||||
}
|
17
integration/platform-server/src/helloworld/client.ts
Normal file
17
integration/platform-server/src/helloworld/client.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import 'zone.js/dist/zone.js';
|
||||
|
||||
import {enableProdMode} from '@angular/core';
|
||||
import {platformBrowser} from '@angular/platform-browser';
|
||||
import {HelloWorldModuleNgFactory} from './app.ngfactory';
|
||||
|
||||
window['doBootstrap'] = function() {
|
||||
platformBrowser().bootstrapModuleFactory(HelloWorldModuleNgFactory);
|
||||
};
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {Component} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'hello-world-app',
|
||||
template: `
|
||||
<div>Hello {{ name }}!</div>
|
||||
`,
|
||||
styles: [`
|
||||
div {
|
||||
font-weight: bold;
|
||||
}
|
||||
`]
|
||||
})
|
||||
export class HelloWorldComponent {
|
||||
name: string = 'world';
|
||||
}
|
10
integration/platform-server/src/helloworld/index.html
Normal file
10
integration/platform-server/src/helloworld/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello World</title>
|
||||
<script src="built/helloworld-bundle.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<hello-world-app></hello-world-app>
|
||||
</body>
|
||||
</html>
|
44
integration/platform-server/src/server.ts
Normal file
44
integration/platform-server/src/server.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
/* tslint:disable:no-console */
|
||||
require('zone.js/dist/zone-node.js');
|
||||
|
||||
import {enableProdMode, NgModuleFactory} from '@angular/core';
|
||||
import {renderModuleFactory} from '@angular/platform-server';
|
||||
import * as express from 'express';
|
||||
|
||||
import {HelloWorldServerModuleNgFactory} from './helloworld/app.server.ngfactory';
|
||||
const helloworld = require('raw-loader!./helloworld/index.html');
|
||||
|
||||
import {TransferStateServerModuleNgFactory} from './transferstate/app.server.ngfactory';
|
||||
const transferstate = require('raw-loader!./transferstate/index.html');
|
||||
|
||||
const app = express();
|
||||
|
||||
function render<T>(moduleFactory: NgModuleFactory<T>, html: string) {
|
||||
return (req, res) => {
|
||||
renderModuleFactory(moduleFactory, {
|
||||
document: html,
|
||||
url: req.url,
|
||||
}).then((response) => { res.send(response); });
|
||||
};
|
||||
}
|
||||
|
||||
enableProdMode();
|
||||
|
||||
// Client bundles will be statically served from the built/ directory.
|
||||
app.use('/built', express.static('built'));
|
||||
|
||||
// Keep the browser logs free of errors.
|
||||
app.get('/favicon.ico', (req, res) => { res.send(''); });
|
||||
|
||||
//-----------ADD YOUR SERVER SIDE RENDERED APP HERE ----------------------
|
||||
app.get('/helloworld', render(HelloWorldServerModuleNgFactory, helloworld));
|
||||
app.get('/transferstate', render(TransferStateServerModuleNgFactory, transferstate));
|
||||
|
||||
app.listen(9876, function() { console.log('Server listening on port 9876!'); });
|
20
integration/platform-server/src/transferstate/app.server.ts
Normal file
20
integration/platform-server/src/transferstate/app.server.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {ServerModule, ServerTransferStateModule} from '@angular/platform-server';
|
||||
|
||||
import {TransferStateModule} from './app';
|
||||
import {TransferStateComponent} from './transfer-state.component';
|
||||
|
||||
@NgModule({
|
||||
bootstrap: [TransferStateComponent],
|
||||
imports: [TransferStateModule, ServerModule, ServerTransferStateModule],
|
||||
})
|
||||
export class TransferStateServerModule {
|
||||
}
|
23
integration/platform-server/src/transferstate/app.ts
Normal file
23
integration/platform-server/src/transferstate/app.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {BrowserModule, BrowserTransferStateModule} from '@angular/platform-browser';
|
||||
|
||||
import {TransferStateComponent} from './transfer-state.component';
|
||||
|
||||
@NgModule({
|
||||
declarations: [TransferStateComponent],
|
||||
bootstrap: [TransferStateComponent],
|
||||
imports: [
|
||||
BrowserModule.withServerTransition({appId: 'ts'}),
|
||||
BrowserTransferStateModule,
|
||||
],
|
||||
})
|
||||
export class TransferStateModule {
|
||||
}
|
17
integration/platform-server/src/transferstate/client.ts
Normal file
17
integration/platform-server/src/transferstate/client.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import 'zone.js/dist/zone.js';
|
||||
|
||||
import {enableProdMode} from '@angular/core';
|
||||
import {platformBrowser} from '@angular/platform-browser';
|
||||
import {TransferStateModuleNgFactory} from './app.ngfactory';
|
||||
|
||||
window['doBootstrap'] = function() {
|
||||
platformBrowser().bootstrapModuleFactory(TransferStateModuleNgFactory);
|
||||
};
|
10
integration/platform-server/src/transferstate/index.html
Normal file
10
integration/platform-server/src/transferstate/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello World</title>
|
||||
<script src="built/transferstate-bundle.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<transfer-state-app></transfer-state-app>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {isPlatformServer} from '@angular/common';
|
||||
import {Component, Inject, PLATFORM_ID} from '@angular/core';
|
||||
import {StateKey, TransferState, makeStateKey} from '@angular/platform-browser';
|
||||
|
||||
const COUNTER_KEY = makeStateKey<number>('counter');
|
||||
|
||||
@Component({
|
||||
selector: 'transfer-state-app',
|
||||
template: `
|
||||
<div>{{counter}}</div>
|
||||
`,
|
||||
})
|
||||
export class TransferStateComponent {
|
||||
counter = 0;
|
||||
|
||||
constructor(@Inject(PLATFORM_ID) private platformId: {}, private transferState: TransferState) {}
|
||||
|
||||
ngOnInit() {
|
||||
if (isPlatformServer(this.platformId)) {
|
||||
// Set it to 5 in the server.
|
||||
this.counter = 5;
|
||||
this.transferState.set(COUNTER_KEY, 50);
|
||||
} else {
|
||||
// Get the transferred counter state in the client(should be 50 and not 0).
|
||||
this.counter = this.transferState.get(COUNTER_KEY, 0);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user