Skip to content
Snippets Groups Projects
Commit 5a8e1b3c authored by Andrea Chiapparelli - 4Science's avatar Andrea Chiapparelli - 4Science
Browse files

PARTIAL TEST, TO VERIFY

parent d6490750
Branches
Tags
No related merge requests found
// import @ngrx // import @ngrx
import { Action } from '@ngrx/store'; import { Action } from '@ngrx/store';
// import type function // import type function
import { type } from '../../shared/ngrx/type'; import { type } from '../../shared/ngrx/type';
// import models // import models
import { Notification } from './models/notification.model'; import { INotification } from './models/notification.model';
export const NotificationsActionTypes = { export const NotificationsActionTypes = {
NEW_NOTIFICATION: type('dspace/notifications/NEW_NOTIFICATION'), NEW_NOTIFICATION: type('dspace/notifications/NEW_NOTIFICATION'),
...@@ -23,9 +21,9 @@ export const NotificationsActionTypes = { ...@@ -23,9 +21,9 @@ export const NotificationsActionTypes = {
*/ */
export class NewNotificationAction implements Action { export class NewNotificationAction implements Action {
public type: string = NotificationsActionTypes.NEW_NOTIFICATION; public type: string = NotificationsActionTypes.NEW_NOTIFICATION;
payload: Notification; payload: INotification;
constructor(notification: Notification) { constructor(notification: INotification) {
this.payload = notification; this.payload = notification;
} }
} }
...@@ -37,9 +35,9 @@ export class NewNotificationAction implements Action { ...@@ -37,9 +35,9 @@ export class NewNotificationAction implements Action {
*/ */
export class NewNotificationWithTimerAction implements Action { export class NewNotificationWithTimerAction implements Action {
public type: string = NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER; public type: string = NotificationsActionTypes.NEW_NOTIFICATION_WITH_TIMER;
payload: Notification; payload: INotification;
constructor(notification: Notification) { constructor(notification: INotification) {
this.payload = notification; this.payload = notification;
} }
} }
......
...@@ -38,7 +38,6 @@ export class NotificationsEffects { ...@@ -38,7 +38,6 @@ export class NotificationsEffects {
/** /**
* @constructor * @constructor
* @param {Actions} actions$ * @param {Actions} actions$
* @param {AuthService} authService
* @param {Store} store * @param {Store} store
*/ */
constructor(private actions$: Actions, constructor(private actions$: Actions,
......
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 = '<p>I\'m a mock test</p>';
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);
})
))
);
});
import { inject, TestBed } from '@angular/core/testing'; import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { NotificationsService } from './notifications.service'; import { NotificationsService } from './notifications.service';
import { NotificationType } from './models/notification-type'; import { NotificationType } from './models/notification-type';
import { NotificationOptions } from './models/notification-options.model'; import { NotificationOptions } from './models/notification-options.model';
import { NotificationAnimationsType } from './models/notification-animations-type'; import { NotificationAnimationsType } from './models/notification-animations-type';
import { NotificationsBoardComponent } from './notifications-board/notifications-board.component'; import { NotificationsBoardComponent } from './notifications-board/notifications-board.component';
import { NotificationComponent } from './notification/notification.component'; import { NotificationComponent } from './notification/notification.component';
import { StoreModule } from '@ngrx/store'; import { Store, StoreModule } from '@ngrx/store';
import { notificationsReducer } from './notifications.reducers'; import { notificationsReducer } from './notifications.reducers';
import { Observable } from 'rxjs/Observable'; 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', () => { describe('NotificationsService', () => {
beforeEach(async () => { beforeEach(async () => {
...@@ -20,6 +24,8 @@ describe('NotificationsService', () => { ...@@ -20,6 +24,8 @@ describe('NotificationsService', () => {
}); });
}); });
it('Default options', it('Default options',
inject([NotificationsService], (service: NotificationsService) => { inject([NotificationsService], (service: NotificationsService) => {
const notification = service.success('Title', Observable.of('Content')); const notification = service.success('Title', Observable.of('Content'));
...@@ -29,7 +35,7 @@ describe('NotificationsService', () => { ...@@ -29,7 +35,7 @@ describe('NotificationsService', () => {
it('Success method', it('Success method',
inject([NotificationsService], (service: NotificationsService) => { 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.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Success); expect(notification.type).toBe(NotificationType.Success);
expect(notification.title).toBe(Observable.of('Title')); expect(notification.title).toBe(Observable.of('Title'));
...@@ -102,4 +108,48 @@ describe('NotificationsService', () => { ...@@ -102,4 +108,48 @@ describe('NotificationsService', () => {
}) })
); );
it('Remove',
inject([NotificationsService, Store], fakeAsync((service: NotificationsService, store: Store<AppState>) => {
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 = '<p>I\'m a mock test</p>';
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);
}) )
);
}); });
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment