Skip to content

Commit 3822823

Browse files
authored
Fix notification operator (#673)
* refactor: success will be shown when source emits atleast one value * refactor: fixing lint
1 parent 0234ae5 commit 3822823

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

projects/components/src/notification/notification.service.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MatSnackBar } from '@angular/material/snack-bar';
22
import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest';
3-
import { of, throwError } from 'rxjs';
3+
import { EMPTY, of, throwError } from 'rxjs';
44
import { NotificationComponent, NotificationMode } from './notification.component';
55
import { NotificationService } from './notification.service';
66

@@ -87,10 +87,11 @@ describe('NotificationService', () => {
8787

8888
test('withNotification operator should work correctly', () => {
8989
spectator = createService();
90+
const matSnackBar = spectator.inject(MatSnackBar);
9091

9192
of(true).pipe(spectator.service.withNotification('success', 'failure')).subscribe();
9293

93-
expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith(
94+
expect(matSnackBar.openFromComponent).toHaveBeenLastCalledWith(
9495
NotificationComponent,
9596
expect.objectContaining({
9697
horizontalPosition: 'left',
@@ -102,7 +103,7 @@ describe('NotificationService', () => {
102103

103104
throwError('error').pipe(spectator.service.withNotification('success', 'failure')).subscribe();
104105

105-
expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith(
106+
expect(matSnackBar.openFromComponent).toHaveBeenLastCalledWith(
106107
NotificationComponent,
107108
expect.objectContaining({
108109
horizontalPosition: 'left',
@@ -111,5 +112,10 @@ describe('NotificationService', () => {
111112
data: expect.objectContaining({ message: 'failure', mode: NotificationMode.Failure })
112113
})
113114
);
115+
116+
// Completing the source observable without emitting a value should not show any message
117+
matSnackBar.openFromComponent.mockClear();
118+
EMPTY.pipe(spectator.service.withNotification('success', 'failure')).subscribe();
119+
expect(matSnackBar.openFromComponent).not.toHaveBeenCalled();
114120
});
115121
});

projects/components/src/notification/notification.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { MatSnackBar, MatSnackBarConfig, MatSnackBarRef } from '@angular/material/snack-bar';
3-
import { EMPTY, noop, Observable, Subject } from 'rxjs';
3+
import { EMPTY, Observable, Subject } from 'rxjs';
44
import { tap } from 'rxjs/operators';
55
import { NotificationComponent, NotificationMode } from './notification.component';
66
import { NotificationModule } from './notification.module';
@@ -51,11 +51,13 @@ export class NotificationService {
5151
}
5252

5353
public wrapWithNotification<T>(source: Observable<T>, successMessage: string, failureMessage: string): Observable<T> {
54+
let emitted = false;
55+
5456
return source.pipe(
5557
tap(
56-
noop,
58+
() => (emitted = true),
5759
() => this.createFailureToast(failureMessage),
58-
() => this.createSuccessToast(successMessage)
60+
() => emitted && this.createSuccessToast(successMessage)
5961
)
6062
);
6163
}

0 commit comments

Comments
 (0)