Skip to content

Commit

Permalink
fix(accordion-item): emitting closed event while closed
Browse files Browse the repository at this point in the history
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 angular#9098.
  • Loading branch information
crisbeto committed Dec 26, 2017
1 parent c3d7cd9 commit 33623f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
50 changes: 30 additions & 20 deletions src/cdk/accordion/accordion-item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,54 @@ describe('CdkAccordionItem', () => {
});

it('should toggle its expanded state', () => {
expect(item.expanded).toBeFalsy();
expect(item.expanded).toBe(false);
item.toggle();
expect(item.expanded).toBeTruthy();
expect(item.expanded).toBe(true);
item.toggle();
expect(item.expanded).toBeFalsy();
expect(item.expanded).toBe(false);
});

it('should set its expanded state to expanded', () => {
item.expanded = false;
item.open();
expect(item.expanded).toBeTruthy();
expect(item.expanded).toBe(true);
});

it('should set its expanded state to closed', () => {
item.expanded = true;
item.close();
expect(item.expanded).toBeFalsy();
expect(item.expanded).toBe(false);
});

it('should emit a closed event', () => {
item.open();
fixture.detectChanges();
spyOn(item.closed, 'emit');
item.close();
fixture.detectChanges();
expect(item.closed.emit).toHaveBeenCalled();
});

it('should not emit a closed event when the item is closed already', () => {
expect(item.expanded).toBe(false);
spyOn(item.closed, 'emit');
item.close();
fixture.detectChanges();
expect(item.closed.emit).toHaveBeenCalledWith();
expect(item.closed.emit).not.toHaveBeenCalled();
});

it('should emit an opened event', () => {
spyOn(item.opened, 'emit');
item.open();
fixture.detectChanges();
expect(item.opened.emit).toHaveBeenCalledWith();
expect(item.opened.emit).toHaveBeenCalled();
});

it('should emit an destroyed event', () => {
spyOn(item.destroyed, 'emit');
item.ngOnDestroy();
fixture.detectChanges();
expect(item.destroyed.emit).toHaveBeenCalledWith();
expect(item.destroyed.emit).toHaveBeenCalled();
});
});

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

it('should not change expanded state based on unrelated items', () => {
expect(firstItem.expanded).toBeFalsy();
expect(secondItem.expanded).toBeFalsy();
expect(firstItem.expanded).toBe(false);
expect(secondItem.expanded).toBe(false);
firstItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeTruthy();
expect(secondItem.expanded).toBeFalsy();
expect(firstItem.expanded).toBe(true);
expect(secondItem.expanded).toBe(false);
secondItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeTruthy();
expect(secondItem.expanded).toBeTruthy();
expect(firstItem.expanded).toBe(true);
expect(secondItem.expanded).toBe(true);
});
});

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

it('should change expanded state based on related items', () => {
expect(firstItem.expanded).toBeFalsy();
expect(secondItem.expanded).toBeFalsy();
expect(firstItem.expanded).toBe(false);
expect(secondItem.expanded).toBe(false);
firstItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeTruthy();
expect(secondItem.expanded).toBeFalsy();
expect(firstItem.expanded).toBe(true);
expect(secondItem.expanded).toBe(false);
secondItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeFalsy();
expect(secondItem.expanded).toBeTruthy();
expect(firstItem.expanded).toBe(false);
expect(secondItem.expanded).toBe(true);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/accordion/accordion-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class CdkAccordionItem implements OnDestroy {
this._changeDetectorRef.markForCheck();
}
}
private _expanded: boolean;
private _expanded = false;

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

0 comments on commit 33623f3

Please sign in to comment.