Skip to content

Commit

Permalink
fix(snackbar): make closeWithAction public method (#5686)
Browse files Browse the repository at this point in the history
* fix(snackbar): make closeWithAction public method

* Add unit tests
  • Loading branch information
willshowell authored and andrewseguin committed Jul 25, 2017
1 parent 940f1a2 commit f4f64ac
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/lib/snack-bar/simple-snack-bar.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<button
class="mat-simple-snackbar-action"
*ngIf="hasAction"
(click)="dismiss()">{{data.action}}</button>
(click)="action()">{{data.action}}</button>
6 changes: 3 additions & 3 deletions src/lib/snack-bar/simple-snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export class SimpleSnackBar {
this.data = data;
}

/** Dismisses the snack bar. */
dismiss(): void {
this.snackBarRef._action();
/** Performs the action on the snack bar. */
action(): void {
this.snackBarRef.closeWithAction();
}

/** If the action button should be shown. */
Expand Down
18 changes: 9 additions & 9 deletions src/lib/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export class MdSnackBarRef<T> {
containerInstance: MdSnackBarContainer;

/** Subject for notifying the user that the snack bar has closed. */
private _afterClosed: Subject<any> = new Subject();
private _afterClosed = new Subject<void>();

/** Subject for notifying the user that the snack bar has opened and appeared. */
private _afterOpened: Subject<any>;
private _afterOpened = new Subject<void>();

/** Subject for notifying the user that the snack bar action was called. */
private _onAction: Subject<any> = new Subject();
private _onAction = new Subject<void>();

/**
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
Expand All @@ -55,19 +55,19 @@ export class MdSnackBarRef<T> {
clearTimeout(this._durationTimeoutId);
}

/** Dismisses the snack bar after some duration */
_dismissAfter(duration: number): void {
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
}

/** Marks the snackbar action clicked. */
_action(): void {
closeWithAction(): void {
if (!this._onAction.closed) {
this._onAction.next();
this._onAction.complete();
}
}

/** Dismisses the snack bar after some duration */
_dismissAfter(duration: number): void {
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
}

/** Marks the snackbar as opened */
_open(): void {
if (!this._afterOpened.closed) {
Expand Down
46 changes: 46 additions & 0 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,29 @@ describe('MdSnackBar', () => {
tick(500);
}));

it('should allow manually closing with an action', fakeAsync(() => {
let dismissObservableCompleted = false;
let actionObservableCompleted = false;
let snackBarRef = snackBar.open('Some content');
viewContainerFixture.detectChanges();

snackBarRef.afterDismissed().subscribe(undefined, undefined, () => {
dismissObservableCompleted = true;
});
snackBarRef.onAction().subscribe(undefined, undefined, () => {
actionObservableCompleted = true;
});

snackBarRef.closeWithAction();
viewContainerFixture.detectChanges();
flushMicrotasks();

expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');

tick(500);
}));

it('should dismiss automatically after a specified timeout', fakeAsync(() => {
let dismissObservableCompleted = false;
let config = new MdSnackBarConfig();
Expand Down Expand Up @@ -386,6 +409,29 @@ describe('MdSnackBar', () => {
.toBe('Chimichanga', 'Expected the injected data object to be the one the user provided.');
});

it('should allow manually closing with an action', fakeAsync(() => {
let dismissObservableCompleted = false;
let actionObservableCompleted = false;
const snackBarRef = snackBar.openFromComponent(BurritosNotification);
viewContainerFixture.detectChanges();

snackBarRef.afterDismissed().subscribe(undefined, undefined, () => {
dismissObservableCompleted = true;
});
snackBarRef.onAction().subscribe(undefined, undefined, () => {
actionObservableCompleted = true;
});

snackBarRef.closeWithAction();
viewContainerFixture.detectChanges();
flushMicrotasks();

expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');

tick(500);
}));

});

});
Expand Down

0 comments on commit f4f64ac

Please sign in to comment.