Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/core/test/acceptance/standalone_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,5 +910,39 @@ describe('standalone components, directives, and pipes', () => {

expect(isStandalone(Module)).toBeFalse();
});

it('should render a recursive cycle of standalone components', () => {
@Component({
selector: 'cmp-a',
standalone: true,
template: '<ng-template [ngIf]="false"><cmp-c></cmp-c></ng-template>A',
imports: [forwardRef(() => StandaloneCmpC)],
})
class StandaloneCmpA {
}

@Component({
selector: 'cmp-b',
standalone: true,
template: '(<cmp-a></cmp-a>)B',
imports: [StandaloneCmpA],
})
class StandaloneCmpB {
}

@Component({
selector: 'cmp-c',
standalone: true,
template: '(<cmp-b></cmp-b>)C',
imports: [StandaloneCmpB],
})
class StandaloneCmpC {
}

TestBed.configureTestingModule({imports: [StandaloneCmpC]});
const fixture = TestBed.createComponent(StandaloneCmpC);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('((A)B)C');
});
});
});
19 changes: 13 additions & 6 deletions packages/core/testing/src/test_bed_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,20 +580,21 @@ export class TestBedCompiler {
}

private queueTypesFromModulesArray(arr: any[]): void {
// Because we may encounter the same NgModule while processing the imports and exports of an
// NgModule tree, we cache them in this set so we can skip ones that have already been seen
// encountered. In some test setups, this caching resulted in 10X runtime improvement.
const processedNgModuleDefs = new Set();
// Because we may encounter the same NgModule or a standalone Component while processing
// the dependencies of an NgModule or a standalone Component, we cache them in this set so we
// can skip ones that have already been seen encountered. In some test setups, this caching
// resulted in 10X runtime improvement.
const processedDefs = new Set();
const queueTypesFromModulesArrayRecur = (arr: any[]): void => {
for (const value of arr) {
if (Array.isArray(value)) {
queueTypesFromModulesArrayRecur(value);
} else if (hasNgModuleDef(value)) {
const def = value.ɵmod;
if (processedNgModuleDefs.has(def)) {
if (processedDefs.has(def)) {
continue;
}
processedNgModuleDefs.add(def);
processedDefs.add(def);
// Look through declarations, imports, and exports, and queue
// everything found there.
this.queueTypeArray(maybeUnwrapFn(def.declarations), value);
Expand All @@ -604,6 +605,12 @@ export class TestBedCompiler {
} else if (isStandaloneComponent(value)) {
this.queueType(value, null);
const def = getComponentDef(value);

if (processedDefs.has(def)) {
continue;
}
processedDefs.add(def);

const dependencies = maybeUnwrapFn(def.dependencies ?? []);
dependencies.forEach((dependency) => {
// Note: in AOT, the `dependencies` might also contain regular
Expand Down