Skip to content

Commit f4f64ac

Browse files
willshowellandrewseguin
authored andcommitted
fix(snackbar): make closeWithAction public method (#5686)
* fix(snackbar): make closeWithAction public method * Add unit tests
1 parent 940f1a2 commit f4f64ac

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

src/lib/snack-bar/simple-snack-bar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<button
44
class="mat-simple-snackbar-action"
55
*ngIf="hasAction"
6-
(click)="dismiss()">{{data.action}}</button>
6+
(click)="action()">{{data.action}}</button>

src/lib/snack-bar/simple-snack-bar.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export class SimpleSnackBar {
3636
this.data = data;
3737
}
3838

39-
/** Dismisses the snack bar. */
40-
dismiss(): void {
41-
this.snackBarRef._action();
39+
/** Performs the action on the snack bar. */
40+
action(): void {
41+
this.snackBarRef.closeWithAction();
4242
}
4343

4444
/** If the action button should be shown. */

src/lib/snack-bar/snack-bar-ref.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export class MdSnackBarRef<T> {
2525
containerInstance: MdSnackBarContainer;
2626

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

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

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

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

58-
/** Dismisses the snack bar after some duration */
59-
_dismissAfter(duration: number): void {
60-
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
61-
}
62-
6358
/** Marks the snackbar action clicked. */
64-
_action(): void {
59+
closeWithAction(): void {
6560
if (!this._onAction.closed) {
6661
this._onAction.next();
6762
this._onAction.complete();
6863
}
6964
}
7065

66+
/** Dismisses the snack bar after some duration */
67+
_dismissAfter(duration: number): void {
68+
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
69+
}
70+
7171
/** Marks the snackbar as opened */
7272
_open(): void {
7373
if (!this._afterOpened.closed) {

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,29 @@ describe('MdSnackBar', () => {
305305
tick(500);
306306
}));
307307

308+
it('should allow manually closing with an action', fakeAsync(() => {
309+
let dismissObservableCompleted = false;
310+
let actionObservableCompleted = false;
311+
let snackBarRef = snackBar.open('Some content');
312+
viewContainerFixture.detectChanges();
313+
314+
snackBarRef.afterDismissed().subscribe(undefined, undefined, () => {
315+
dismissObservableCompleted = true;
316+
});
317+
snackBarRef.onAction().subscribe(undefined, undefined, () => {
318+
actionObservableCompleted = true;
319+
});
320+
321+
snackBarRef.closeWithAction();
322+
viewContainerFixture.detectChanges();
323+
flushMicrotasks();
324+
325+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
326+
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');
327+
328+
tick(500);
329+
}));
330+
308331
it('should dismiss automatically after a specified timeout', fakeAsync(() => {
309332
let dismissObservableCompleted = false;
310333
let config = new MdSnackBarConfig();
@@ -386,6 +409,29 @@ describe('MdSnackBar', () => {
386409
.toBe('Chimichanga', 'Expected the injected data object to be the one the user provided.');
387410
});
388411

412+
it('should allow manually closing with an action', fakeAsync(() => {
413+
let dismissObservableCompleted = false;
414+
let actionObservableCompleted = false;
415+
const snackBarRef = snackBar.openFromComponent(BurritosNotification);
416+
viewContainerFixture.detectChanges();
417+
418+
snackBarRef.afterDismissed().subscribe(undefined, undefined, () => {
419+
dismissObservableCompleted = true;
420+
});
421+
snackBarRef.onAction().subscribe(undefined, undefined, () => {
422+
actionObservableCompleted = true;
423+
});
424+
425+
snackBarRef.closeWithAction();
426+
viewContainerFixture.detectChanges();
427+
flushMicrotasks();
428+
429+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
430+
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');
431+
432+
tick(500);
433+
}));
434+
389435
});
390436

391437
});

0 commit comments

Comments
 (0)