Skip to content

Commit db35266

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
test(core): more tests for queries as signals (#54508)
A couple of tests that illustrate combination of signal and decorator queries in once component. PR Close #54508
1 parent 790f4f7 commit db35266

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

packages/core/test/acceptance/authoring/signal_queries_spec.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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';
1010
import {TestBed} from '@angular/core/testing';
1111
import {By} from '@angular/platform-browser';
1212

@@ -475,4 +475,85 @@ describe('queries as signals', () => {
475475
expect(cmpRef.instance.element()).toBeUndefined();
476476
});
477477
});
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+
});
478559
});

0 commit comments

Comments
 (0)