Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit c16860c

Browse files
someApprenticeCaerusKaru
authored andcommitted
feat(socket-engine): add providers parameter (#1072)
1 parent 675e066 commit c16860c

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

modules/socket-engine/spec/index.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { ServerModule } from '@angular/platform-server';
3-
import { NgModule, Component } from '@angular/core';
3+
import { NgModule, Component, Inject, InjectionToken } from '@angular/core';
44
import 'zone.js';
55

66
import { BrowserModule } from '@angular/platform-browser';
@@ -88,4 +88,28 @@ describe('test runner', () => {
8888
expect(result.error).toBeUndefined();
8989
done();
9090
});
91+
it('should be able to inject some token', async(done) => {
92+
const SOME_TOKEN = new InjectionToken<string>('SOME_TOKEN');
93+
const someValue = {message: 'value' + new Date()};
94+
@Component({
95+
selector: 'root',
96+
template: `message:{{_someToken.message}}`
97+
})
98+
class MockComponent {constructor(@Inject(SOME_TOKEN) public readonly _someToken: any) {}}
99+
const appModule = makeTestingModule('', MockComponent);
100+
const server = await startSocketEngine(appModule, [{provide: SOME_TOKEN, useValue: someValue}]);
101+
102+
const client = net.createConnection(9090, 'localhost', () => {
103+
const renderOptions = {id: 1, url: '/path',
104+
document: '<root></root>'} as SocketEngineRenderOptions;
105+
client.write(JSON.stringify(renderOptions));
106+
});
107+
108+
client.on('data', data => {
109+
const res = JSON.parse(data.toString()) as SocketEngineResponse;
110+
server.close();
111+
expect(res.html).toContain(someValue.message);
112+
done();
113+
});
114+
});
91115
});

modules/socket-engine/src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { ɵCommonEngine as CommonEngine,
99
ɵRenderOptions as RenderOptions } from '@nguniversal/common/engine';
10-
import { NgModuleFactory, Type } from '@angular/core';
10+
import { NgModuleFactory, Type, StaticProvider } from '@angular/core';
1111
import * as net from 'net';
1212

1313
export interface SocketEngineServer {
@@ -26,11 +26,12 @@ export interface SocketEngineResponse {
2626

2727
export function startSocketEngine(
2828
moduleOrFactory: Type<{}> | NgModuleFactory<{}>,
29+
providers: StaticProvider[] = [],
2930
host = 'localhost',
3031
port = 9090
3132
): Promise<SocketEngineServer> {
3233
return new Promise((resolve, _reject) => {
33-
const engine = new CommonEngine(moduleOrFactory);
34+
const engine = new CommonEngine(moduleOrFactory, providers);
3435

3536
const server = net.createServer(socket => {
3637
socket.on('data', async buff => {

0 commit comments

Comments
 (0)