Doug Parker 160dee33d7 fix(@schematics/angular): don't show server routing prompt when using browser builder
The new routing APIs don't support `browser` builder, but calling `ng add @angular/ssr` with a `browser` builder would still prompt the user to add them. If the user said "Yes", it would actually ignore that answer and not enable the new APIs.

With this change, `ng add @angular/ssr` when using `browser` builder does not show the prompt and assumes the answer is "No". It also throws an error if the user runs `ng add @angular/ssr --server-routing`.

I'm not aware of a built-in prompting mechanism in schematics beyond `x-prompt`, which can't be used here, so instead I just called Inquirer directly. Unfortunately testing the prompt is a little awkward, as Inquirier does not provide useful APIs in this space. I evaluated `@inquirer/testing`, but ultimately decided that was more intended for testing custom Inquirer prompts, not mocking usage of standard prompts. Schematics APIs do not provide a useful way to inject additional data like a mock, so instead I had to do this through a `setPrompterForTestOnly` function. I'm not a huge fan of it, but I don't see a more straightforward way of solving the problem.
2024-11-18 10:37:31 -08:00

63 lines
1.5 KiB
TypeScript

/**
* @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
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { join } from 'node:path';
describe('@angular/ssr ng-add schematic', () => {
const defaultOptions = {
project: 'test-app',
serverRouting: false,
};
const schematicRunner = new SchematicTestRunner(
'@angular/ssr',
require.resolve(join(__dirname, '../collection.json')),
);
let appTree: UnitTestTree;
const workspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
beforeEach(async () => {
appTree = await schematicRunner.runExternalSchematic(
'@schematics/angular',
'workspace',
workspaceOptions,
);
appTree = await schematicRunner.runExternalSchematic(
'@schematics/angular',
'application',
{
name: 'test-app',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: 'css',
skipTests: false,
standalone: true,
},
appTree,
);
});
it('works', async () => {
const filePath = '/projects/test-app/src/server.ts';
expect(appTree.exists(filePath)).toBeFalse();
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
expect(tree.exists(filePath)).toBeTrue();
});
});