-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathtesting-utils.ts
More file actions
122 lines (116 loc) · 4.17 KB
/
Copy pathtesting-utils.ts
File metadata and controls
122 lines (116 loc) · 4.17 KB
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
/**
* @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.dev/license
*/
import {
Component,
EnvironmentProviders,
Provider,
Type,
provideZonelessChangeDetection,
} from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { RouterOutlet, Routes, provideRouter } from '@angular/router';
import { type CompactPlan, compileSheet, encodePlan } from 'beasties/compiler';
import { destroyAngularServerApp } from '../src/app';
import { ServerAsset, setAngularAppManifest } from '../src/manifest';
import { ServerRoute, provideServerRendering, withRoutes } from '../src/routes/route-config';
@Component({
selector: 'app-root',
template: '<router-outlet />',
imports: [RouterOutlet],
})
class AppComponent {}
/**
* Configures the Angular application for testing by resetting any existing server application,
* setting up the Angular app manifest, and bootstrapping the application with the provided routes.
* This function generates default HTML templates (`index.server.html` and `index.csr.html`) with a base href
* and sets up the necessary Angular components and providers for testing purposes.
*
* @param routes - An array of route definitions to be used by the Angular Router.
* @param serverRoutes - An array of server route definitions for server rendering.
* @param baseHref - An optional base href to be used in the HTML templates. Defaults to `'/'`.
* @param additionalServerAssets - An optional record of additional server assets to include,
* where the keys are asset paths and the values are asset details.
* @param locale - An optional locale to configure for the application during testing.
* @param rootComponent - An optional root Angular component to bootstrap the application. Defaults to `AppComponent`.
* @param extraProviders - An optional array of additional providers that should be available to the
* root component and all its children.
* @param inlineCriticalCss - An optional record of stylesheet paths to CSS content to be compiled into critical CSS plans.
* @param nonce - An optional Content Security Policy (CSP) nonce to be used for inlined critical CSS.
*/
export function setAngularAppTestingManifest(
routes: Routes,
serverRoutes: ServerRoute[],
baseHref = '/',
additionalServerAssets: Record<string, ServerAsset> = {},
locale?: string,
rootComponent: Type<unknown> = AppComponent,
extraProviders: Array<Provider | EnvironmentProviders> = [],
inlineCriticalCss?: Record<string, string>,
nonce?: string,
): void {
destroyAngularServerApp();
let criticalCssPlans: CompactPlan[] | undefined;
if (inlineCriticalCss) {
criticalCssPlans = Object.entries(inlineCriticalCss).map(([href, css]) => {
const sheet = compileSheet(css, { href });
return encodePlan(sheet);
});
}
setAngularAppManifest({
criticalCssPlans,
nonce,
baseHref,
locale,
assets: {
'index.server.html': {
size: 25,
hash: 'f799132d0a09e0fef93c68a12e443527700eb59e6f67fcb7854c3a60ff082fde',
text: async () => `<html>
<head>
<title>SSR page</title>
<base href="${baseHref}" />
</head>
<body>
<app-root></app-root>
</body>
</html>
`,
},
'index.csr.html': {
size: 25,
hash: 'f799132d0a09e0fef93c68a12e443527700eb59e6f67fcb7854c3a60ff082fde',
text: async () =>
`<html>
<head>
<title>CSR page</title>
<base href="${baseHref}" />
</head>
<body>
<app-root></app-root>
</body>
</html>
`,
},
...additionalServerAssets,
},
bootstrap: async () => (context) => {
return bootstrapApplication(
rootComponent,
{
providers: [
provideZonelessChangeDetection(),
provideRouter(routes),
provideServerRendering(withRoutes(serverRoutes)),
...extraProviders,
],
},
context,
);
},
});
}