-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathapp-shell_spec.ts
182 lines (153 loc) · 5.39 KB
/
app-shell_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @license
* Copyright Google LLC 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 { Architect } from '@angular-devkit/architect';
import { normalize, virtualFs } from '@angular-devkit/core';
import { createArchitect, host } from '../../testing/test-utils';
describe('AppShell Builder', () => {
const target = { project: 'app', target: 'app-shell' };
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(async () => host.restore().toPromise());
const appShellRouteFiles = {
'src/styles.css': `
p { color: #000 }
`,
'src/app/app-shell/app-shell.component.html': `
<p>
app-shell works!
</p>
`,
'src/app/app-shell/app-shell.component.ts': `
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-app-shell',
templateUrl: './app-shell.component.html',
})
export class AppShellComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
`,
'src/app/app.module.ts': `
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import { RouterModule } from '@angular/router';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
AppRoutingModule,
RouterModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`,
'src/app/app.server.module.ts': `
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { Routes, RouterModule } from '@angular/router';
import { AppShellComponent } from './app-shell/app-shell.component';
const routes: Routes = [ { path: 'shell', component: AppShellComponent }];
@NgModule({
imports: [
AppModule,
ServerModule,
RouterModule.forRoot(routes),
],
bootstrap: [AppComponent],
declarations: [AppShellComponent],
})
export class AppServerModule {}
`,
'src/main.ts': `
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
document.addEventListener('DOMContentLoaded', () => {
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
});
`,
'src/app/app-routing.module.ts': `
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
`,
'src/app/app.component.html': `
<router-outlet></router-outlet>
`,
};
it('works (basic)', async () => {
host.replaceInFile(
'src/app/app.module.ts',
/ {4}BrowserModule/,
`
BrowserModule.withServerTransition({ appId: 'some-app' })
`,
);
const run = await architect.scheduleTarget(target);
const output = await run.result;
await run.stop();
expect(output.success).toBe(true);
const fileName = 'dist/index.html';
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toMatch('Welcome to app');
expect(content).toMatch('ng-server-context="app-shell"');
});
it('works with route', async () => {
host.writeMultipleFiles(appShellRouteFiles);
const overrides = { route: 'shell' };
const run = await architect.scheduleTarget(target, overrides);
const output = await run.result;
await run.stop();
expect(output.success).toBe(true);
const fileName = 'dist/index.html';
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toContain('app-shell works!');
});
it('critical CSS is inlined', async () => {
host.writeMultipleFiles(appShellRouteFiles);
const overrides = {
route: 'shell',
browserTarget: 'app:build:production,inline-critical-css',
};
const run = await architect.scheduleTarget(target, overrides);
const output = await run.result;
await run.stop();
expect(output.success).toBe(true);
const fileName = 'dist/index.html';
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toContain('app-shell works!');
expect(content).toContain('p{color:#000}');
expect(content).toMatch(
/<link rel="stylesheet" href="styles\.[a-z0-9]+\.css" media="print" onload="this\.media='all'">/,
);
});
});