Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5652fb1

Browse files
pkozlowski-opensourceatscott
authored andcommitted
fix(core): allow null / undefined values in query results (#35796)
Before this change ngIvy implementation of queries would throw upon encountering null / undefined query result collected from an embedded view. It turns out that we might have a provider that explicitly provides a null / undefined value in a place of a token queried for. This commit removes a check from the ngIvy query implementation that was asserting on a query result to be defined. Fixes #35673 PR Close #35796
1 parent 44e47da commit 5652fb1

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

‎packages/core/src/render3/query.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,7 @@ function collectQueryResults<T>(tView: TView, lView: LView, queryIndex: number,
363363
for (let i = 0; i < tQueryMatches.length; i += 2) {
364364
const tNodeIdx = tQueryMatches[i];
365365
if (tNodeIdx > 0) {
366-
const viewResult = lViewResults[i / 2];
367-
ngDevMode && assertDefined(viewResult, 'materialized query result should be defined');
368-
result.push(viewResult as T);
366+
result.push(lViewResults[i / 2] as T);
369367
} else {
370368
const childQueryIndex = tQueryMatches[i + 1];
371369

‎packages/core/test/acceptance/query_spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,78 @@ describe('query logic', () => {
14251425
expect(fixture.componentInstance.queryResults.last).toBeAnInstanceOf(WithMultiProvider);
14261426
});
14271427

1428+
it('should allow undefined provider value in a [View/Content]Child queries', () => {
1429+
@Directive({selector: '[group]'})
1430+
class GroupDir {
1431+
}
1432+
1433+
@Directive(
1434+
{selector: '[undefinedGroup]', providers: [{provide: GroupDir, useValue: undefined}]})
1435+
class UndefinedGroup {
1436+
}
1437+
1438+
@Component({
1439+
template: `
1440+
<div group></div>
1441+
<ng-template [ngIf]="true">
1442+
<div undefinedGroup></div>
1443+
</ng-template>
1444+
`
1445+
})
1446+
class App {
1447+
@ViewChild(GroupDir) group !: GroupDir;
1448+
}
1449+
1450+
TestBed.configureTestingModule(
1451+
{declarations: [App, GroupDir, UndefinedGroup], imports: [CommonModule]});
1452+
const fixture = TestBed.createComponent(App);
1453+
fixture.detectChanges();
1454+
1455+
expect(fixture.componentInstance.group).toBeAnInstanceOf(GroupDir);
1456+
});
1457+
1458+
it('should allow null / undefined provider value in a [View/Content]Children queries', () => {
1459+
@Directive({selector: '[group]'})
1460+
class GroupDir {
1461+
}
1462+
1463+
@Directive({selector: '[nullGroup]', providers: [{provide: GroupDir, useValue: null}]})
1464+
class NullGroup {
1465+
}
1466+
1467+
@Directive(
1468+
{selector: '[undefinedGroup]', providers: [{provide: GroupDir, useValue: undefined}]})
1469+
class UndefinedGroup {
1470+
}
1471+
1472+
@Component({
1473+
template: `
1474+
<ng-template [ngIf]="true">
1475+
<div nullGroup></div>
1476+
</ng-template>
1477+
<div group></div>
1478+
<ng-template [ngIf]="true">
1479+
<div undefinedGroup></div>
1480+
</ng-template>
1481+
`
1482+
})
1483+
class App {
1484+
@ViewChildren(GroupDir) groups !: QueryList<GroupDir>;
1485+
}
1486+
1487+
TestBed.configureTestingModule(
1488+
{declarations: [App, GroupDir, NullGroup, UndefinedGroup], imports: [CommonModule]});
1489+
const fixture = TestBed.createComponent(App);
1490+
fixture.detectChanges();
1491+
1492+
const queryList = fixture.componentInstance.groups;
1493+
expect(queryList.length).toBe(3);
1494+
1495+
const groups = queryList.toArray();
1496+
expect(groups[0]).toBeNull();
1497+
expect(groups[1]).toBeAnInstanceOf(GroupDir);
1498+
expect(groups[2]).toBeUndefined();
1499+
});
14281500

14291501
});
14301502

0 commit comments

Comments
 (0)