Skip to content

Commit

Permalink
fixup! refactor(core): update TestBed to handle async component metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKushnir committed Aug 14, 2023
1 parent 3188a0d commit 617d681
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions packages/core/testing/src/test_bed_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ export class TestBedCompiler {
private testModuleType: NgModuleType<any>;
private testModuleRef: NgModuleRef<any>|null = null;

// List of Promises that represent async process to set Component metadata,
// in case those components use `{#defer}` blocks inside.
// private componentsWithAsyncMetadata: Array<Promise<Array<Type<unknown>>>> = [];
private componentsWithAsyncMetadata = new Set<Type<unknown>>();

constructor(private platform: PlatformRef, private additionalModuleTypes: Type<any>|Type<any>[]) {
class DynamicTestModule {}
this.testModuleType = DynamicTestModule as any;
Expand Down Expand Up @@ -261,35 +256,35 @@ export class TestBedCompiler {
this.componentToModuleScope.set(type, TestingModuleOverride.OVERRIDE_TEMPLATE);
}

private async resolveComponentsWithAsyncMetadata() {
if (this.componentsWithAsyncMetadata.size === 0) return;
private async resolvePendingComponentsWithAsyncMetadata() {
if (this.pendingComponents.size === 0) return;

const promises = [];
for (const component of this.componentsWithAsyncMetadata) {
for (const component of this.pendingComponents) {
const asyncMetadataPromise = getAsyncClassMetadata(component);
if (asyncMetadataPromise) {
promises.push(asyncMetadataPromise);
}
}
this.componentsWithAsyncMetadata.clear();

const resolvedDeps = await Promise.all(promises);
this.queueTypesFromModulesArray(resolvedDeps.flat(2));

// If we've got more types with async metadata queued,
// recursively process such components again.
if (this.componentsWithAsyncMetadata.size > 0) {
await this.resolveComponentsWithAsyncMetadata();
}
}

async compileComponents(): Promise<void> {
this.clearComponentResolutionQueue();

// Wait for all async metadata for all components.
if (this.componentsWithAsyncMetadata.size > 0) {
await this.resolveComponentsWithAsyncMetadata();
}
// Wait for all async metadata for components that were
// overridden, we need resolved metadata to perform an override
// and re-compile a component.
await this.resolvePendingComponentsWithAsyncMetadata();

// Verify that there were no standalone components present in the `declarations` field
// during the `TestBed.configureTestingModule` call. We perform this check here in addition
// to the logic in the `configureTestingModule` function, since at this point we have
// all async metadata resolved.
assertNoStandaloneComponents(
this.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');

// Run compilers for all queued types.
let needsAsyncResources = this.compileTypesSync();
Expand Down Expand Up @@ -641,8 +636,6 @@ export class TestBedCompiler {
for (const value of arr) {
if (Array.isArray(value)) {
queueTypesFromModulesArrayRecur(value);
} else if (getAsyncClassMetadata(value)) {
this.componentsWithAsyncMetadata.add(value);
} else if (hasNgModuleDef(value)) {
const def = value.ɵmod;
if (processedDefs.has(def)) {
Expand Down

0 comments on commit 617d681

Please sign in to comment.