Skip to content

Commit 39f5909

Browse files
committed
test: add unit test for aspnet engine
1 parent 6a0659d commit 39f5909

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

modules/aspnetcore-engine/BUILD.bazel

+21
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,34 @@ ng_package(
3434
],
3535
)
3636

37+
# NgModule used only for tests.
38+
ng_module(
39+
name = "testing",
40+
srcs = glob([
41+
"testing/**/*.ts",
42+
]),
43+
module_name = "@nguniversal/aspnetcore-engine/testing",
44+
deps = [
45+
":aspnetcore-engine",
46+
"@npm//@angular/platform-browser",
47+
"@npm//@angular/platform-server",
48+
],
49+
)
50+
3751
ng_test_library(
3852
name = "unit_test_lib",
3953
srcs = glob([
4054
"spec/**/*.spec.ts",
4155
]),
4256
deps = [
4357
":aspnetcore-engine",
58+
":testing",
59+
"@npm//@angular/compiler",
60+
"@npm//@angular/platform-browser",
61+
"@npm//@angular/platform-server",
62+
"@npm//domino",
63+
"@npm//xhr2",
64+
"@npm//zone.js",
4465
],
4566
)
4667

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
describe('test runner', () => {
2-
it('can run a test', () => {
3-
expect(true).toBe(true);
1+
import 'zone.js';
2+
3+
import { ngAspnetCoreEngine } from '@nguniversal/aspnetcore-engine';
4+
import { MockServerModule } from '../testing/mock.server.module';
5+
6+
describe('ASPNETCore Engine', () => {
7+
it('should render a basic template', async () => {
8+
const { html } = await ngAspnetCoreEngine({
9+
appSelector: '<root></root>',
10+
request: {
11+
data: {
12+
request: 'localhost'
13+
}
14+
} as any,
15+
ngModule: MockServerModule
16+
});
17+
18+
expect(html).toContain('some template');
419
});
520
});

modules/aspnetcore-engine/src/main.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import { IEngineRenderResult } from './interfaces/engine-render-result';
1717
import { renderModuleFactory } from './platform-server-utils';
1818

1919
/* @internal */
20-
let appSelector = 'app-root'; // default
21-
22-
/* @internal */
23-
function _getUniversalData(doc: Document): Omit<IEngineRenderResult, 'moduleRef'> {
20+
function _getUniversalData(doc: Document, appSelector: string): Omit<IEngineRenderResult, 'moduleRef'> {
2421

2522
const styles: string[] = [];
2623
const scripts: string[] = [];
@@ -69,13 +66,13 @@ function _getUniversalData(doc: Document): Omit<IEngineRenderResult, 'moduleRef'
6966
export async function ngAspnetCoreEngine(options: Readonly<IEngineOptions>)
7067
: Promise<IEngineRenderResult> {
7168
if (!options.appSelector) {
72-
const selector = `" appSelector: '<${appSelector}></${appSelector}>' "`;
69+
const selector = `" appSelector: '<app-root></app-root>' "`;
7370
throw new Error(`appSelector is required! Pass in ${selector},
7471
for your root App component.`);
7572
}
7673

7774
// Grab the DOM "selector" from the passed in Template <app-root> for example = "app-root"
78-
appSelector = options.appSelector.substring(1, options.appSelector.indexOf('>'));
75+
const appSelector = options.appSelector.substring(1, options.appSelector.indexOf('>'));
7976

8077
const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);
8178
const compiler: Compiler = compilerFactory.createCompiler([
@@ -107,7 +104,7 @@ export async function ngAspnetCoreEngine(options: Readonly<IEngineOptions>)
107104

108105
return {
109106
moduleRef: result.moduleRef,
110-
..._getUniversalData(doc),
107+
..._getUniversalData(doc, appSelector),
111108
};
112109
}
113110

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Component, NgModule } from '@angular/core';
9+
import { BrowserModule } from '@angular/platform-browser';
10+
import { ServerModule } from '@angular/platform-server';
11+
12+
@Component({selector: 'root', template: 'some template'})
13+
export class MockComponent {
14+
}
15+
16+
@NgModule({
17+
imports: [BrowserModule.withServerTransition({appId: 'mock'}), ServerModule],
18+
declarations: [MockComponent],
19+
bootstrap: [MockComponent],
20+
})
21+
export class MockServerModule {
22+
}

0 commit comments

Comments
 (0)