From 5a8e1b3ca85b3ed1c72fa4f8e95e6cce1fa112b7 Mon Sep 17 00:00:00 2001 From: Andrea Chiapparelli - 4Science Date: Tue, 27 Mar 2018 17:12:42 +0200 Subject: [PATCH] PARTIAL TEST, TO VERIFY --- .../notifications/notifications.actions.ts | 12 ++- .../notifications/notifications.effects.ts | 1 - .../notifications.reducers.spec.ts | 81 +++++++++++++++++++ .../notifications.service.spec.ts | 56 ++++++++++++- 4 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 src/app/shared/notifications/notifications.reducers.spec.ts diff --git a/src/app/shared/notifications/notifications.actions.ts b/src/app/shared/notifications/notifications.actions.ts index 6bd76a920d..8fc8551d92 100644 --- a/src/app/shared/notifications/notifications.actions.ts +++ b/src/app/shared/notifications/notifications.actions.ts @@ -1,11 +1,9 @@ // import @ngrx import { Action } from '@ngrx/store'; - // import type function import { type } from '../../shared/ngrx/type'; - // import models -import { Notification } from './models/notification.model'; +import { INotification } from './models/notification.model'; export const NotificationsActionTypes = { NEW_NOTIFICATION: type('dspace/notifications/NEW_NOTIFICATION'), @@ -23,9 +21,9 @@ export const NotificationsActionTypes = { */ export class NewNotificationAction implements Action { public type: string = NotificationsActionTypes.NEW_NOTIFICATION; - payload: Notification; + payload: INotification; - constructor(notification: Notification) { + constructor(notification: INotification) { this.payload = notification; } } @@ -37,9 +35,9 @@ export class NewNotificationAction implements Action { */ export class NewNotificationWithTimerAction implements Action { public type: string = NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER; - payload: Notification; + payload: INotification; - constructor(notification: Notification) { + constructor(notification: INotification) { this.payload = notification; } } diff --git a/src/app/shared/notifications/notifications.effects.ts b/src/app/shared/notifications/notifications.effects.ts index 345b1084e0..e3261b5acc 100644 --- a/src/app/shared/notifications/notifications.effects.ts +++ b/src/app/shared/notifications/notifications.effects.ts @@ -38,7 +38,6 @@ export class NotificationsEffects { /** * @constructor * @param {Actions} actions$ - * @param {AuthService} authService * @param {Store} store */ constructor(private actions$: Actions, diff --git a/src/app/shared/notifications/notifications.reducers.spec.ts b/src/app/shared/notifications/notifications.reducers.spec.ts new file mode 100644 index 0000000000..4b6d126bb4 --- /dev/null +++ b/src/app/shared/notifications/notifications.reducers.spec.ts @@ -0,0 +1,81 @@ +import { notificationsReducer } from './notifications.reducers'; +import { NewNotificationAction, 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'; +import { StoreModule } from '@ngrx/store'; +import { NotificationComponent } from './notification/notification.component'; +import { NotificationOptions } from './models/notification-options.model'; +import { NotificationAnimationsType } from './models/notification-animations-type'; +import { NotificationType } from './models/notification-type'; +import { Notification } from './models/notification.model'; +import { uniqueId } from 'lodash'; + +fdescribe('Notification reducers', () => { + + beforeEach(async () => { + TestBed.configureTestingModule({ + declarations: [NotificationComponent, NotificationsBoardComponent], + providers: [NotificationsService], + imports: [ + StoreModule.forRoot({notificationsReducer}), + ] + }); + }); + + it('should handle state for add, remove and removeAll', fakeAsync((inject([NotificationsService], (service: NotificationsService) => { + const options = new NotificationOptions( + 10000, + false, + 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); + + + + let state = notificationsReducer(undefined, new NewNotificationAction(notification1)); + tick(2000); + console.log('Length: #' + state.length); + expect(state.length).toEqual(1); + + state = notificationsReducer(undefined, new NewNotificationAction(notification2)); + tick(2000); + console.log('Length: #' + state.length); + expect(state.length).toEqual(2); + + 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); + }) + )) + ); +}); diff --git a/src/app/shared/notifications/notifications.service.spec.ts b/src/app/shared/notifications/notifications.service.spec.ts index 5f31a96491..c30b7bbc07 100644 --- a/src/app/shared/notifications/notifications.service.spec.ts +++ b/src/app/shared/notifications/notifications.service.spec.ts @@ -1,13 +1,17 @@ -import { inject, TestBed } from '@angular/core/testing'; +import { fakeAsync, inject, TestBed, tick } 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 { StoreModule } from '@ngrx/store'; +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'; describe('NotificationsService', () => { beforeEach(async () => { @@ -20,6 +24,8 @@ describe('NotificationsService', () => { }); }); + + it('Default options', inject([NotificationsService], (service: NotificationsService) => { const notification = service.success('Title', Observable.of('Content')); @@ -29,7 +35,7 @@ describe('NotificationsService', () => { it('Success method', inject([NotificationsService], (service: NotificationsService) => { - const notification = service.success(Observable.of('Title'), Observable.of('Content')); + const notification = service.success('Title', 'Content'); expect(notification.id !== undefined).toBeTruthy(); expect(notification.type).toBe(NotificationType.Success); expect(notification.title).toBe(Observable.of('Title')); @@ -102,4 +108,48 @@ describe('NotificationsService', () => { }) ); + it('Remove', + inject([NotificationsService, Store], fakeAsync((service: NotificationsService, store: Store) => { + const options = new NotificationOptions( + 10000, + false, + NotificationAnimationsType.Rotate); + + const id = 'notificationsReducer'; + // let state = store[id]; + // + // // let notifications = store.select(notificationsStateSelector); + // store.subscribe((state) => { + // const id = 'notificationsReducer'; + // console.log('Length: ' + state[id].length); + // // state[id].forEach( (n, i) => { + // // console.log('Notification #' + i); + // // console.log(n); + // }); + const notification1 = service.success('Title', Observable.of('Content')); + tick(2000); + expect(store[id].length).toBe(1); + const notification2 = service.error(Observable.of('Title'), Observable.of('Content')); + tick(2000); + expect(store[id].length).toBe(2); + const notification3 = service.warning(Observable.of('Title'), Observable.of('Content')); + tick(2000); + expect(store[id].length).toBe(3); + const notification4 = service.info(Observable.of('Title'), Observable.of('Content')); + tick(2000); + expect(store[id].length).toBe(4); + const html = '

I\'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); + + }) ) + ); + });