Skip to content

Commit 9dab578

Browse files
mistrykaran91dgp1130
authored andcommitted
fix(@schematics/angular): add fixture.whenStable in spec files when zoneless apps
1 parent b8892b7 commit 9dab578

File tree

7 files changed

+83
-5
lines changed

7 files changed

+83
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ describe('App', () => {
2020
expect(app).toBeTruthy();
2121
});
2222

23-
it('should render title', () => {
23+
it('should render title', <% if(zoneless) { %> async <% } %>() => {
2424
const fixture = TestBed.createComponent(App);
25-
fixture.detectChanges();
25+
<%= zoneless ? 'await fixture.whenStable();' : 'fixture.detectChanges();' %>
2626
const compiled = fixture.nativeElement as HTMLElement;
2727
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, <%= name %>');
2828
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('App', () => {
1414
expect(app).toBeTruthy();
1515
});
1616

17-
it('should render title', () => {
17+
it('should render title', <% if(zoneless) { %> async <% } %>() => {
1818
const fixture = TestBed.createComponent(App);
19-
fixture.detectChanges();
19+
<%= zoneless ? 'await fixture.whenStable();' : 'fixture.detectChanges();' %>
2020
const compiled = fixture.nativeElement as HTMLElement;
2121
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, <%= name %>');
2222
});

packages/schematics/angular/application/index_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,35 @@ describe('Application Schematic', () => {
831831
});
832832
});
833833

834+
it('should add fixture.whenStable() when zoneless application', async () => {
835+
const tree = await schematicRunner.runSchematic(
836+
'application',
837+
{
838+
...defaultOptions,
839+
},
840+
workspaceTree,
841+
);
842+
843+
const content = tree.readContent('/projects/foo/src/app/app.spec.ts');
844+
expect(content).toContain('fixture.whenStable()');
845+
expect(content).not.toContain('fixture.detectChanges()');
846+
});
847+
848+
it('should not add fixture.whenStable() in initial spec files when its not zoneless application', async () => {
849+
const tree = await schematicRunner.runSchematic(
850+
'application',
851+
{
852+
...defaultOptions,
853+
zoneless: false,
854+
},
855+
workspaceTree,
856+
);
857+
858+
const content = tree.readContent('/projects/foo/src/app/app.spec.ts');
859+
expect(content).not.toContain('fixture.whenStable()');
860+
expect(content).toContain('fixture.detectChanges()');
861+
});
862+
834863
it('should call the tailwind schematic when style is tailwind', async () => {
835864
// eslint-disable-next-line @typescript-eslint/no-explicit-any
836865
const options = { ...defaultOptions, style: 'tailwind' as any };

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('<%= classifiedName %>', () => {
1414

1515
fixture = TestBed.createComponent(<%= classifiedName %>);
1616
component = fixture.componentInstance;
17-
fixture.detectChanges();
17+
<%= zoneless ? 'await fixture.whenStable();' : 'fixture.detectChanges();' %>
1818
});
1919

2020
it('should create', () => {

packages/schematics/angular/component/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { addDeclarationToNgModule } from '../utility/add-declaration-to-ng-modul
2424
import { findModuleFromOptions } from '../utility/find-module';
2525
import { parseName } from '../utility/parse-name';
2626
import { createProjectSchematic } from '../utility/project';
27+
import { isZonelessApp } from '../utility/project-targets';
2728
import { validateClassName, validateHtmlSelector } from '../utility/validation';
2829
import { buildDefaultPath } from '../utility/workspace';
2930
import { Schema as ComponentOptions, Style } from './schema';
@@ -59,6 +60,7 @@ export default createProjectSchematic<ComponentOptions>((options, { project, tre
5960
strings.classify(options.name) +
6061
(options.addTypeToClassName && options.type ? strings.classify(options.type) : '');
6162
validateClassName(classifiedName);
63+
const zoneless = isZonelessApp(project);
6264

6365
const skipStyleFile = options.inlineStyle || options.style === Style.None;
6466
const templateSource = apply(url('./files'), [
@@ -72,6 +74,7 @@ export default createProjectSchematic<ComponentOptions>((options, { project, tre
7274
...options,
7375
// Add a new variable for the classified name, conditionally including the type
7476
classifiedName,
77+
zoneless,
7578
}),
7679
!options.type
7780
? forEach(((file) => {

packages/schematics/angular/component/index_spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,38 @@ describe('Component Schematic', () => {
554554
const specContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
555555
expect(specContent).toContain("import { FooComponent } from './foo.component';");
556556
});
557+
558+
it('should add fixture.whenStable() in spec file when zoneless and standalone apps', async () => {
559+
const tree = await schematicRunner.runSchematic('component', { ...defaultOptions }, appTree);
560+
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
561+
562+
expect(tsContent).toContain('fixture.whenStable()');
563+
expect(tsContent).not.toContain('fixture.detectChanges()');
564+
});
565+
566+
describe('with zone.js application', () => {
567+
const zoneAppOptions: ApplicationOptions = {
568+
...appOptions,
569+
name: 'baz',
570+
zoneless: false,
571+
};
572+
573+
it('should add not fixture.whenStable() in spec file for standalone', async () => {
574+
appTree = await schematicRunner.runSchematic(
575+
'application',
576+
{ ...zoneAppOptions, standalone: true },
577+
appTree,
578+
);
579+
const tree = await schematicRunner.runSchematic(
580+
'component',
581+
{ ...defaultOptions, standalone: true, project: 'baz' },
582+
appTree,
583+
);
584+
585+
const tsContent = tree.readContent('/projects/baz/src/app/foo/foo.component.spec.ts');
586+
587+
expect(tsContent).not.toContain('fixture.whenStable()');
588+
expect(tsContent).toContain('fixture.detectChanges()');
589+
});
590+
});
557591
});

packages/schematics/angular/utility/project-targets.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ export function isUsingApplicationBuilder(project: ProjectDefinition): boolean {
2121

2222
return isUsingApplicationBuilder;
2323
}
24+
25+
export function isZonelessApp(project: ProjectDefinition): boolean {
26+
const buildTarget = project.targets.get('build');
27+
if (!buildTarget?.options?.polyfills) {
28+
return true;
29+
}
30+
31+
const polyfills = buildTarget.options.polyfills as string[] | string;
32+
const polyfillsList = Array.isArray(polyfills) ? polyfills : [polyfills];
33+
34+
return !polyfillsList.includes('zone.js');
35+
}

0 commit comments

Comments
 (0)