|
8 | 8 |
|
9 | 9 | import {SelectorFlags} from '@angular/core/src/render3/interfaces/projection';
|
10 | 10 |
|
11 |
| -import {AttributeMarker, defineDirective, detectChanges, directiveInject, loadViewQuery, queryRefresh, reference, templateRefExtractor, viewQuery} from '../../src/render3/index'; |
| 11 | +import {AttributeMarker, defineComponent, defineDirective, detectChanges, directiveInject, loadViewQuery, queryRefresh, reference, templateRefExtractor, viewQuery} from '../../src/render3/index'; |
12 | 12 |
|
13 | 13 | import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementContainerEnd, elementContainerStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, projection, projectionDef, template, text, textBinding, interpolation1} from '../../src/render3/instructions';
|
14 | 14 | import {RenderFlags} from '../../src/render3/interfaces/definition';
|
@@ -1441,6 +1441,86 @@ describe('content projection', () => {
|
1441 | 1441 | expect(toHtml(parent)).toEqual('<child><grand-child>content</grand-child></child>');
|
1442 | 1442 | });
|
1443 | 1443 |
|
| 1444 | + it('should handle projected containers inside other containers', () => { |
| 1445 | + // <div>Child content</div> |
| 1446 | + const NestedComp = createComponent('nested-comp', function(rf: RenderFlags, ctx: any) { |
| 1447 | + if (rf & RenderFlags.Create) { |
| 1448 | + elementStart(0, 'div'); |
| 1449 | + text(1, 'Child content'); |
| 1450 | + elementEnd(); |
| 1451 | + } |
| 1452 | + }, 2, 0, []); |
| 1453 | + |
| 1454 | + // <ng-content></ng-content> |
| 1455 | + const RootComp = createComponent('root-comp', function(rf: RenderFlags, ctx: any) { |
| 1456 | + if (rf & RenderFlags.Create) { |
| 1457 | + projectionDef(); |
| 1458 | + projection(0); |
| 1459 | + } |
| 1460 | + }, 1, 0, []); |
| 1461 | + |
| 1462 | + // <root-comp> |
| 1463 | + // <ng-container *ngFor="let item of items; last as last"> |
| 1464 | + // <child-comp *ngIf="!last"></child-comp> |
| 1465 | + // </ng-container> |
| 1466 | + // </root-comp> |
| 1467 | + function MyApp_ng_container_1_child_comp_1_Template(rf: RenderFlags, ctx: any) { |
| 1468 | + if (rf & RenderFlags.Create) { |
| 1469 | + element(0, 'nested-comp'); |
| 1470 | + } |
| 1471 | + } |
| 1472 | + function MyApp_ng_container_1_Template(rf: RenderFlags, ctx: any) { |
| 1473 | + if (rf & RenderFlags.Create) { |
| 1474 | + elementContainerStart(0); |
| 1475 | + template(1, MyApp_ng_container_1_child_comp_1_Template, 1, 0, 'nested-comp', [3, 'ngIf']); |
| 1476 | + elementContainerEnd(); |
| 1477 | + } |
| 1478 | + if (rf & RenderFlags.Update) { |
| 1479 | + const last_r2 = ctx.last; |
| 1480 | + elementProperty(1, 'ngIf', bind(!last_r2)); |
| 1481 | + } |
| 1482 | + } |
| 1483 | + let myAppInstance: MyApp; |
| 1484 | + class MyApp { |
| 1485 | + items = [1, 2]; |
| 1486 | + |
| 1487 | + static ngComponentDef = defineComponent({ |
| 1488 | + type: MyApp, |
| 1489 | + selectors: [['', 'my-app', '']], |
| 1490 | + factory: () => myAppInstance = new MyApp(), |
| 1491 | + consts: 2, |
| 1492 | + vars: 1, |
| 1493 | + template: function MyApp_Template(rf: RenderFlags, ctx: any) { |
| 1494 | + if (rf & RenderFlags.Create) { |
| 1495 | + elementStart(0, 'root-comp'); |
| 1496 | + template( |
| 1497 | + 1, MyApp_ng_container_1_Template, 2, 1, 'ng-container', |
| 1498 | + ['ngFor', '', 3, 'ngForOf']); |
| 1499 | + elementEnd(); |
| 1500 | + } |
| 1501 | + if (rf & RenderFlags.Update) { |
| 1502 | + elementProperty(1, 'ngForOf', bind(ctx.items)); |
| 1503 | + } |
| 1504 | + }, |
| 1505 | + directives: [NgForOf, NgIf, NestedComp, RootComp] |
| 1506 | + }); |
| 1507 | + } |
| 1508 | + const fixture = new ComponentFixture(MyApp); |
| 1509 | + fixture.update(); |
| 1510 | + |
| 1511 | + // expecting # of divs to be (items.length - 1), since last element is filtered out by *ngIf, |
| 1512 | + // this applies to all other assertions below |
| 1513 | + expect(fixture.hostElement.querySelectorAll('div').length).toBe(1); |
| 1514 | + |
| 1515 | + myAppInstance !.items = [3, 4, 5]; |
| 1516 | + fixture.update(); |
| 1517 | + expect(fixture.hostElement.querySelectorAll('div').length).toBe(2); |
| 1518 | + |
| 1519 | + myAppInstance !.items = [6, 7, 8, 9]; |
| 1520 | + fixture.update(); |
| 1521 | + expect(fixture.hostElement.querySelectorAll('div').length).toBe(3); |
| 1522 | + }); |
| 1523 | + |
1444 | 1524 | describe('with selectors', () => {
|
1445 | 1525 |
|
1446 | 1526 | it('should project nodes using attribute selectors', () => {
|
|
0 commit comments