Skip to content

Commit

Permalink
fix(core): handle local refs when getDeferBlocks is invoked in tests (
Browse files Browse the repository at this point in the history
#52973)

This commit fixes an issue where having elements with local refs in some cases causes JS exception.

PR Close #52973
  • Loading branch information
AndrewKushnir committed Nov 16, 2023
1 parent 81e080e commit 1ce31d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/defer/utils.ts
Expand Up @@ -141,6 +141,6 @@ export function assertDeferredDependenciesLoaded(tDetails: TDeferBlockDetails) {
* that a primary template exists. All the other template options are optional.
*/
export function isTDeferBlockDetails(value: unknown): value is TDeferBlockDetails {
return (typeof value === 'object') &&
return value !== null && (typeof value === 'object') &&
(typeof (value as TDeferBlockDetails).primaryTmplIndex === 'number');
}
33 changes: 33 additions & 0 deletions packages/core/test/defer_fixture_spec.ts
Expand Up @@ -190,6 +190,39 @@ describe('DeferFixture', () => {
expect(el.querySelector('.more')).toBeDefined();
});

it('should work with templates that have local refs', async () => {
@Component({
selector: 'defer-comp',
standalone: true,
imports: [SecondDeferredComp],
template: `
<ng-template #template>Hello</ng-template>
<div>
@defer (on immediate) {
<second-deferred-comp />
}
</div>
`
})
class DeferComp {
}

TestBed.configureTestingModule({
imports: [
DeferComp,
SecondDeferredComp,
],
providers: COMMON_PROVIDERS,
deferBlockBehavior: DeferBlockBehavior.Manual,
});

const componentFixture = TestBed.createComponent(DeferComp);
const deferBlock = (await componentFixture.getDeferBlocks())[0];
const el = componentFixture.nativeElement as HTMLElement;
await deferBlock.render(DeferBlockState.Complete);
expect(el.querySelector('.more')).toBeDefined();
});

it('should render a placeholder defer state', async () => {
@Component({
selector: 'defer-comp',
Expand Down

0 comments on commit 1ce31d8

Please sign in to comment.