|
1 | 1 | import { MatSnackBar } from '@angular/material/snack-bar'; |
2 | 2 | import { createServiceFactory, mockProvider, SpectatorService } from '@ngneat/spectator/jest'; |
| 3 | +import { of, throwError } from 'rxjs'; |
3 | 4 | import { NotificationComponent, NotificationMode } from './notification.component'; |
4 | 5 | import { NotificationService } from './notification.service'; |
5 | 6 |
|
@@ -55,4 +56,60 @@ describe('NotificationService', () => { |
55 | 56 | }) |
56 | 57 | ); |
57 | 58 | }); |
| 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 | + }); |
58 | 115 | }); |
0 commit comments