|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import {CommonModule, DOCUMENT, ɵgetDOM as getDOM} from '@angular/common';
|
10 |
| -import {Compiler, ComponentFactory, ComponentRef, ErrorHandler, EventEmitter, Host, Inject, Injectable, InjectionToken, Injector, NgModule, NgModuleRef, NO_ERRORS_SCHEMA, OnDestroy, SkipSelf, ViewRef, ɵivyEnabled as ivyEnabled} from '@angular/core'; |
| 10 | +import {Compiler, ComponentFactory, ComponentRef, ErrorHandler, EventEmitter, Host, Inject, Injectable, InjectionToken, Injector, NgModule, NgModuleRef, NO_ERRORS_SCHEMA, OnDestroy, SkipSelf, ViewChild, ViewRef, ɵivyEnabled as ivyEnabled} from '@angular/core'; |
11 | 11 | import {ChangeDetectionStrategy, ChangeDetectorRef, PipeTransform} from '@angular/core/src/change_detection/change_detection';
|
12 | 12 | import {getDebugContext} from '@angular/core/src/errors';
|
13 | 13 | import {ComponentFactoryResolver} from '@angular/core/src/linker/component_factory_resolver';
|
@@ -1641,6 +1641,159 @@ function declareTests(config?: {useJit: boolean}) {
|
1641 | 1641 | expect(fixture.nativeElement).toHaveText('');
|
1642 | 1642 | });
|
1643 | 1643 |
|
| 1644 | + |
| 1645 | + describe('moving embedded views of projectable nodes in a dynamic component', () => { |
| 1646 | + @Component({selector: 'menu-item', template: ''}) |
| 1647 | + class DynamicMenuItem { |
| 1648 | + @ViewChild('templateRef', {static: true}) templateRef!: TemplateRef<any>; |
| 1649 | + itemContent!: string; |
| 1650 | + } |
| 1651 | + |
| 1652 | + @NgModule({ |
| 1653 | + declarations: [DynamicMenuItem], |
| 1654 | + entryComponents: [DynamicMenuItem], |
| 1655 | + }) |
| 1656 | + class DynamicMenuItemModule { |
| 1657 | + } |
| 1658 | + |
| 1659 | + @Component({selector: 'test', template: `<ng-container #menuItemsContainer></ng-container>`}) |
| 1660 | + class TestCmp { |
| 1661 | + constructor(public cfr: ComponentFactoryResolver) {} |
| 1662 | + @ViewChild('menuItemsContainer', {static: true, read: ViewContainerRef}) |
| 1663 | + menuItemsContainer!: ViewContainerRef; |
| 1664 | + } |
| 1665 | + |
| 1666 | + beforeEach(() => { |
| 1667 | + TestBed.configureTestingModule({ |
| 1668 | + declarations: [TestCmp], |
| 1669 | + imports: [DynamicMenuItemModule], |
| 1670 | + }); |
| 1671 | + }); |
| 1672 | + |
| 1673 | + const createElWithContent = (content: string, tagName = 'span') => { |
| 1674 | + const element = document.createElement(tagName); |
| 1675 | + element.textContent = content; |
| 1676 | + return element; |
| 1677 | + }; |
| 1678 | + |
| 1679 | + it('should support moving embedded views of projectable nodes', () => { |
| 1680 | + TestBed.overrideTemplate( |
| 1681 | + DynamicMenuItem, `<ng-template #templateRef><ng-content></ng-content></ng-template>`); |
| 1682 | + |
| 1683 | + const fixture = TestBed.createComponent(TestCmp); |
| 1684 | + const menuItemsContainer = fixture.componentInstance.menuItemsContainer; |
| 1685 | + const dynamicCmptFactory = |
| 1686 | + fixture.componentInstance.cfr.resolveComponentFactory(DynamicMenuItem); |
| 1687 | + |
| 1688 | + const cmptRefWithAa = |
| 1689 | + dynamicCmptFactory.create(Injector.NULL, [[createElWithContent('Aa')]]); |
| 1690 | + const cmptRefWithBb = |
| 1691 | + dynamicCmptFactory.create(Injector.NULL, [[createElWithContent('Bb')]]); |
| 1692 | + const cmptRefWithCc = |
| 1693 | + dynamicCmptFactory.create(Injector.NULL, [[createElWithContent('Cc')]]); |
| 1694 | + |
| 1695 | + menuItemsContainer.insert(cmptRefWithAa.instance.templateRef.createEmbeddedView({})); |
| 1696 | + menuItemsContainer.insert(cmptRefWithBb.instance.templateRef.createEmbeddedView({})); |
| 1697 | + menuItemsContainer.insert(cmptRefWithCc.instance.templateRef.createEmbeddedView({})); |
| 1698 | + |
| 1699 | + menuItemsContainer.move(menuItemsContainer.get(0)!, 1); |
| 1700 | + expect(fixture.nativeElement.textContent).toBe('BbAaCc'); |
| 1701 | + menuItemsContainer.move(menuItemsContainer.get(2)!, 1); |
| 1702 | + expect(fixture.nativeElement.textContent).toBe('BbCcAa'); |
| 1703 | + }); |
| 1704 | + |
| 1705 | + it('should support moving embedded views of projectable nodes in multiple slots', () => { |
| 1706 | + TestBed.overrideTemplate( |
| 1707 | + DynamicMenuItem, |
| 1708 | + `<ng-template #templateRef><ng-content select="span"></ng-content><ng-content select="button"></ng-content></ng-template>`); |
| 1709 | + |
| 1710 | + const fixture = TestBed.createComponent(TestCmp); |
| 1711 | + const menuItemsContainer = fixture.componentInstance.menuItemsContainer; |
| 1712 | + const dynamicCmptFactory = |
| 1713 | + fixture.componentInstance.cfr.resolveComponentFactory(DynamicMenuItem); |
| 1714 | + |
| 1715 | + const cmptRefWithAa = dynamicCmptFactory.create( |
| 1716 | + Injector.NULL, [[createElWithContent('A')], [createElWithContent('a', 'button')]]); |
| 1717 | + const cmptRefWithBb = dynamicCmptFactory.create( |
| 1718 | + Injector.NULL, [[createElWithContent('B')], [createElWithContent('b', 'button')]]); |
| 1719 | + const cmptRefWithCc = dynamicCmptFactory.create( |
| 1720 | + Injector.NULL, [[createElWithContent('C')], [createElWithContent('c', 'button')]]); |
| 1721 | + |
| 1722 | + menuItemsContainer.insert(cmptRefWithAa.instance.templateRef.createEmbeddedView({})); |
| 1723 | + menuItemsContainer.insert(cmptRefWithBb.instance.templateRef.createEmbeddedView({})); |
| 1724 | + menuItemsContainer.insert(cmptRefWithCc.instance.templateRef.createEmbeddedView({})); |
| 1725 | + |
| 1726 | + menuItemsContainer.move(menuItemsContainer.get(0)!, 1); |
| 1727 | + expect(fixture.nativeElement.textContent).toBe('BbAaCc'); |
| 1728 | + menuItemsContainer.move(menuItemsContainer.get(2)!, 1); |
| 1729 | + expect(fixture.nativeElement.textContent).toBe('BbCcAa'); |
| 1730 | + }); |
| 1731 | + |
| 1732 | + it('should support moving embedded views of projectable nodes in multiple slots and interpolations', |
| 1733 | + () => { |
| 1734 | + TestBed.overrideTemplate( |
| 1735 | + DynamicMenuItem, |
| 1736 | + `<ng-template #templateRef><ng-content select="span"></ng-content>{{itemContent}}<ng-content select="button"></ng-content></ng-template>`); |
| 1737 | + |
| 1738 | + TestBed.configureTestingModule( |
| 1739 | + {declarations: [TestCmp], imports: [DynamicMenuItemModule]}); |
| 1740 | + |
| 1741 | + const fixture = TestBed.createComponent(TestCmp); |
| 1742 | + const menuItemsContainer = fixture.componentInstance.menuItemsContainer; |
| 1743 | + const dynamicCmptFactory = |
| 1744 | + fixture.componentInstance.cfr.resolveComponentFactory(DynamicMenuItem); |
| 1745 | + |
| 1746 | + const cmptRefWithAa = dynamicCmptFactory.create( |
| 1747 | + Injector.NULL, [[createElWithContent('A')], [createElWithContent('a', 'button')]]); |
| 1748 | + const cmptRefWithBb = dynamicCmptFactory.create( |
| 1749 | + Injector.NULL, [[createElWithContent('B')], [createElWithContent('b', 'button')]]); |
| 1750 | + const cmptRefWithCc = dynamicCmptFactory.create( |
| 1751 | + Injector.NULL, [[createElWithContent('C')], [createElWithContent('c', 'button')]]); |
| 1752 | + |
| 1753 | + menuItemsContainer.insert(cmptRefWithAa.instance.templateRef.createEmbeddedView({})); |
| 1754 | + menuItemsContainer.insert(cmptRefWithBb.instance.templateRef.createEmbeddedView({})); |
| 1755 | + menuItemsContainer.insert(cmptRefWithCc.instance.templateRef.createEmbeddedView({})); |
| 1756 | + |
| 1757 | + cmptRefWithAa.instance.itemContent = '0'; |
| 1758 | + cmptRefWithBb.instance.itemContent = '1'; |
| 1759 | + cmptRefWithCc.instance.itemContent = '2'; |
| 1760 | + |
| 1761 | + fixture.detectChanges(); |
| 1762 | + |
| 1763 | + menuItemsContainer.move(menuItemsContainer.get(0)!, 1); |
| 1764 | + expect(fixture.nativeElement.textContent).toBe('B1bA0aC2c'); |
| 1765 | + menuItemsContainer.move(menuItemsContainer.get(2)!, 1); |
| 1766 | + expect(fixture.nativeElement.textContent).toBe('B1bC2cA0a'); |
| 1767 | + }); |
| 1768 | + |
| 1769 | + it('should support moving embedded views with empty projectable slots', () => { |
| 1770 | + TestBed.overrideTemplate( |
| 1771 | + DynamicMenuItem, `<ng-template #templateRef><ng-content></ng-content></ng-template>`); |
| 1772 | + |
| 1773 | + const fixture = TestBed.createComponent(TestCmp); |
| 1774 | + const menuItemsContainer = fixture.componentInstance.menuItemsContainer; |
| 1775 | + const dynamicCmptFactory = |
| 1776 | + fixture.componentInstance.cfr.resolveComponentFactory(DynamicMenuItem); |
| 1777 | + |
| 1778 | + const cmptRefWithAa = dynamicCmptFactory.create(Injector.NULL, [[]]); |
| 1779 | + const cmptRefWithBb = |
| 1780 | + dynamicCmptFactory.create(Injector.NULL, [[createElWithContent('Bb')]]); |
| 1781 | + const cmptRefWithCc = |
| 1782 | + dynamicCmptFactory.create(Injector.NULL, [[createElWithContent('Cc')]]); |
| 1783 | + |
| 1784 | + menuItemsContainer.insert(cmptRefWithAa.instance.templateRef.createEmbeddedView({})); |
| 1785 | + menuItemsContainer.insert(cmptRefWithBb.instance.templateRef.createEmbeddedView({})); |
| 1786 | + menuItemsContainer.insert(cmptRefWithCc.instance.templateRef.createEmbeddedView({})); |
| 1787 | + |
| 1788 | + menuItemsContainer.move(menuItemsContainer.get(0)!, 1); // [ Bb, NULL, Cc] |
| 1789 | + expect(fixture.nativeElement.textContent).toBe('BbCc'); |
| 1790 | + menuItemsContainer.move(menuItemsContainer.get(2)!, 1); // [ Bb, Cc, NULL] |
| 1791 | + expect(fixture.nativeElement.textContent).toBe('BbCc'); |
| 1792 | + menuItemsContainer.move(menuItemsContainer.get(0)!, 1); // [ Cc, Bb, NULL] |
| 1793 | + expect(fixture.nativeElement.textContent).toBe('CcBb'); |
| 1794 | + }); |
| 1795 | + }); |
| 1796 | + |
1644 | 1797 | describe('Property bindings', () => {
|
1645 | 1798 | modifiedInIvy('Unknown property error throws an error instead of logging it')
|
1646 | 1799 | .it('should throw on bindings to unknown properties', () => {
|
|
0 commit comments