@@ -30,19 +30,18 @@ runInEachFileSystem(() => {
3030 env . tsconfig ( { strictTemplates : true , _enableTemplateTypeChecker : true } ) ;
3131 } ) ;
3232
33-
3433 describe ( 'supports `getPrimaryAngularDecorator()` ' , ( ) => {
3534 it ( 'for components' , ( ) => {
3635 env . write ( 'test.ts' , `
37- import {Component} from '@angular/core';
38-
39- @Component({
40- standalone: true,
41- selector: 'test-cmp',
42- template: '<div></div>',
43- })
44- export class TestCmp {}
45- ` ) ;
36+ import {Component} from '@angular/core';
37+
38+ @Component({
39+ standalone: true,
40+ selector: 'test-cmp',
41+ template: '<div></div>',
42+ })
43+ export class TestCmp {}
44+ `) ;
4645 const { program, checker} = env . driveTemplateTypeChecker ( ) ;
4746 const sf = program . getSourceFile ( _ ( '/test.ts' ) ) ;
4847 expect ( sf ) . not . toBeNull ( ) ;
@@ -52,15 +51,15 @@ runInEachFileSystem(() => {
5251
5352 it ( 'for pipes' , ( ) => {
5453 env . write ( 'test.ts' , `
55- import {Pipe, PipeTransform} from '@angular/core';
56-
57- @Pipe({name: 'expPipe'})
58- export class ExpPipe implements PipeTransform {
59- transform(value: number, exponent = 1): number {
60- return Math.pow(value, exponent);
61- }
62- }
63- ` ) ;
54+ import {Pipe, PipeTransform} from '@angular/core';
55+
56+ @Pipe({name: 'expPipe'})
57+ export class ExpPipe implements PipeTransform {
58+ transform(value: number, exponent = 1): number {
59+ return Math.pow(value, exponent);
60+ }
61+ }
62+ `) ;
6463 const { program, checker} = env . driveTemplateTypeChecker ( ) ;
6564 const sf = program . getSourceFile ( _ ( '/test.ts' ) ) ;
6665 expect ( sf ) . not . toBeNull ( ) ;
@@ -70,22 +69,106 @@ runInEachFileSystem(() => {
7069
7170 it ( 'for NgModules' , ( ) => {
7271 env . write ( 'test.ts' , `
73- import {NgModule} from '@angular/core';
74-
75- @NgModule({
76- declarations: [],
77- imports: [],
78- providers: [],
79- bootstrap: []
80- })
81- export class AppModule {}
82- ` ) ;
72+ import {NgModule} from '@angular/core';
73+
74+ @NgModule({
75+ declarations: [],
76+ imports: [],
77+ providers: [],
78+ bootstrap: []
79+ })
80+ export class AppModule {}
81+ ` ) ;
8382 const { program, checker} = env . driveTemplateTypeChecker ( ) ;
8483 const sf = program . getSourceFile ( _ ( '/test.ts' ) ) ;
8584 expect ( sf ) . not . toBeNull ( ) ;
8685 const decorator = checker . getPrimaryAngularDecorator ( getClass ( sf ! , 'AppModule' ) ) ;
8786 expect ( decorator ?. getText ( ) ) . toContain ( `declarations: []` ) ;
8887 } ) ;
8988 } ) ;
89+
90+ describe ( 'supports `getOwningNgModule()` ' , ( ) => {
91+ it ( 'for components' , ( ) => {
92+ env . write ( 'test.ts' , `
93+ import {Component, NgModule} from '@angular/core';
94+
95+ @NgModule({
96+ declarations: [AppCmp],
97+ imports: [],
98+ providers: [],
99+ bootstrap: [AppCmp]
100+ })
101+ export class AppModule {}
102+
103+ @Component({
104+ selector: 'app-cmp',
105+ template: '<div></div>',
106+ })
107+ export class AppCmp {}
108+ ` ) ;
109+ const { program, checker} = env . driveTemplateTypeChecker ( ) ;
110+ const sf = program . getSourceFile ( _ ( '/test.ts' ) ) ;
111+ expect ( sf ) . not . toBeNull ( ) ;
112+ const ngModuleKnownClass = getClass ( sf ! , 'AppModule' ) ;
113+ expect ( ngModuleKnownClass ) . not . toBeNull ( ) ;
114+ const ngModuleRetrievedClass = checker . getOwningNgModule ( getClass ( sf ! , 'AppCmp' ) ) ;
115+ expect ( ngModuleRetrievedClass ) . toEqual ( ngModuleKnownClass ) ;
116+ } ) ;
117+
118+ it ( 'for standalone components (which should be null)' , ( ) => {
119+ env . write ( 'test.ts' , `
120+ import {Component, NgModule} from '@angular/core';
121+
122+ @NgModule({
123+ declarations: [AppCmp],
124+ imports: [],
125+ providers: [],
126+ bootstrap: [AppCmp]
127+ })
128+ export class AppModule {}
129+
130+ @Component({
131+ selector: 'app-cmp',
132+ template: '<div></div>',
133+ standalone: true,
134+ })
135+ export class AppCmp {}
136+ ` ) ;
137+ const { program, checker} = env . driveTemplateTypeChecker ( ) ;
138+ const sf = program . getSourceFile ( _ ( '/test.ts' ) ) ;
139+ expect ( sf ) . not . toBeNull ( ) ;
140+ const ngModuleKnownClass = getClass ( sf ! , 'AppModule' ) ;
141+ expect ( ngModuleKnownClass ) . not . toBeNull ( ) ;
142+ const ngModuleRetrievedClass = checker . getOwningNgModule ( getClass ( sf ! , 'AppCmp' ) ) ;
143+ expect ( ngModuleRetrievedClass ) . toBe ( null ) ;
144+ } ) ;
145+
146+ it ( 'for pipes' , ( ) => {
147+ env . write ( 'test.ts' , `
148+ import {Component, NgModule, Pipe, PipeTransform} from '@angular/core';
149+
150+ @NgModule({
151+ declarations: [ExpPipe],
152+ imports: [],
153+ providers: [],
154+ })
155+ export class PipeModule {}
156+
157+ @Pipe({name: 'expPipe'})
158+ export class ExpPipe implements PipeTransform {
159+ transform(value: number, exponent = 1): number {
160+ return Math.pow(value, exponent);
161+ }
162+ }
163+ ` ) ;
164+ const { program, checker} = env . driveTemplateTypeChecker ( ) ;
165+ const sf = program . getSourceFile ( _ ( '/test.ts' ) ) ;
166+ expect ( sf ) . not . toBeNull ( ) ;
167+ const ngModuleKnownClass = getClass ( sf ! , 'PipeModule' ) ;
168+ expect ( ngModuleKnownClass ) . not . toBeNull ( ) ;
169+ const ngModuleRetrievedClass = checker . getOwningNgModule ( getClass ( sf ! , 'ExpPipe' ) ) ;
170+ expect ( ngModuleRetrievedClass ) . toEqual ( ngModuleKnownClass ) ;
171+ } ) ;
172+ } ) ;
90173 } ) ;
91174} ) ;
0 commit comments