66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { Component } from '@angular/core' ;
9+ import { Component , signal } from '@angular/core' ;
1010import { TestBed } from '@angular/core/testing' ;
1111import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed' ;
1212import { ComponentHarness } from '@angular/cdk/testing' ;
1313import { AccordionHarness , AccordionGroupHarness } from './accordion-harness' ;
1414import { AccordionGroup , AccordionPanel , AccordionTrigger } from '../index' ;
15+ import { AccordionContent } from '../accordion-content' ;
1516
1617/** Lightweight test harness to test querying inside the accordion body panel. */
1718class TestButtonHarness extends ComponentHarness {
@@ -27,7 +28,7 @@ describe('Accordion Harnesses', () => {
2728 let loader : any ;
2829
2930 @Component ( {
30- imports : [ AccordionGroup , AccordionPanel , AccordionTrigger ] ,
31+ imports : [ AccordionGroup , AccordionPanel , AccordionTrigger , AccordionContent ] ,
3132 template : `
3233 <div ngAccordionGroup>
3334 <div #panel1="ngAccordionPanel" ngAccordionPanel>
@@ -37,10 +38,19 @@ describe('Accordion Harnesses', () => {
3738
3839 <div #panel2="ngAccordionPanel" ngAccordionPanel>Content 2</div>
3940 <button ngAccordionTrigger [panel]="panel2" disabled>Section 2</button>
41+
42+ <div #panel3="ngAccordionPanel" ngAccordionPanel [preserveContent]="preserveContent()">
43+ <ng-template ngAccordionContent>
44+ <button class="test-button">Inside Content 3</button>
45+ </ng-template>
46+ </div>
47+ <button ngAccordionTrigger [panel]="panel3" id="custom-id-3">Section 3</button>
4048 </div>
4149 ` ,
4250 } )
43- class AccordionHarnessTestComponent { }
51+ class AccordionHarnessTestComponent {
52+ preserveContent = signal ( false ) ;
53+ }
4454
4555 beforeEach ( ( ) => {
4656 TestBed . configureTestingModule ( {
@@ -55,14 +65,14 @@ describe('Accordion Harnesses', () => {
5565 const group = await loader . getHarness ( AccordionGroupHarness ) ;
5666 const accordions = await group . getAccordions ( ) ;
5767
58- expect ( accordions . length ) . toBe ( 2 ) ;
68+ expect ( accordions . length ) . toBe ( 3 ) ;
5969 expect ( await accordions [ 0 ] . getTitle ( ) ) . toBe ( 'Section 1' ) ;
6070 expect ( await accordions [ 1 ] . getTitle ( ) ) . toBe ( 'Section 2' ) ;
6171 } ) ;
6272
6373 it ( 'should find all individual accordions via standard root loader' , async ( ) => {
6474 const accordions = await loader . getAllHarnesses ( AccordionHarness ) ;
65- expect ( accordions . length ) . toBe ( 2 ) ;
75+ expect ( accordions . length ) . toBe ( 3 ) ;
6676 } ) ;
6777
6878 it ( 'should filter accordions by title' , async ( ) => {
@@ -147,4 +157,40 @@ describe('Accordion Harnesses', () => {
147157 const button = await accordion . getHarness ( TestButtonHarness ) ;
148158 expect ( await button . getText ( ) ) . toBe ( 'Inside Content 1' ) ;
149159 } ) ;
160+
161+ it ( 'should filter accordions by id' , async ( ) => {
162+ const accordions = await loader . getAllHarnesses ( AccordionHarness . with ( { id : 'custom-id-3' } ) ) ;
163+ expect ( accordions . length ) . toBe ( 1 ) ;
164+ expect ( await accordions [ 0 ] . getTitle ( ) ) . toBe ( 'Section 3' ) ;
165+ } ) ;
166+
167+ it ( 'should handle deferred content when collapsed' , async ( ) => {
168+ const accordion = await loader . getHarness ( AccordionHarness . with ( { title : 'Section 3' } ) ) ;
169+
170+ // Initially collapsed, content should not be available
171+ const button = await accordion . getHarnessOrNull ( TestButtonHarness ) ;
172+ expect ( button ) . toBeNull ( ) ;
173+ } ) ;
174+
175+ it ( 'should handle deferred content when expanded' , async ( ) => {
176+ const accordion = await loader . getHarness ( AccordionHarness . with ( { title : 'Section 3' } ) ) ;
177+ await accordion . expand ( ) ;
178+
179+ // Now expanded, content should be available
180+ const button = await accordion . getHarness ( TestButtonHarness ) ;
181+ expect ( await button . getText ( ) ) . toBe ( 'Inside Content 3' ) ;
182+ } ) ;
183+
184+ it ( 'should preserve content when collapsed if preserveContent is true' , async ( ) => {
185+ fixture . componentInstance . preserveContent . set ( true ) ;
186+ fixture . detectChanges ( ) ;
187+
188+ const accordion = await loader . getHarness ( AccordionHarness . with ( { title : 'Section 3' } ) ) ;
189+ await accordion . expand ( ) ; // Render it first
190+ await accordion . collapse ( ) ;
191+
192+ // Content should still be available even though collapsed
193+ const button = await accordion . getHarness ( TestButtonHarness ) ;
194+ expect ( await button . getText ( ) ) . toBe ( 'Inside Content 3' ) ;
195+ } ) ;
150196} ) ;
0 commit comments