|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { setTimeout } from 'node:timers/promises'; |
| 3 | +import { replaceInFile, writeMultipleFiles } from '../../utils/fs'; |
| 4 | +import { ng, silentNg, waitForAnyProcessOutputToMatch } from '../../utils/process'; |
| 5 | +import { installPackage, installWorkspacePackages, uninstallPackage } from '../../utils/packages'; |
| 6 | +import { ngServe, updateJsonFile, useSha } from '../../utils/project'; |
| 7 | +import { getGlobalVariable } from '../../utils/env'; |
| 8 | + |
| 9 | +export default async function () { |
| 10 | + assert( |
| 11 | + getGlobalVariable('argv')['esbuild'], |
| 12 | + 'This test should not be called in the Webpack suite.', |
| 13 | + ); |
| 14 | + |
| 15 | + // Forcibly remove in case another test doesn't clean itself up. |
| 16 | + await uninstallPackage('@angular/ssr'); |
| 17 | + await ng('add', '@angular/ssr', '--skip-confirmation', '--skip-install'); |
| 18 | + await useSha(); |
| 19 | + await installWorkspacePackages(); |
| 20 | + await installPackage('h3@1'); |
| 21 | + |
| 22 | + // Update angular.json |
| 23 | + await updateJsonFile('angular.json', (workspaceJson) => { |
| 24 | + const appArchitect = workspaceJson.projects['test-project'].architect; |
| 25 | + const options = appArchitect.build.options; |
| 26 | + options.outputMode = 'server'; |
| 27 | + }); |
| 28 | + |
| 29 | + await writeMultipleFiles({ |
| 30 | + // Replace the template of app.component.html as it makes it harder to debug |
| 31 | + 'src/app/app.component.html': '<router-outlet />', |
| 32 | + 'src/app/app.config.server.ts': ` |
| 33 | + import { mergeApplicationConfig, ApplicationConfig } from '@angular/core'; |
| 34 | + import { provideServerRendering } from '@angular/platform-server'; |
| 35 | + import { provideServerRoutesConfig } from '@angular/ssr'; |
| 36 | + import { routes } from './app.routes.server'; |
| 37 | + import { appConfig } from './app.config'; |
| 38 | +
|
| 39 | + const serverConfig: ApplicationConfig = { |
| 40 | + providers: [ |
| 41 | + provideServerRoutesConfig(routes), |
| 42 | + provideServerRendering() |
| 43 | + ] |
| 44 | + }; |
| 45 | +
|
| 46 | + export const config = mergeApplicationConfig(appConfig, serverConfig); |
| 47 | + `, |
| 48 | + 'src/app/app.routes.ts': ` |
| 49 | + import { Routes } from '@angular/router'; |
| 50 | + import { HomeComponent } from './home/home.component'; |
| 51 | +
|
| 52 | + export const routes: Routes = [ |
| 53 | + { path: 'home', component: HomeComponent } |
| 54 | + ]; |
| 55 | + `, |
| 56 | + 'src/app/app.routes.server.ts': ` |
| 57 | + import { RenderMode, ServerRoute } from '@angular/ssr'; |
| 58 | +
|
| 59 | + export const routes: ServerRoute[] = [ |
| 60 | + { path: '**', renderMode: RenderMode.Server } |
| 61 | + ]; |
| 62 | + `, |
| 63 | + 'server.ts': ` |
| 64 | + import { AngularAppEngine, createRequestHandler } from '@angular/ssr'; |
| 65 | + import { createApp, createRouter, toWebHandler, defineEventHandler, toWebRequest } from 'h3'; |
| 66 | +
|
| 67 | + export function app() { |
| 68 | + const server = createApp(); |
| 69 | + const router = createRouter(); |
| 70 | + const angularAppEngine = new AngularAppEngine(); |
| 71 | +
|
| 72 | + router.use( |
| 73 | + '/api/**', |
| 74 | + defineEventHandler(() => ({ hello: 'foo' })), |
| 75 | + ); |
| 76 | +
|
| 77 | + router.use( |
| 78 | + '/**', |
| 79 | + defineEventHandler((event) => angularAppEngine.render(toWebRequest(event))), |
| 80 | + ); |
| 81 | +
|
| 82 | + server.use(router); |
| 83 | +
|
| 84 | + return server; |
| 85 | + } |
| 86 | +
|
| 87 | + const server = app(); |
| 88 | + const handler = toWebHandler(server); |
| 89 | + export default createRequestHandler(handler); |
| 90 | + `, |
| 91 | + }); |
| 92 | + |
| 93 | + await silentNg('generate', 'component', 'home'); |
| 94 | + |
| 95 | + const port = await ngServe(); |
| 96 | + |
| 97 | + // Verify the server is running and the API response is correct. |
| 98 | + await validateResponse('/main.js', /bootstrapApplication/); |
| 99 | + await validateResponse('/api/test', /foo/); |
| 100 | + await validateResponse('/home', /home works/); |
| 101 | + |
| 102 | + // Modify the home component and validate the change. |
| 103 | + await modifyFileAndWaitUntilUpdated( |
| 104 | + 'src/app/home/home.component.html', |
| 105 | + 'home works', |
| 106 | + 'yay home works!!!', |
| 107 | + ); |
| 108 | + await validateResponse('/api/test', /foo/); |
| 109 | + await validateResponse('/home', /yay home works/); |
| 110 | + |
| 111 | + // Modify the API response and validate the change. |
| 112 | + await modifyFileAndWaitUntilUpdated('server.ts', `{ hello: 'foo' }`, `{ hello: 'bar' }`); |
| 113 | + await validateResponse('/api/test', /bar/); |
| 114 | + await validateResponse('/home', /yay home works/); |
| 115 | + |
| 116 | + async function validateResponse(pathname: string, match: RegExp): Promise<void> { |
| 117 | + const response = await fetch(new URL(pathname, `http://localhost:${port}`)); |
| 118 | + const text = await response.text(); |
| 119 | + assert.match(text, match); |
| 120 | + assert.equal(response.status, 200); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +async function modifyFileAndWaitUntilUpdated( |
| 125 | + filePath: string, |
| 126 | + searchValue: string, |
| 127 | + replaceValue: string, |
| 128 | +): Promise<void> { |
| 129 | + await Promise.all([ |
| 130 | + waitForAnyProcessOutputToMatch(/Page reload sent to client/), |
| 131 | + replaceInFile(filePath, searchValue, replaceValue), |
| 132 | + ]); |
| 133 | + |
| 134 | + await setTimeout(200); |
| 135 | +} |
0 commit comments