Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion modules/socket-engine/spec/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

import { BrowserModule } from '@angular/platform-browser';
Expand Down Expand Up @@ -88,4 +88,28 @@ describe('test runner', () => {
expect(result.error).toBeUndefined();
done();
});
it('should be able to inject some token', async(done) => {
const SOME_TOKEN = new InjectionToken<string>('SOME_TOKEN');
const someValue = {message: 'value' + new Date()};
@Component({
selector: 'root',
template: `message:{{_someToken.message}}`
})
class MockComponent {constructor(@Inject(SOME_TOKEN) public readonly _someToken: any) {}}
const appModule = makeTestingModule('', MockComponent);
const server = await startSocketEngine(appModule, [{provide: SOME_TOKEN, useValue: someValue}]);

const client = net.createConnection(9090, 'localhost', () => {
const renderOptions = {id: 1, url: '/path',
document: '<root></root>'} as SocketEngineRenderOptions;
client.write(JSON.stringify(renderOptions));
});

client.on('data', data => {
const res = JSON.parse(data.toString()) as SocketEngineResponse;
server.close();
expect(res.html).toContain(someValue.message);
done();
});
});
});
5 changes: 3 additions & 2 deletions modules/socket-engine/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { ɵCommonEngine as CommonEngine,
ɵRenderOptions as RenderOptions } from '@nguniversal/common/engine';
import { NgModuleFactory, Type } from '@angular/core';
import { NgModuleFactory, Type, StaticProvider } from '@angular/core';
import * as net from 'net';

export interface SocketEngineServer {
Expand All @@ -26,11 +26,12 @@ export interface SocketEngineResponse {

export function startSocketEngine(
moduleOrFactory: Type<{}> | NgModuleFactory<{}>,
providers: StaticProvider[] = [],
host = 'localhost',
port = 9090
): Promise<SocketEngineServer> {
return new Promise((resolve, _reject) => {
const engine = new CommonEngine(moduleOrFactory);
const engine = new CommonEngine(moduleOrFactory, providers);

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