test(compiler): add correct use case of ngFor in r3 ast (#35671)

The only test case for `ngFor` exercises an incorrect usage which causes
two bound attributes to be generated . This commit adds a canonical and
correct usage to show the difference between the two.

PR Close #35671
This commit is contained in:
Keen Yee Liau 2020-02-25 12:23:03 -08:00 committed by Miško Hevery
parent f9d483e76e
commit 24f32e373d
2 changed files with 36 additions and 0 deletions

View File

@ -225,6 +225,24 @@ describe('R3 AST source spans', () => {
// TODO(joost): improve spans of nodes extracted from macrosyntax
describe('inline templates', () => {
it('is correct for attribute and bound attributes', () => {
// Desugared form is
// <ng-template ngFor [ngForOf]="items" let-item>
// <div></div>
// </ng-template>
expectFromHtml('<div *ngFor="let item of items"></div>').toEqual([
['Template', '0:32', '0:32', '32:38'],
['TextAttribute', '5:31', '<empty>'],
['BoundAttribute', '5:31', '<empty>'],
['Variable', '5:31', '<empty>'],
['Element', '0:38', '0:32', '32:38'],
]);
// Note that this test exercises an *incorrect* usage of the ngFor
// directive. There is a missing 'let' in the beginning of the expression
// which causes the template to be desugared into
// <ng-template [ngFor]="item" [ngForOf]="items">
// <div></div>
// </ng-template>
expectFromHtml('<div *ngFor="item of items"></div>').toEqual([
['Template', '0:28', '0:28', '28:34'],
['BoundAttribute', '5:27', '<empty>'],

View File

@ -290,6 +290,24 @@ describe('R3 template transform', () => {
describe('inline templates', () => {
it('should support attribute and bound attributes', () => {
// Desugared form is
// <ng-template ngFor [ngForOf]="items" let-item>
// <div></div>
// </ng-template>
expectFromHtml('<div *ngFor="let item of items"></div>').toEqual([
['Template'],
['TextAttribute', 'ngFor', ''],
['BoundAttribute', BindingType.Property, 'ngForOf', 'items'],
['Variable', 'item', '$implicit'],
['Element', 'div'],
]);
// Note that this test exercises an *incorrect* usage of the ngFor
// directive. There is a missing 'let' in the beginning of the expression
// which causes the template to be desugared into
// <ng-template [ngFor]="item" [ngForOf]="items">
// <div></div>
// </ng-template>
expectFromHtml('<div *ngFor="item of items"></div>').toEqual([
['Template'],
['BoundAttribute', BindingType.Property, 'ngFor', 'item'],