Notifications test final

This commit is contained in:
Andrea Chiapparelli - 4Science
2018-04-05 17:35:49 +02:00
parent b03acc8a3f
commit 548aafcf62
11 changed files with 251 additions and 194 deletions

View File

@@ -1,10 +1,7 @@
import { notificationsReducer } from './notifications.reducers';
import {
NewNotificationAction, NewNotificationWithTimerAction, RemoveAllNotificationsAction,
RemoveNotificationAction
} from './notifications.actions';
import { NewNotificationAction, RemoveAllNotificationsAction, RemoveNotificationAction } from './notifications.actions';
import { NotificationsService } from './notifications.service';
import { fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { fakeAsync, flush, 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';
@@ -13,102 +10,131 @@ import { NotificationAnimationsType } from './models/notification-animations-typ
import { NotificationType } from './models/notification-type';
import { Notification } from './models/notification.model';
import { uniqueId } from 'lodash';
import { ChangeDetectorRef } from '@angular/core';
describe('Notifications reducer', () => {
let notification1;
let notification2;
let notification3;
let notification4;
let notificationHtml;
let options;
let html;
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [NotificationComponent, NotificationsBoardComponent],
providers: [NotificationsService],
imports: [
ChangeDetectorRef,
StoreModule.forRoot({notificationsReducer}),
]
});
const options = new NotificationOptions(
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);
notification1 = new Notification(uniqueId(), NotificationType.Success, 'title1', 'content1', options, null);
notification2 = new Notification(uniqueId(), NotificationType.Info, 'title2', 'content2', options, null);
notification3 = new Notification(uniqueId(), NotificationType.Warning, 'title3', 'content3', options, null);
html = '<p>I\'m a mock test</p>';
notificationHtml = new Notification(uniqueId(), NotificationType.Error, null, null, options, html);
});
it('should handle state for add', (inject([NotificationsService], (service: NotificationsService) => {
const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1));
expect(state1.length).toEqual(1);
it('should add 4 notifications and verify fields and length', () => {
const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1));
const n1 = state1[0];
expect(n1.title).toBe('title1');
expect(n1.content).toBe('content1');
expect(n1.type).toBe(NotificationType.Success);
expect(n1.options).toBe(options);
expect(n1.html).toBeNull();
expect(state1.length).toEqual(1);
const state2 = notificationsReducer(state1, new NewNotificationWithTimerAction(notification2));
expect(state2.length).toEqual(2);
const state2 = notificationsReducer(state1, new NewNotificationAction(notification2));
const n2 = state2[1];
expect(n2.title).toBe('title2');
expect(n2.content).toBe('content2');
expect(n2.type).toBe(NotificationType.Info);
expect(n2.options).toBe(options);
expect(n2.html).toBeNull();
expect(state2.length).toEqual(2);
const state3 = notificationsReducer(state2, new NewNotificationAction(notificationHtml));
expect(state3.length).toEqual(3);
})
)
);
const state3 = notificationsReducer(state2, new NewNotificationAction(notification3));
const n3 = state3[2];
expect(n3.title).toBe('title3');
expect(n3.content).toBe('content3');
expect(n3.type).toBe(NotificationType.Warning);
expect(n3.options).toBe(options);
expect(n3.html).toBeNull();
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 state4 = notificationsReducer(state3, new NewNotificationAction(notificationHtml));
const n4 = state4[3];
expect(n4.title).toBeNull();
expect(n4.content).toBeNull();
expect(n4.type).toBe(NotificationType.Error);
expect(n4.options).toBe(options);
expect(n4.html).toBe(html);
expect(state4.length).toEqual(4);
});
const state2 = notificationsReducer(state1, new NewNotificationAction(notification2));
expect(state2.length).toEqual(2);
it('should add 2 notifications and remove only the first', () => {
const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1));
expect(state1.length).toEqual(1);
const state3 = notificationsReducer(state2, new RemoveNotificationAction(notification1.id));
expect(state3.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);
it('should add 2 notifications and later remove all', () => {
const state1 = notificationsReducer(undefined, new NewNotificationAction(notification1));
expect(state1.length).toEqual(1);
const state3 = notificationsReducer(state2, new RemoveAllNotificationsAction());
expect(state3.length).toEqual(0);
})
)
);
const state2 = notificationsReducer(state1, new NewNotificationAction(notification2));
expect(state2.length).toEqual(2);
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 state3 = notificationsReducer(state2, new RemoveAllNotificationsAction());
expect(state3.length).toEqual(0);
});
const state2 = notificationsReducer(state1, new NewNotificationAction(notification2));
expect(state2.length).toEqual(2);
it('should create 2 notifications and check they close after different timeout', fakeAsync(() => {
inject([ChangeDetectorRef], (cdr: ChangeDetectorRef) => {
const optionsWithTimeout = new NotificationOptions(
1000,
true,
NotificationAnimationsType.Rotate);
// Timeout 1000ms
const notification = new Notification(uniqueId(), NotificationType.Success, 'title', 'content', optionsWithTimeout, null);
const state = notificationsReducer(undefined, new NewNotificationAction(notification));
expect(state.length).toEqual(1);
const state3 = notificationsReducer(state2, new NewNotificationAction(notification3));
expect(state3.length).toEqual(3);
// Timeout default 5000ms
const notificationBis = new Notification(uniqueId(), NotificationType.Success, 'title', 'content');
const stateBis = notificationsReducer(state, new NewNotificationAction(notification));
expect(stateBis.length).toEqual(2);
const state4 = notificationsReducer(state3, new NewNotificationAction(notification4));
expect(state4.length).toEqual(4);
tick(1000);
cdr.detectChanges();
const state5 = notificationsReducer(state4, new NewNotificationWithTimerAction(notificationHtml));
expect(state5.length).toEqual(5);
const action = new NewNotificationAction(notification);
action.type = 'NothingToDo, return only the state';
const state6 = notificationsReducer(state5, new RemoveNotificationAction(notification4.id));
expect(state6.length).toEqual(4);
const lastState = notificationsReducer(stateBis, action);
expect(lastState.length).toEqual(1);
const state7 = notificationsReducer(state6, new RemoveNotificationAction(notificationHtml.id));
expect(state7.length).toEqual(3);
flush();
cdr.detectChanges();
const state8 = notificationsReducer(state7, new RemoveAllNotificationsAction());
expect(state8.length).toEqual(0);
})
)
);
const finalState = notificationsReducer(lastState, action);
expect(finalState.length).toEqual(0);
});
}));
});