notifications test

This commit is contained in:
Andrea Chiapparelli - 4Science
2018-04-04 17:53:00 +02:00
parent 50d6e119ea
commit b03acc8a3f
2 changed files with 126 additions and 176 deletions

View File

@@ -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 = '<p>I\'m a mock test</p>';
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 = '<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);
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);
})
)
);
});

View File

@@ -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: Store<Notification> = jasmine.createSpyObj('store', {
dispatch: {},
select: Observable.of(true)
});
let service;
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [NotificationComponent, NotificationsBoardComponent],
@@ -22,134 +28,43 @@ describe('NotificationsService', () => {
StoreModule.forRoot({notificationsReducer}),
]
});
service = new NotificationsService(store);
});
it('Success notification', () => {
const notification = service.success('Title', Observable.of('Content'));
expect(notification.type).toBe(NotificationType.Success);
expect(store.dispatch).toHaveBeenCalledWith(new NewNotificationWithTimerAction(notification));
});
it('Warning notification', () => {
const notification = service.warning('Title', Observable.of('Content'));
expect(notification.type).toBe(NotificationType.Warning);
expect(store.dispatch).toHaveBeenCalledWith(new NewNotificationWithTimerAction(notification));
});
it('Default options',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.success('Title', Observable.of('Content'));
expect(notification.options.clickToClose).toBe(true);
})
);
it('Info notification', () => {
const notification = service.info('Title', Observable.of('Content'));
expect(notification.type).toBe(NotificationType.Info);
expect(store.dispatch).toHaveBeenCalledWith(new NewNotificationWithTimerAction(notification));
});
it('Success method',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.success('Title', 'Content');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Success);
expect(notification.title).toBe(Observable.of('Title'));
expect(notification.content).toBe(Observable.of('Content'));
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
it('Error notification', () => {
const notification = service.error('Title', Observable.of('Content'));
expect(notification.type).toBe(NotificationType.Error);
expect(store.dispatch).toHaveBeenCalledWith(new NewNotificationWithTimerAction(notification));
});
it('Error method',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.error(Observable.of('Title'), Observable.of('Content'));
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Error);
expect(notification.title).toBe(Observable.of('Title'));
expect(notification.content).toBe(Observable.of('Content'));
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
it('Remove notification', () => {
const notification = new Notification('1234', NotificationType.Info, 'title...', 'description');
service.remove(notification);
expect(store.dispatch).toHaveBeenCalledWith(new RemoveNotificationAction(notification.id));
});
it('Warning method',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.warning(Observable.of('Title'), Observable.of('Content'));
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Warning);
expect(notification.title).toBe(Observable.of('Title'));
expect(notification.content).toBe(Observable.of('Content'));
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
it('Info method',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.info(Observable.of('Title'), Observable.of('Content'));
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Info);
expect(notification.title).toBe(Observable.of('Title'));
expect(notification.content).toBe(Observable.of('Content'));
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
it('Html content',
inject([NotificationsService], (service: NotificationsService) => {
const options = new NotificationOptions(
10000,
false,
NotificationAnimationsType.Rotate);
const html = '<p>I\'m a mock test</p>';
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: 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);
}) )
);
it('Remove all notification', () => {
service.removeAll();
expect(store.dispatch).toHaveBeenCalledWith(new RemoveAllNotificationsAction());
});
});