1
1
import { waitForAsync , TestBed } from '@angular/core/testing' ;
2
- import { Component , ViewChild } from '@angular/core' ;
2
+ import { Component , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
5
- import { CdkAccordionModule , CdkAccordionItem } from './public-api' ;
5
+ import { CdkAccordion } from './accordion' ;
6
+ import { CdkAccordionItem } from './accordion-item' ;
7
+ import { CdkAccordionModule } from './accordion-module' ;
6
8
7
9
describe ( 'CdkAccordion' , ( ) => {
8
10
beforeEach ( waitForAsync ( ( ) => {
@@ -59,6 +61,61 @@ describe('CdkAccordion', () => {
59
61
60
62
expect ( innerItem . accordion ) . not . toBe ( outerItem . accordion ) ;
61
63
} ) ;
64
+
65
+ it ( 'should be able to expand and collapse all items in multiple mode' , ( ) => {
66
+ const fixture = TestBed . createComponent ( SetOfItems ) ;
67
+ fixture . componentInstance . multi = true ;
68
+ fixture . detectChanges ( ) ;
69
+ fixture . componentInstance . accordion . openAll ( ) ;
70
+ fixture . detectChanges ( ) ;
71
+
72
+ expect ( fixture . componentInstance . items . toArray ( ) . every ( item => item . expanded ) ) . toBe ( true ) ;
73
+
74
+ fixture . componentInstance . accordion . closeAll ( ) ;
75
+ fixture . detectChanges ( ) ;
76
+
77
+ expect ( fixture . componentInstance . items . toArray ( ) . some ( item => item . expanded ) ) . toBe ( false ) ;
78
+ } ) ;
79
+
80
+ it ( 'should not be able to expand all items if multiple mode is off' , ( ) => {
81
+ const fixture = TestBed . createComponent ( SetOfItems ) ;
82
+ fixture . componentInstance . multi = false ;
83
+ fixture . detectChanges ( ) ;
84
+ fixture . componentInstance . accordion . openAll ( ) ;
85
+ fixture . detectChanges ( ) ;
86
+
87
+ expect ( fixture . componentInstance . items . toArray ( ) . some ( item => item . expanded ) ) . toBe ( false ) ;
88
+ } ) ;
89
+
90
+ it ( 'should be able to use closeAll even if multiple mode is disabled' , ( ) => {
91
+ const fixture = TestBed . createComponent ( SetOfItems ) ;
92
+ fixture . componentInstance . multi = false ;
93
+ fixture . detectChanges ( ) ;
94
+ const item = fixture . componentInstance . items . first ;
95
+
96
+ item . expanded = true ;
97
+ fixture . detectChanges ( ) ;
98
+
99
+ fixture . componentInstance . accordion . closeAll ( ) ;
100
+ fixture . detectChanges ( ) ;
101
+
102
+ expect ( item . expanded ) . toBe ( false ) ;
103
+ } ) ;
104
+
105
+ it ( 'should complete the accordion observables on destroy' , ( ) => {
106
+ const fixture = TestBed . createComponent ( SetOfItems ) ;
107
+ fixture . detectChanges ( ) ;
108
+ const stateSpy = jasmine . createSpy ( 'stateChanges complete spy' ) ;
109
+ const openCloseSpy = jasmine . createSpy ( 'openCloseAllActions complete spy' ) ;
110
+
111
+ fixture . componentInstance . accordion . _stateChanges . subscribe ( { complete : stateSpy } ) ;
112
+ fixture . componentInstance . accordion . _openCloseAllActions . subscribe ( { complete : openCloseSpy } ) ;
113
+ fixture . destroy ( ) ;
114
+
115
+ expect ( stateSpy ) . toHaveBeenCalled ( ) ;
116
+ expect ( openCloseSpy ) . toHaveBeenCalled ( ) ;
117
+ } ) ;
118
+
62
119
} ) ;
63
120
64
121
@Component ( { template : `
@@ -67,6 +124,8 @@ describe('CdkAccordion', () => {
67
124
<cdk-accordion-item></cdk-accordion-item>
68
125
</cdk-accordion>` } )
69
126
class SetOfItems {
127
+ @ViewChild ( CdkAccordion ) accordion : CdkAccordion ;
128
+ @ViewChildren ( CdkAccordionItem ) items : QueryList < CdkAccordionItem > ;
70
129
multi : boolean = false ;
71
130
}
72
131
0 commit comments