|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {DOCUMENT} from '@angular/common'; |
10 | | -import {ApplicationRef, Component, ComponentFactoryResolver, ComponentRef, createComponent, createEnvironmentInjector, Directive, ElementRef, EmbeddedViewRef, EnvironmentInjector, inject, Injectable, InjectionToken, Injector, Input, NgModule, OnDestroy, Renderer2, Type, ViewChild, ViewContainerRef, ViewEncapsulation, ɵsetDocument} from '@angular/core'; |
| 10 | +import {ApplicationRef, Component, ComponentFactoryResolver, ComponentRef, createComponent, createEnvironmentInjector, Directive, ElementRef, EmbeddedViewRef, EnvironmentInjector, inject, Injectable, InjectionToken, Injector, Input, NgModule, OnDestroy, reflectComponentType, Renderer2, Type, ViewChild, ViewContainerRef, ViewEncapsulation, ɵsetDocument} from '@angular/core'; |
11 | 11 | import {stringifyForError} from '@angular/core/src/render3/util/stringify_utils'; |
12 | 12 | import {TestBed} from '@angular/core/testing'; |
13 | 13 | import {expect} from '@angular/platform-browser/testing/src/matchers'; |
@@ -866,4 +866,91 @@ describe('component', () => { |
866 | 866 | }); |
867 | 867 | }); |
868 | 868 | }); |
| 869 | + |
| 870 | + describe('reflectComponentType', () => { |
| 871 | + it('should create an ComponentMirror for a standalone component', () => { |
| 872 | + @Component({ |
| 873 | + selector: 'standalone-component', |
| 874 | + standalone: true, |
| 875 | + template: ` |
| 876 | + <ng-content></ng-content> |
| 877 | + <ng-content select="content-selector-a"></ng-content> |
| 878 | + <ng-content select="content-selector-b"></ng-content> |
| 879 | + <ng-content></ng-content> |
| 880 | + `, |
| 881 | + inputs: ['input-a', 'input-b:input-alias-b'], |
| 882 | + outputs: ['output-a', 'output-b:output-alias-b'], |
| 883 | + }) |
| 884 | + class StandaloneComponent { |
| 885 | + } |
| 886 | + |
| 887 | + const mirror = reflectComponentType(StandaloneComponent)!; |
| 888 | + |
| 889 | + expect(mirror.selector).toBe('standalone-component'); |
| 890 | + expect(mirror.type).toBe(StandaloneComponent); |
| 891 | + expect(mirror.isStandalone).toEqual(true); |
| 892 | + expect(mirror.inputs).toEqual([ |
| 893 | + {propName: 'input-a', templateName: 'input-a'}, |
| 894 | + {propName: 'input-b', templateName: 'input-alias-b'} |
| 895 | + ]); |
| 896 | + expect(mirror.outputs).toEqual([ |
| 897 | + {propName: 'output-a', templateName: 'output-a'}, |
| 898 | + {propName: 'output-b', templateName: 'output-alias-b'} |
| 899 | + ]); |
| 900 | + expect(mirror.ngContentSelectors).toEqual([ |
| 901 | + '*', 'content-selector-a', 'content-selector-b', '*' |
| 902 | + ]); |
| 903 | + }); |
| 904 | + |
| 905 | + it('should create an ComponentMirror for a non-standalone component', () => { |
| 906 | + @Component({ |
| 907 | + selector: 'non-standalone-component', |
| 908 | + template: ` |
| 909 | + <ng-content></ng-content> |
| 910 | + <ng-content select="content-selector-a"></ng-content> |
| 911 | + <ng-content select="content-selector-b"></ng-content> |
| 912 | + <ng-content></ng-content> |
| 913 | + `, |
| 914 | + inputs: ['input-a', 'input-b:input-alias-b'], |
| 915 | + outputs: ['output-a', 'output-b:output-alias-b'], |
| 916 | + }) |
| 917 | + class NonStandaloneComponent { |
| 918 | + } |
| 919 | + |
| 920 | + const mirror = reflectComponentType(NonStandaloneComponent)!; |
| 921 | + |
| 922 | + expect(mirror.selector).toBe('non-standalone-component'); |
| 923 | + expect(mirror.type).toBe(NonStandaloneComponent); |
| 924 | + expect(mirror.isStandalone).toEqual(false); |
| 925 | + expect(mirror.inputs).toEqual([ |
| 926 | + {propName: 'input-a', templateName: 'input-a'}, |
| 927 | + {propName: 'input-b', templateName: 'input-alias-b'} |
| 928 | + ]); |
| 929 | + expect(mirror.outputs).toEqual([ |
| 930 | + {propName: 'output-a', templateName: 'output-a'}, |
| 931 | + {propName: 'output-b', templateName: 'output-alias-b'} |
| 932 | + ]); |
| 933 | + expect(mirror.ngContentSelectors).toEqual([ |
| 934 | + '*', 'content-selector-a', 'content-selector-b', '*' |
| 935 | + ]); |
| 936 | + }); |
| 937 | + |
| 938 | + describe('error checking', () => { |
| 939 | + it('should throw when provided class is not a component', () => { |
| 940 | + class NotAnnotated {} |
| 941 | + |
| 942 | + @Directive() |
| 943 | + class ADirective { |
| 944 | + } |
| 945 | + |
| 946 | + @Injectable() |
| 947 | + class AnInjectiable { |
| 948 | + } |
| 949 | + |
| 950 | + expect(reflectComponentType(NotAnnotated)).toBe(null); |
| 951 | + expect(reflectComponentType(ADirective)).toBe(null); |
| 952 | + expect(reflectComponentType(AnInjectiable)).toBe(null); |
| 953 | + }); |
| 954 | + }); |
| 955 | + }); |
869 | 956 | }); |
0 commit comments