Skip to content

Commit 6c2d647

Browse files
authored
feat: adding notification operator (#575)
1 parent fba66dc commit 6c2d647

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +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';
34
import { NotificationComponent, NotificationMode } from './notification.component';
45
import { NotificationService } from './notification.service';
56

@@ -55,4 +56,60 @@ describe('NotificationService', () => {
5556
})
5657
);
5758
});
59+
60+
test('withNotification should work correctly', () => {
61+
spectator = createService();
62+
63+
spectator.service.wrapWithNotification(of(true), 'success', 'failure').subscribe();
64+
65+
expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith(
66+
NotificationComponent,
67+
expect.objectContaining({
68+
horizontalPosition: 'left',
69+
verticalPosition: 'bottom',
70+
duration: 5000,
71+
data: expect.objectContaining({ message: 'success', mode: NotificationMode.Success })
72+
})
73+
);
74+
75+
spectator.service.wrapWithNotification(throwError('error'), 'success', 'failure').subscribe();
76+
77+
expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith(
78+
NotificationComponent,
79+
expect.objectContaining({
80+
horizontalPosition: 'left',
81+
verticalPosition: 'bottom',
82+
duration: 0,
83+
data: expect.objectContaining({ message: 'failure', mode: NotificationMode.Failure })
84+
})
85+
);
86+
});
87+
88+
test('withNotification operator should work correctly', () => {
89+
spectator = createService();
90+
91+
of(true).pipe(spectator.service.withNotification('success', 'failure')).subscribe();
92+
93+
expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith(
94+
NotificationComponent,
95+
expect.objectContaining({
96+
horizontalPosition: 'left',
97+
verticalPosition: 'bottom',
98+
duration: 5000,
99+
data: expect.objectContaining({ message: 'success', mode: NotificationMode.Success })
100+
})
101+
);
102+
103+
throwError('error').pipe(spectator.service.withNotification('success', 'failure')).subscribe();
104+
105+
expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith(
106+
NotificationComponent,
107+
expect.objectContaining({
108+
horizontalPosition: 'left',
109+
verticalPosition: 'bottom',
110+
duration: 0,
111+
data: expect.objectContaining({ message: 'failure', mode: NotificationMode.Failure })
112+
})
113+
);
114+
});
58115
});

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { MatSnackBar, MatSnackBarConfig, MatSnackBarRef } from '@angular/material/snack-bar';
3-
import { EMPTY, Observable, Subject } from 'rxjs';
3+
import { EMPTY, noop, Observable, Subject } from 'rxjs';
4+
import { tap } from 'rxjs/operators';
45
import { NotificationComponent, NotificationMode } from './notification.component';
56
import { NotificationModule } from './notification.module';
67

@@ -48,4 +49,18 @@ export class NotificationService {
4849
data: { message: message, mode: NotificationMode.Info, closedObserver: this.closedObserver$ }
4950
});
5051
}
52+
53+
public wrapWithNotification<T>(source: Observable<T>, successMessage: string, failureMessage: string): Observable<T> {
54+
return source.pipe(
55+
tap(
56+
noop,
57+
() => this.createFailureToast(failureMessage),
58+
() => this.createSuccessToast(successMessage)
59+
)
60+
);
61+
}
62+
63+
public withNotification<T>(successMessage: string, failureMessage: string): (source: Observable<T>) => Observable<T> {
64+
return (source: Observable<T>) => this.wrapWithNotification(source, successMessage, failureMessage);
65+
}
5166
}

0 commit comments

Comments
 (0)