|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | 2 | import { compileCode as compile } from './test-helpers'; |
3 | 3 | import { expectCompiles, buildRegistry } from './test-helpers'; |
| 4 | +import { ANGULAR_MAJOR } from './angular-version'; |
4 | 5 |
|
5 | 6 | describe('@NgModule', () => { |
6 | 7 | it('compiles a basic NgModule', () => { |
@@ -448,3 +449,166 @@ describe('Dependency list deduplication', () => { |
448 | 449 | expect(countRefs(deps, 'SelfCmp')).toBe(1); |
449 | 450 | }); |
450 | 451 | }); |
| 452 | + |
| 453 | +describe('NgModule scope for declared (non-standalone) components', () => { |
| 454 | + function depsArrayFor(result: string): string { |
| 455 | + const m = result.match(/dependencies:\s*\(\)\s*=>\s*\[([\s\S]*?)\]/); |
| 456 | + expect( |
| 457 | + m, |
| 458 | + 'compiled output should contain a dependencies array', |
| 459 | + ).not.toBeNull(); |
| 460 | + return m![1]; |
| 461 | + } |
| 462 | + |
| 463 | + it("inlines the declaring module's own declarations and transitive imported-module exports into a non-standalone component", () => { |
| 464 | + const componentSrc = ` |
| 465 | + import { Component } from '@angular/core'; |
| 466 | + @Component({ |
| 467 | + selector: 'app-panel', |
| 468 | + standalone: false, |
| 469 | + template: '<div highlight shared>x</div>', |
| 470 | + }) |
| 471 | + export class PanelComponent {} |
| 472 | + `; |
| 473 | + const highlightSrc = ` |
| 474 | + import { Directive } from '@angular/core'; |
| 475 | + @Directive({ selector: '[highlight]', standalone: false }) |
| 476 | + export class HighlightDirective {} |
| 477 | + `; |
| 478 | + const sharedDirectiveSrc = ` |
| 479 | + import { Directive } from '@angular/core'; |
| 480 | + @Directive({ selector: '[shared]' }) |
| 481 | + export class SharedDirective {} |
| 482 | + `; |
| 483 | + const sharedModuleSrc = ` |
| 484 | + import { NgModule } from '@angular/core'; |
| 485 | + import { SharedDirective } from './shared.directive'; |
| 486 | + @NgModule({ declarations: [SharedDirective], exports: [SharedDirective] }) |
| 487 | + export class SharedModule {} |
| 488 | + `; |
| 489 | + const moduleSrc = ` |
| 490 | + import { NgModule } from '@angular/core'; |
| 491 | + import { PanelComponent } from './panel.component'; |
| 492 | + import { HighlightDirective } from './highlight.directive'; |
| 493 | + import { SharedModule } from './shared.module'; |
| 494 | + @NgModule({ |
| 495 | + declarations: [PanelComponent, HighlightDirective], |
| 496 | + imports: [SharedModule], |
| 497 | + }) |
| 498 | + export class PanelModule {} |
| 499 | + `; |
| 500 | + |
| 501 | + const registry = buildRegistry({ |
| 502 | + 'panel.component.ts': componentSrc, |
| 503 | + 'highlight.directive.ts': highlightSrc, |
| 504 | + 'shared.directive.ts': sharedDirectiveSrc, |
| 505 | + 'shared.module.ts': sharedModuleSrc, |
| 506 | + 'panel.module.ts': moduleSrc, |
| 507 | + }); |
| 508 | + |
| 509 | + const result = compile(componentSrc, 'panel.component.ts', registry); |
| 510 | + expectCompiles(result); |
| 511 | + |
| 512 | + const deps = depsArrayFor(result); |
| 513 | + // sibling declaration from the owning module |
| 514 | + expect(deps).toContain('HighlightDirective'); |
| 515 | + // directive re-exported by an imported module (transitive scope) |
| 516 | + expect(deps).toContain('SharedDirective'); |
| 517 | + // every dependency resolved to a real selector, none left unresolved |
| 518 | + expect(result).not.toContain('_unresolved-'); |
| 519 | + }); |
| 520 | + |
| 521 | + it('resolves ModuleWithProviders (forRoot) imports in declared-component scope', () => { |
| 522 | + const widgetSrc = ` |
| 523 | + import { Component } from '@angular/core'; |
| 524 | + @Component({ |
| 525 | + selector: 'app-widget', |
| 526 | + standalone: false, |
| 527 | + template: '<i config></i>', |
| 528 | + }) |
| 529 | + export class WidgetComponent {} |
| 530 | + `; |
| 531 | + const configDirectiveSrc = ` |
| 532 | + import { Directive } from '@angular/core'; |
| 533 | + @Directive({ selector: '[config]' }) |
| 534 | + export class ConfigDirective {} |
| 535 | + `; |
| 536 | + const configModuleSrc = ` |
| 537 | + import { NgModule, ModuleWithProviders } from '@angular/core'; |
| 538 | + import { ConfigDirective } from './config.directive'; |
| 539 | + @NgModule({ declarations: [ConfigDirective], exports: [ConfigDirective] }) |
| 540 | + export class ConfigModule { |
| 541 | + static forRoot(): ModuleWithProviders<ConfigModule> { |
| 542 | + return { ngModule: ConfigModule }; |
| 543 | + } |
| 544 | + } |
| 545 | + `; |
| 546 | + const widgetModuleSrc = ` |
| 547 | + import { NgModule } from '@angular/core'; |
| 548 | + import { WidgetComponent } from './widget.component'; |
| 549 | + import { ConfigModule } from './config.module'; |
| 550 | + @NgModule({ |
| 551 | + declarations: [WidgetComponent], |
| 552 | + imports: [ConfigModule.forRoot()], |
| 553 | + }) |
| 554 | + export class WidgetModule {} |
| 555 | + `; |
| 556 | + |
| 557 | + const registry = buildRegistry({ |
| 558 | + 'widget.component.ts': widgetSrc, |
| 559 | + 'config.directive.ts': configDirectiveSrc, |
| 560 | + 'config.module.ts': configModuleSrc, |
| 561 | + 'widget.module.ts': widgetModuleSrc, |
| 562 | + }); |
| 563 | + |
| 564 | + const result = compile(widgetSrc, 'widget.component.ts', registry); |
| 565 | + expectCompiles(result); |
| 566 | + |
| 567 | + const deps = depsArrayFor(result); |
| 568 | + // directive exported by the module imported via `ConfigModule.forRoot()` |
| 569 | + expect(deps).toContain('ConfigDirective'); |
| 570 | + expect(result).not.toContain('_unresolved-'); |
| 571 | + }); |
| 572 | + |
| 573 | + it('treats a declared component that omits `standalone` per the Angular version', () => { |
| 574 | + // Angular 19+ defaults `standalone` to true, so a component must set |
| 575 | + // `standalone: false` to be NgModule-declared. On v17/v18 the flag is |
| 576 | + // usually omitted (it defaulted to false), so the owning-module scope must |
| 577 | + // still apply there. This asserts both sides of that version gate. |
| 578 | + const componentSrc = ` |
| 579 | + import { Component } from '@angular/core'; |
| 580 | + @Component({ selector: 'app-bare', template: '<div sibling></div>' }) |
| 581 | + export class BareComponent {} |
| 582 | + `; |
| 583 | + const siblingSrc = ` |
| 584 | + import { Directive } from '@angular/core'; |
| 585 | + @Directive({ selector: '[sibling]' }) |
| 586 | + export class SiblingDirective {} |
| 587 | + `; |
| 588 | + const moduleSrc = ` |
| 589 | + import { NgModule } from '@angular/core'; |
| 590 | + import { BareComponent } from './bare.component'; |
| 591 | + import { SiblingDirective } from './sibling.directive'; |
| 592 | + @NgModule({ declarations: [BareComponent, SiblingDirective] }) |
| 593 | + export class BareModule {} |
| 594 | + `; |
| 595 | + |
| 596 | + const registry = buildRegistry({ |
| 597 | + 'bare.component.ts': componentSrc, |
| 598 | + 'sibling.directive.ts': siblingSrc, |
| 599 | + 'bare.module.ts': moduleSrc, |
| 600 | + }); |
| 601 | + |
| 602 | + const result = compile(componentSrc, 'bare.component.ts', registry); |
| 603 | + expectCompiles(result); |
| 604 | + const deps = depsArrayFor(result); |
| 605 | + |
| 606 | + if (ANGULAR_MAJOR < 19) { |
| 607 | + // pre-v19: omitted flag means non-standalone, so module scope applies |
| 608 | + expect(deps).toContain('SiblingDirective'); |
| 609 | + } else { |
| 610 | + // v19+: omitted flag means standalone, so no module scope is forced |
| 611 | + expect(deps).not.toContain('SiblingDirective'); |
| 612 | + } |
| 613 | + }); |
| 614 | +}); |
0 commit comments