Skip to content

Commit 69824ae

Browse files
pawelfrasdgp1130
authored andcommitted
fix(@schematics/angular): issues in apps generated with '--file-name-style-guide=2016' flag
- fixes references to non-existing `app.ts` file, causing build and test issues - changes file names from `app-module.ts` and `app-routing-module.ts` to `app.module.ts` and `app-routing.module.ts`, for consistency. Closes: #31830
1 parent 102ca8b commit 69824ae

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed

packages/schematics/angular/application/files/module-files/src/app/app__suffix__.spec.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TestBed } from '@angular/core/testing';<% if (routing) { %>
22
import { RouterModule } from '@angular/router';<% } %>
3-
import { App } from './app';
3+
import { App } from './app<%= suffix %>';
44

55
describe('App', () => {
66
beforeEach(async () => {

packages/schematics/angular/application/files/module-files/src/app/app__suffix__.ts.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { Component, signal } from '@angular/core';
99
%><router-outlet /><%
1010
} %>
1111
`,<% } else { %>
12-
templateUrl: './app.html',<% } %>
12+
templateUrl: './app<%= suffix %>.html',<% } %>
1313
standalone: false,<% if(inlineStyle) { %>
1414
styles: []<% } else { %>
15-
styleUrl: './app.<%= style %>'<% } %>
15+
styleUrl: './app<%= suffix %>.<%= style %>'<% } %>
1616
})
1717
export class App {
1818
protected readonly title = signal('<%= name %>');

packages/schematics/angular/application/files/module-files/src/app/app-module.ts.template renamed to packages/schematics/angular/application/files/module-files/src/app/app__typeSeparator__module.ts.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { NgModule, provideBrowserGlobalErrorListeners } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
33
<% if (routing) { %>
4-
import { AppRoutingModule } from './app-routing-module';<% } %>
5-
import { App } from './app';
4+
import { AppRoutingModule } from './app-routing<%= typeSeparator %>module';<% } %>
5+
import { App } from './app<%= suffix %>';
66

77
@NgModule({
88
declarations: [

packages/schematics/angular/application/files/standalone-files/src/app/app__suffix__.spec.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TestBed } from '@angular/core/testing';
2-
import { App } from './app';
2+
import { App } from './app<%= suffix %>';
33

44
describe('App', () => {
55
beforeEach(async () => {

packages/schematics/angular/application/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default function (options: ApplicationOptions): Rule {
6868
await getAppOptions(host, options);
6969

7070
const suffix = options.fileNameStyleGuide === '2016' ? '.component' : '';
71+
const typeSeparator = options.fileNameStyleGuide === '2016' ? '.' : '-';
7172

7273
return chain([
7374
addAppToWorkspaceFile(options, appDir),
@@ -85,7 +86,7 @@ export default function (options: ApplicationOptions): Rule {
8586
routingScope: 'Root',
8687
path: sourceDir,
8788
project: options.name,
88-
typeSeparator: undefined,
89+
typeSeparator,
8990
}),
9091
schematic('component', {
9192
name: 'app',
@@ -111,6 +112,7 @@ export default function (options: ApplicationOptions): Rule {
111112
appName: options.name,
112113
folderName,
113114
suffix,
115+
typeSeparator,
114116
}),
115117
move(appDir),
116118
]),

packages/schematics/angular/application/index_spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,5 +879,62 @@ describe('Application Schematic', () => {
879879
expect(component).toContain(`templateUrl: './app.component.html'`);
880880
expect(component).toContain(`styleUrl: './app.component.css'`);
881881
});
882+
883+
it('should create a test file with import from the path without suffix', async () => {
884+
const options = { ...defaultOptions, fileNameStyleGuide: '2016' as const };
885+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
886+
const componentSpec = tree.readContent('/projects/foo/src/app/app.component.spec.ts');
887+
expect(componentSpec).toContain(`import { App } from './app.component'`);
888+
});
889+
890+
describe('standalone: false', () => {
891+
it('should create a component with the correct template and style urls', async () => {
892+
const options = {
893+
...defaultOptions,
894+
standalone: false,
895+
fileNameStyleGuide: '2016' as const,
896+
};
897+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
898+
const component = tree.readContent('/projects/foo/src/app/app.component.ts');
899+
expect(component).toContain(`templateUrl: './app.component.html'`);
900+
expect(component).toContain(`styleUrl: './app.component.css'`);
901+
});
902+
903+
it('should create a test file with import from the path without suffix', async () => {
904+
const options = {
905+
...defaultOptions,
906+
standalone: false,
907+
fileNameStyleGuide: '2016' as const,
908+
};
909+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
910+
const componentSpec = tree.readContent('/projects/foo/src/app/app.component.spec.ts');
911+
expect(componentSpec).toContain(`import { App } from './app.component'`);
912+
});
913+
914+
it('should create a module with the correct suffix', async () => {
915+
const options = {
916+
...defaultOptions,
917+
standalone: false,
918+
fileNameStyleGuide: '2016' as const,
919+
};
920+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
921+
const module = tree.readContent('/projects/foo/src/app/app.module.ts');
922+
expect(module).toContain(`import { App } from './app.component'`);
923+
});
924+
925+
it('should create a routing module with the correct suffix', async () => {
926+
const options = {
927+
...defaultOptions,
928+
standalone: false,
929+
routing: true,
930+
fileNameStyleGuide: '2016' as const,
931+
};
932+
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
933+
const module = tree.readContent('/projects/foo/src/app/app.module.ts');
934+
const routingModule = tree.readContent('/projects/foo/src/app/app-routing.module.ts');
935+
expect(routingModule).toBeDefined();
936+
expect(module).toContain(`import { AppRoutingModule } from './app-routing.module'`);
937+
});
938+
});
882939
});
883940
});

0 commit comments

Comments
 (0)