Skip to content

Commit 6ef0401

Browse files
crisbetojelbourn
authored andcommitted
fix(expansion): accordion emitting closed event while closed (#9101)
Fixes the accordion item emitting its closed event once, even if it is closed. It is due to the `expanded` property defaulting to `undefined`. Also includes making a few of the tests a bit more strict to ensure that the `expanded` property is always a boolean. Fixes #9098.
1 parent 0fdeab8 commit 6ef0401

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

src/cdk/accordion/accordion-item.spec.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,54 @@ describe('CdkAccordionItem', () => {
3232
});
3333

3434
it('should toggle its expanded state', () => {
35-
expect(item.expanded).toBeFalsy();
35+
expect(item.expanded).toBe(false);
3636
item.toggle();
37-
expect(item.expanded).toBeTruthy();
37+
expect(item.expanded).toBe(true);
3838
item.toggle();
39-
expect(item.expanded).toBeFalsy();
39+
expect(item.expanded).toBe(false);
4040
});
4141

4242
it('should set its expanded state to expanded', () => {
4343
item.expanded = false;
4444
item.open();
45-
expect(item.expanded).toBeTruthy();
45+
expect(item.expanded).toBe(true);
4646
});
4747

4848
it('should set its expanded state to closed', () => {
4949
item.expanded = true;
5050
item.close();
51-
expect(item.expanded).toBeFalsy();
51+
expect(item.expanded).toBe(false);
5252
});
5353

5454
it('should emit a closed event', () => {
55+
item.open();
56+
fixture.detectChanges();
57+
spyOn(item.closed, 'emit');
58+
item.close();
59+
fixture.detectChanges();
60+
expect(item.closed.emit).toHaveBeenCalled();
61+
});
62+
63+
it('should not emit a closed event when the item is closed already', () => {
64+
expect(item.expanded).toBe(false);
5565
spyOn(item.closed, 'emit');
5666
item.close();
5767
fixture.detectChanges();
58-
expect(item.closed.emit).toHaveBeenCalledWith();
68+
expect(item.closed.emit).not.toHaveBeenCalled();
5969
});
6070

6171
it('should emit an opened event', () => {
6272
spyOn(item.opened, 'emit');
6373
item.open();
6474
fixture.detectChanges();
65-
expect(item.opened.emit).toHaveBeenCalledWith();
75+
expect(item.opened.emit).toHaveBeenCalled();
6676
});
6777

6878
it('should emit an destroyed event', () => {
6979
spyOn(item.destroyed, 'emit');
7080
item.ngOnDestroy();
7181
fixture.detectChanges();
72-
expect(item.destroyed.emit).toHaveBeenCalledWith();
82+
expect(item.destroyed.emit).toHaveBeenCalled();
7383
});
7484
});
7585

@@ -88,16 +98,16 @@ describe('CdkAccordionItem', () => {
8898
});
8999

90100
it('should not change expanded state based on unrelated items', () => {
91-
expect(firstItem.expanded).toBeFalsy();
92-
expect(secondItem.expanded).toBeFalsy();
101+
expect(firstItem.expanded).toBe(false);
102+
expect(secondItem.expanded).toBe(false);
93103
firstItem.open();
94104
fixture.detectChanges();
95-
expect(firstItem.expanded).toBeTruthy();
96-
expect(secondItem.expanded).toBeFalsy();
105+
expect(firstItem.expanded).toBe(true);
106+
expect(secondItem.expanded).toBe(false);
97107
secondItem.open();
98108
fixture.detectChanges();
99-
expect(firstItem.expanded).toBeTruthy();
100-
expect(secondItem.expanded).toBeTruthy();
109+
expect(firstItem.expanded).toBe(true);
110+
expect(secondItem.expanded).toBe(true);
101111
});
102112
});
103113

@@ -117,16 +127,16 @@ describe('CdkAccordionItem', () => {
117127
});
118128

119129
it('should change expanded state based on related items', () => {
120-
expect(firstItem.expanded).toBeFalsy();
121-
expect(secondItem.expanded).toBeFalsy();
130+
expect(firstItem.expanded).toBe(false);
131+
expect(secondItem.expanded).toBe(false);
122132
firstItem.open();
123133
fixture.detectChanges();
124-
expect(firstItem.expanded).toBeTruthy();
125-
expect(secondItem.expanded).toBeFalsy();
134+
expect(firstItem.expanded).toBe(true);
135+
expect(secondItem.expanded).toBe(false);
126136
secondItem.open();
127137
fixture.detectChanges();
128-
expect(firstItem.expanded).toBeFalsy();
129-
expect(secondItem.expanded).toBeTruthy();
138+
expect(firstItem.expanded).toBe(false);
139+
expect(secondItem.expanded).toBe(true);
130140
});
131141
});
132142
});

src/cdk/accordion/accordion-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class CdkAccordionItem implements OnDestroy {
6666
this._changeDetectorRef.markForCheck();
6767
}
6868
}
69-
private _expanded: boolean;
69+
private _expanded = false;
7070

7171
/** Unregister function for _expansionDispatcher. */
7272
private _removeUniqueSelectionListener: () => void = () => {};

0 commit comments

Comments
 (0)