Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(accordion-item): emitting closed event while closed #9101

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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