Skip to content

Commit 369f03a

Browse files
committed
fix(core): unable to retrieve defer blocks in tests when component injects ViewContainerRef (#62156)
Fixes that `getDeferBlocks` wasn't accounting for the case where a component might be injecting `ViewContainerRef`. When that happens, an additional wrapper is introduced that needs to be accounted for when traversing the tree. Fixes #62047. PR Close #62156
1 parent 5a11a79 commit 369f03a

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎packages/core/src/defer/discovery.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {CONTAINER_HEADER_OFFSET} from '../render3/interfaces/container';
1010
import {TNode} from '../render3/interfaces/node';
1111
import {isLContainer, isLView} from '../render3/interfaces/type_checks';
12-
import {HEADER_OFFSET, LView, TVIEW} from '../render3/interfaces/view';
12+
import {HEADER_OFFSET, HOST, LView, TVIEW} from '../render3/interfaces/view';
1313

1414
import {DehydratedDeferBlock, TDeferBlockDetails} from './interfaces';
1515
import {getTDeferBlockDetails, isTDeferBlockDetails} from './utils';
@@ -46,8 +46,15 @@ export function getDeferBlocks(lView: LView, deferBlocks: DeferBlockDetails[]) {
4646
continue;
4747
}
4848
}
49-
for (let i = CONTAINER_HEADER_OFFSET; i < lContainer.length; i++) {
50-
getDeferBlocks(lContainer[i] as LView, deferBlocks);
49+
50+
// The host can be an `LView` if this is the container
51+
// for a component that injects `ViewContainerRef`.
52+
if (isLView(lContainer[HOST])) {
53+
getDeferBlocks(lContainer[HOST], deferBlocks);
54+
}
55+
56+
for (let j = CONTAINER_HEADER_OFFSET; j < lContainer.length; j++) {
57+
getDeferBlocks(lContainer[j] as LView, deferBlocks);
5158
}
5259
} else if (isLView(lView[i])) {
5360
// This is a component, enter the `getDeferBlocks` recursively.

‎packages/core/test/defer_fixture_spec.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ɵPLATFORM_BROWSER_ID as PLATFORM_BROWSER_ID} from '@angular/common';
10-
import {Component, PLATFORM_ID} from '../src/core';
10+
import {Component, inject, PLATFORM_ID, ViewContainerRef} from '../src/core';
1111
import {PendingTasksInternal} from '../src/pending_tasks';
1212
import {DeferBlockBehavior, DeferBlockState, TestBed} from '../testing';
1313
import {expect} from '@angular/private/testing/matchers';
@@ -428,4 +428,19 @@ describe('DeferFixture', () => {
428428
const fixtures = await deferBlock.getDeferBlocks();
429429
expect(fixtures.length).toBe(1);
430430
});
431+
432+
it('should resolve defer blocks in components that inject ViewContainerRef', async () => {
433+
@Component({template: '@defer {Hello}'})
434+
class DeferTestComponent {
435+
viewContainerRef = inject(ViewContainerRef);
436+
}
437+
438+
TestBed.configureTestingModule({deferBlockBehavior: DeferBlockBehavior.Manual});
439+
const fixture = TestBed.createComponent(DeferTestComponent);
440+
fixture.detectChanges();
441+
442+
const deferBlocks = await fixture.getDeferBlocks();
443+
expect(deferBlocks.length).toBe(1);
444+
expect(fixture.componentInstance.viewContainerRef).toBeTruthy();
445+
});
431446
});

0 commit comments

Comments
 (0)