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);
})
)
);
});