Skip to content
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a public API now, could you add a unit test that is based around calling this? The current unit test only deals with clicking the button directly.

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