Skip to content

Commit 31a1575

Browse files
committed
fix(core): handle local refs when getDeferBlocks is invoked in tests (#52973)
This commit fixes an issue where having elements with local refs in some cases causes JS exception. PR Close #52973
1 parent baeb671 commit 31a1575

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/core/src/defer/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,6 @@ export function assertDeferredDependenciesLoaded(tDetails: TDeferBlockDetails) {
141141
* that a primary template exists. All the other template options are optional.
142142
*/
143143
export function isTDeferBlockDetails(value: unknown): value is TDeferBlockDetails {
144-
return (typeof value === 'object') &&
144+
return value !== null && (typeof value === 'object') &&
145145
(typeof (value as TDeferBlockDetails).primaryTmplIndex === 'number');
146146
}

packages/core/test/defer_fixture_spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,39 @@ describe('DeferFixture', () => {
190190
expect(el.querySelector('.more')).toBeDefined();
191191
});
192192

193+
it('should work with templates that have local refs', async () => {
194+
@Component({
195+
selector: 'defer-comp',
196+
standalone: true,
197+
imports: [SecondDeferredComp],
198+
template: `
199+
<ng-template #template>Hello</ng-template>
200+
<div>
201+
@defer (on immediate) {
202+
<second-deferred-comp />
203+
}
204+
</div>
205+
`
206+
})
207+
class DeferComp {
208+
}
209+
210+
TestBed.configureTestingModule({
211+
imports: [
212+
DeferComp,
213+
SecondDeferredComp,
214+
],
215+
providers: COMMON_PROVIDERS,
216+
deferBlockBehavior: DeferBlockBehavior.Manual,
217+
});
218+
219+
const componentFixture = TestBed.createComponent(DeferComp);
220+
const deferBlock = (await componentFixture.getDeferBlocks())[0];
221+
const el = componentFixture.nativeElement as HTMLElement;
222+
await deferBlock.render(DeferBlockState.Complete);
223+
expect(el.querySelector('.more')).toBeDefined();
224+
});
225+
193226
it('should render a placeholder defer state', async () => {
194227
@Component({
195228
selector: 'defer-comp',

0 commit comments

Comments
 (0)