|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Component, computed, contentChild, contentChildren, createComponent, Directive, ElementRef, EnvironmentInjector, viewChild, viewChildren} from '@angular/core'; |
| 9 | +import {Component, computed, contentChild, contentChildren, createComponent, Directive, ElementRef, EnvironmentInjector, QueryList, viewChild, ViewChildren, viewChildren} from '@angular/core'; |
10 | 10 | import {TestBed} from '@angular/core/testing'; |
11 | 11 | import {By} from '@angular/platform-browser'; |
12 | 12 |
|
@@ -475,4 +475,85 @@ describe('queries as signals', () => { |
475 | 475 | expect(cmpRef.instance.element()).toBeUndefined(); |
476 | 476 | }); |
477 | 477 | }); |
| 478 | + |
| 479 | + |
| 480 | + describe('mix of signal and decorator queries', () => { |
| 481 | + it('should allow specifying both types of queries in one component', () => { |
| 482 | + @Component({ |
| 483 | + standalone: true, |
| 484 | + template: ` |
| 485 | + <div #el></div> |
| 486 | + @if (show) { |
| 487 | + <div #el></div> |
| 488 | + } |
| 489 | + `, |
| 490 | + }) |
| 491 | + class AppComponent { |
| 492 | + show = false; |
| 493 | + |
| 494 | + divElsSignal = viewChildren<ElementRef<HTMLDivElement>>('el'); |
| 495 | + |
| 496 | + @ViewChildren('el') divElsDecorator!: QueryList<ElementRef<HTMLDivElement>>; |
| 497 | + } |
| 498 | + |
| 499 | + const fixture = TestBed.createComponent(AppComponent); |
| 500 | + fixture.detectChanges(); |
| 501 | + expect(fixture.componentInstance.divElsSignal().length).toBe(1); |
| 502 | + expect(fixture.componentInstance.divElsDecorator.length).toBe(1); |
| 503 | + |
| 504 | + fixture.componentInstance.show = true; |
| 505 | + fixture.detectChanges(); |
| 506 | + expect(fixture.componentInstance.divElsSignal().length).toBe(2); |
| 507 | + expect(fixture.componentInstance.divElsDecorator.length).toBe(2); |
| 508 | + |
| 509 | + fixture.componentInstance.show = false; |
| 510 | + fixture.detectChanges(); |
| 511 | + expect(fixture.componentInstance.divElsSignal().length).toBe(1); |
| 512 | + expect(fixture.componentInstance.divElsDecorator.length).toBe(1); |
| 513 | + }); |
| 514 | + |
| 515 | + it('should allow combination via inheritance of both types of queries in one component', () => { |
| 516 | + @Component({ |
| 517 | + standalone: true, |
| 518 | + template: ` |
| 519 | + <div #el></div> |
| 520 | + @if (show) { |
| 521 | + <div #el></div> |
| 522 | + } |
| 523 | + `, |
| 524 | + }) |
| 525 | + class BaseComponent { |
| 526 | + show = false; |
| 527 | + divElsSignal = viewChildren<ElementRef<HTMLDivElement>>('el'); |
| 528 | + } |
| 529 | + |
| 530 | + @Component({ |
| 531 | + standalone: true, |
| 532 | + template: ` |
| 533 | + <div #el></div> |
| 534 | + @if (show) { |
| 535 | + <div #el></div> |
| 536 | + } |
| 537 | + ` |
| 538 | + }) |
| 539 | + class AppComponent extends BaseComponent { |
| 540 | + @ViewChildren('el') divElsDecorator!: QueryList<ElementRef<HTMLDivElement>>; |
| 541 | + } |
| 542 | + |
| 543 | + const fixture = TestBed.createComponent(AppComponent); |
| 544 | + fixture.detectChanges(); |
| 545 | + expect(fixture.componentInstance.divElsSignal().length).toBe(1); |
| 546 | + expect(fixture.componentInstance.divElsDecorator.length).toBe(1); |
| 547 | + |
| 548 | + fixture.componentInstance.show = true; |
| 549 | + fixture.detectChanges(); |
| 550 | + expect(fixture.componentInstance.divElsSignal().length).toBe(2); |
| 551 | + expect(fixture.componentInstance.divElsDecorator.length).toBe(2); |
| 552 | + |
| 553 | + fixture.componentInstance.show = false; |
| 554 | + fixture.detectChanges(); |
| 555 | + expect(fixture.componentInstance.divElsSignal().length).toBe(1); |
| 556 | + expect(fixture.componentInstance.divElsDecorator.length).toBe(1); |
| 557 | + }); |
| 558 | + }); |
478 | 559 | }); |
0 commit comments