|
| 1 | + |
| 2 | +import { ServerModule } from '@angular/platform-server'; |
| 3 | +import { NgModule, Component } from '@angular/core'; |
| 4 | +import 'zone.js'; |
| 5 | + |
| 6 | +import { BrowserModule } from '@angular/platform-browser'; |
| 7 | +import { startSocketEngine, SocketEngineResponse, |
| 8 | + SocketEngineRenderOptions } from '@nguniversal/socket-engine'; |
| 9 | +import * as net from 'net'; |
| 10 | + |
| 11 | +export function makeTestingModule(template: string, component?: any): any { |
| 12 | + @Component({ |
| 13 | + selector: 'root', |
| 14 | + template: template |
| 15 | + }) |
| 16 | + class MockComponent {} |
| 17 | + @NgModule({ |
| 18 | + imports: [ServerModule, BrowserModule.withServerTransition({appId: 'mock'})], |
| 19 | + declarations: [component || MockComponent], |
| 20 | + bootstrap: [component || MockComponent] |
| 21 | + }) |
| 22 | + class MockServerModule {} |
| 23 | + return MockServerModule; |
| 24 | +} |
| 25 | + |
| 26 | +async function sendAndRecieve(renderOptions: SocketEngineRenderOptions, template = '') { |
| 27 | + return new Promise<SocketEngineResponse>(async(resolve, _reject) => { |
| 28 | + |
| 29 | + const appModule = makeTestingModule(template); |
| 30 | + const server = await startSocketEngine(appModule); |
| 31 | + |
| 32 | + const client = net.createConnection(9090, 'localhost', () => { |
| 33 | + client.write(JSON.stringify(renderOptions)); |
| 34 | + }); |
| 35 | + |
| 36 | + client.on('data', data => { |
| 37 | + const res = JSON.parse(data.toString()) as SocketEngineResponse; |
| 38 | + server.close(); |
| 39 | + resolve(res); |
| 40 | + }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +describe('test runner', () => { |
| 45 | + it('should render a basic template', async (done) => { |
| 46 | + const template = `${new Date()}`; |
| 47 | + const renderOptions = {id: 1, url: '/path', |
| 48 | + document: '<root></root>'} as SocketEngineRenderOptions; |
| 49 | + const result = await sendAndRecieve(renderOptions, template); |
| 50 | + expect(result.html).toContain(template); |
| 51 | + done(); |
| 52 | + }); |
| 53 | + it('should return the same id', async(done) => { |
| 54 | + const id = Math.random(); |
| 55 | + const renderOptions = {id , url: '/path', |
| 56 | + document: '<root></root>'} as SocketEngineRenderOptions; |
| 57 | + const result = await sendAndRecieve(renderOptions); |
| 58 | + expect(result.id).toEqual(id); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + it('should return an error if it cant render', async(done) => { |
| 62 | + @Component({ |
| 63 | + selector: 'root', |
| 64 | + template: '' |
| 65 | + }) |
| 66 | + class MockComponent {constructor(_illMakeItThrow: '') {}} |
| 67 | + const appModule = makeTestingModule('', MockComponent); |
| 68 | + const server = await startSocketEngine(appModule); |
| 69 | + |
| 70 | + const client = net.createConnection(9090, 'localhost', () => { |
| 71 | + const renderOptions = {id: 1, url: '/path', |
| 72 | + document: '<root></root>'} as SocketEngineRenderOptions; |
| 73 | + client.write(JSON.stringify(renderOptions)); |
| 74 | + }); |
| 75 | + |
| 76 | + client.on('data', data => { |
| 77 | + const res = JSON.parse(data.toString()) as SocketEngineResponse; |
| 78 | + server.close(); |
| 79 | + expect(res.error).not.toBeNull(); |
| 80 | + done(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + it('should return an error if it cant render', async(done) => { |
| 84 | + const template = `${new Date()}`; |
| 85 | + const renderOptions = {id: 1, url: '/path', |
| 86 | + document: '<root></root>'} as SocketEngineRenderOptions; |
| 87 | + const result = await sendAndRecieve(renderOptions, template); |
| 88 | + expect(result.error).toBeUndefined(); |
| 89 | + done(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments