diff --git a/src/app/shared/notifications/notifications.reducers.spec.ts b/src/app/shared/notifications/notifications.reducers.spec.ts index 5913d03545..5df9ff55e8 100644 --- a/src/app/shared/notifications/notifications.reducers.spec.ts +++ b/src/app/shared/notifications/notifications.reducers.spec.ts @@ -1,5 +1,8 @@ import { notificationsReducer } from './notifications.reducers'; -import { NewNotificationAction, RemoveAllNotificationsAction, RemoveNotificationAction } from './notifications.actions'; +import { + NewNotificationAction, NewNotificationWithTimerAction, RemoveAllNotificationsAction, + RemoveNotificationAction +} from './notifications.actions'; import { NotificationsService } from './notifications.service'; import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing'; import { NotificationsBoardComponent } from './notifications-board/notifications-board.component'; @@ -11,7 +14,13 @@ import { NotificationType } from './models/notification-type'; import { Notification } from './models/notification.model'; import { uniqueId } from 'lodash'; -fdescribe('Notification reducers', () => { +describe('Notifications reducer', () => { + + let notification1; + let notification2; + let notification3; + let notification4; + let notificationHtml; beforeEach(async () => { TestBed.configureTestingModule({ @@ -21,59 +30,85 @@ fdescribe('Notification reducers', () => { StoreModule.forRoot({notificationsReducer}), ] }); + + const options = new NotificationOptions( + 0, + true, + NotificationAnimationsType.Rotate); + notification1 = new Notification(uniqueId(), NotificationType.Success, 'title1', 'content', options, null); + notification2 = new Notification(uniqueId(), NotificationType.Success, 'title2', 'content', options, null); + notification3 = new Notification(uniqueId(), NotificationType.Success, 'title3', 'content', options, null); + notification4 = new Notification(uniqueId(), NotificationType.Success, 'title4', 'content', options, null); + const html = '
I\'m a mock test
'; + notificationHtml = new Notification(uniqueId(), NotificationType.Success, null, null, options, html); }); - it('should handle state for add, remove and removeAll', (inject([NotificationsService], (service: NotificationsService) => { - const options = new NotificationOptions( - 0, - true, - NotificationAnimationsType.Rotate); - const notification1 = new Notification(uniqueId(), NotificationType.Success, 'title1', 'content', options, null); - const notification2 = new Notification(uniqueId(), NotificationType.Success, 'title2', 'content', options, null); - const notification3 = new Notification(uniqueId(), NotificationType.Success, 'title3', 'content', options, null); - const notification4 = new Notification(uniqueId(), NotificationType.Success, 'title4', 'content', options, null); - const html = 'I\'m a mock test
'; - const notification5 = new Notification(uniqueId(), NotificationType.Success, null, null, options, html); - - console.log(notification1.id); - console.log(notification2.id); - console.log(notification3.id); - console.log(notification4.id); - console.log(notification5.id); - + it('should handle state for add', (inject([NotificationsService], (service: NotificationsService) => { const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1)); - // tick(2000); - console.log('Length: #' + state1.length); expect(state1.length).toEqual(1); - const state2 = notificationsReducer(undefined, new NewNotificationAction(notification2)); - // tick(2000); - console.log('Length: #' + state2.length); + const state2 = notificationsReducer(state1, new NewNotificationWithTimerAction(notification2)); expect(state2.length).toEqual(2); - let state = notificationsReducer(undefined, new NewNotificationAction(notification3)); - // tick(2000); - console.log('Length: #' + state.length); - expect(state.length).toEqual(3); - - state = notificationsReducer(undefined, new NewNotificationAction(notification4)); - // tick(2000); - console.log('Length: #' + state.length); - expect(state.length).toEqual(4); - - state = notificationsReducer(undefined, new NewNotificationAction(notification5)); - // tick(2000); - console.log('Length: #' + state.length); - expect(state.length).toEqual(5); - - state = notificationsReducer(undefined, new RemoveNotificationAction(notification4.id)); - expect(state.length).toEqual(4); - state = notificationsReducer(undefined, new RemoveNotificationAction(notification5.id)); - expect(state.length).toEqual(3); - - state = notificationsReducer(undefined, new RemoveAllNotificationsAction()); - expect(state.length).toEqual(0); + const state3 = notificationsReducer(state2, new NewNotificationAction(notificationHtml)); + expect(state3.length).toEqual(3); }) ) ); + + it('should handle state for remove', (inject([NotificationsService], (service: NotificationsService) => { + const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1)); + expect(state1.length).toEqual(1); + + const state2 = notificationsReducer(state1, new NewNotificationAction(notification2)); + expect(state2.length).toEqual(2); + + const state3 = notificationsReducer(state2, new RemoveNotificationAction(notification1.id)); + expect(state3.length).toEqual(1); + + }) + ) + ); + + it('should handle state for removeAll', (inject([NotificationsService], (service: NotificationsService) => { + const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1)); + expect(state1.length).toEqual(1); + + const state2 = notificationsReducer(state1, new NewNotificationAction(notification2)); + expect(state2.length).toEqual(2); + + const state3 = notificationsReducer(state2, new RemoveAllNotificationsAction()); + expect(state3.length).toEqual(0); + }) + ) + ); + + it('should handle state for add, remove and removeAll', (inject([NotificationsService], (service: NotificationsService) => { + const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1)); + expect(state1.length).toEqual(1); + + const state2 = notificationsReducer(state1, new NewNotificationAction(notification2)); + expect(state2.length).toEqual(2); + + const state3 = notificationsReducer(state2, new NewNotificationAction(notification3)); + expect(state3.length).toEqual(3); + + const state4 = notificationsReducer(state3, new NewNotificationAction(notification4)); + expect(state4.length).toEqual(4); + + const state5 = notificationsReducer(state4, new NewNotificationWithTimerAction(notificationHtml)); + expect(state5.length).toEqual(5); + + const state6 = notificationsReducer(state5, new RemoveNotificationAction(notification4.id)); + expect(state6.length).toEqual(4); + + const state7 = notificationsReducer(state6, new RemoveNotificationAction(notificationHtml.id)); + expect(state7.length).toEqual(3); + + const state8 = notificationsReducer(state7, new RemoveAllNotificationsAction()); + expect(state8.length).toEqual(0); + }) + ) + ); + }); diff --git a/src/app/shared/notifications/notifications.service.spec.ts b/src/app/shared/notifications/notifications.service.spec.ts index c30b7bbc07..b1b64921ad 100644 --- a/src/app/shared/notifications/notifications.service.spec.ts +++ b/src/app/shared/notifications/notifications.service.spec.ts @@ -1,19 +1,25 @@ -import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing'; +import { inject, TestBed } from '@angular/core/testing'; import { NotificationsService } from './notifications.service'; -import { NotificationType } from './models/notification-type'; -import { NotificationOptions } from './models/notification-options.model'; -import { NotificationAnimationsType } from './models/notification-animations-type'; import { NotificationsBoardComponent } from './notifications-board/notifications-board.component'; import { NotificationComponent } from './notification/notification.component'; import { Store, StoreModule } from '@ngrx/store'; import { notificationsReducer } from './notifications.reducers'; import { Observable } from 'rxjs/Observable'; // import 'rxjs/add/observable/of'; -import { AppState } from '../../app.reducer'; -import { notificationsStateSelector } from './selectors'; -import { PromiseObservable } from 'rxjs/observable/PromiseObservable'; +import { + NewNotificationAction, NewNotificationWithTimerAction, RemoveAllNotificationsAction, + RemoveNotificationAction +} from './notifications.actions'; +import { Notification } from './models/notification.model'; +import { NotificationType } from './models/notification-type'; describe('NotificationsService', () => { + const store: StoreI\'m a mock test
'; - const notification = service.success(null, null, options, html); - expect(notification.id !== undefined).toBeTruthy(); - expect(notification.type).toBe(NotificationType.Success); - expect(notification.title).toBeNull(); - expect(notification.content).toBeNull(); - expect(notification.html).not.toBeNull(); - expect(notification.options.timeOut).toBe(10000); - expect(notification.options.clickToClose).toBeFalsy(); - expect(notification.options.animate).toBe(NotificationAnimationsType.Rotate); - }) - ); - - it('Remove', - inject([NotificationsService, Store], fakeAsync((service: NotificationsService, store: StoreI\'m a mock test
'; - const notification5 = service.success(null, null, options, html); - tick(2000); - expect(store[id].length).toBe(5); - - // expect(notifications) - - service.remove(notification1); - tick(2000); - expect(store[id].length).toBe(0); - - }) ) - ); + it('Remove all notification', () => { + service.removeAll(); + expect(store.dispatch).toHaveBeenCalledWith(new RemoveAllNotificationsAction()); + }); });