diff --git a/projects/components/src/notification/notification.service.test.ts b/projects/components/src/notification/notification.service.test.ts index b6b2c8201..46b006091 100644 --- a/projects/components/src/notification/notification.service.test.ts +++ b/projects/components/src/notification/notification.service.test.ts @@ -1,5 +1,6 @@ import { MatSnackBar } from '@angular/material/snack-bar'; import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest'; +import { of, throwError } from 'rxjs'; import { NotificationComponent, NotificationMode } from './notification.component'; import { NotificationService } from './notification.service'; @@ -55,4 +56,60 @@ describe('NotificationService', () => { }) ); }); + + test('withNotification should work correctly', () => { + spectator = createService(); + + spectator.service.wrapWithNotification(of(true), 'success', 'failure').subscribe(); + + expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith( + NotificationComponent, + expect.objectContaining({ + horizontalPosition: 'left', + verticalPosition: 'bottom', + duration: 5000, + data: expect.objectContaining({ message: 'success', mode: NotificationMode.Success }) + }) + ); + + spectator.service.wrapWithNotification(throwError('error'), 'success', 'failure').subscribe(); + + expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith( + NotificationComponent, + expect.objectContaining({ + horizontalPosition: 'left', + verticalPosition: 'bottom', + duration: 0, + data: expect.objectContaining({ message: 'failure', mode: NotificationMode.Failure }) + }) + ); + }); + + test('withNotification operator should work correctly', () => { + spectator = createService(); + + of(true).pipe(spectator.service.withNotification('success', 'failure')).subscribe(); + + expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith( + NotificationComponent, + expect.objectContaining({ + horizontalPosition: 'left', + verticalPosition: 'bottom', + duration: 5000, + data: expect.objectContaining({ message: 'success', mode: NotificationMode.Success }) + }) + ); + + throwError('error').pipe(spectator.service.withNotification('success', 'failure')).subscribe(); + + expect(spectator.inject(MatSnackBar).openFromComponent).toHaveBeenLastCalledWith( + NotificationComponent, + expect.objectContaining({ + horizontalPosition: 'left', + verticalPosition: 'bottom', + duration: 0, + data: expect.objectContaining({ message: 'failure', mode: NotificationMode.Failure }) + }) + ); + }); }); diff --git a/projects/components/src/notification/notification.service.ts b/projects/components/src/notification/notification.service.ts index 85d3585b3..c261a0ae3 100644 --- a/projects/components/src/notification/notification.service.ts +++ b/projects/components/src/notification/notification.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { MatSnackBar, MatSnackBarConfig, MatSnackBarRef } from '@angular/material/snack-bar'; -import { EMPTY, Observable, Subject } from 'rxjs'; +import { EMPTY, noop, Observable, Subject } from 'rxjs'; +import { tap } from 'rxjs/operators'; import { NotificationComponent, NotificationMode } from './notification.component'; import { NotificationModule } from './notification.module'; @@ -48,4 +49,18 @@ export class NotificationService { data: { message: message, mode: NotificationMode.Info, closedObserver: this.closedObserver$ } }); } + + public wrapWithNotification(source: Observable, successMessage: string, failureMessage: string): Observable { + return source.pipe( + tap( + noop, + () => this.createFailureToast(failureMessage), + () => this.createSuccessToast(successMessage) + ) + ); + } + + public withNotification(successMessage: string, failureMessage: string): (source: Observable) => Observable { + return (source: Observable) => this.wrapWithNotification(source, successMessage, failureMessage); + } }