This commit is contained in:
Andrea Chiapparelli - 4Science
2018-03-08 13:30:57 +01:00
parent 6397e6fae8
commit c102dd8b93
2 changed files with 94 additions and 254 deletions

View File

@@ -1,263 +1,104 @@
import {inject, TestBed} from '@angular/core/testing'; import { inject, TestBed } from '@angular/core/testing';
import {NotificationsService} from './notifications.service'; import { NotificationsService } from './notifications.service';
import {defaultIcons} from '../interfaces/icons'; import { NotificationType } from './models/notification-type';
import {NotificationEvent} from '../interfaces/notification-event.type'; import { NotificationOptions } from './models/notification-options.model';
import {Notification} from '../interfaces/notification.type'; 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 { notificationsReducer } from './notifications.reducers';
describe('NotificationsService', () => { describe('NotificationsService', () => {
beforeEach(async () => {
beforeEach(() => { TestBed.configureTestingModule({
TestBed.configureTestingModule({ declarations: [NotificationComponent, NotificationsBoardComponent],
declarations: [], providers: [NotificationsService],
providers: [NotificationsService], imports: [
}); StoreModule.forRoot({notificationsReducer}),
]
}); });
});
let defaultNotification = { it('Default options',
id: '0', inject([NotificationsService], (service: NotificationsService) => {
title: 'Test title', const notification = service.success('Title', 'Content');
type: 'success', expect(notification.options.clickToClose).toBe(true);
icon: defaultIcons.success, })
content: 'Test Content', );
timeOut: 0,
maxLength: 0,
clickToClose: true,
clickIconToClose: false,
showProgressBar: true,
pauseOnHover: true,
theClass: 'initial',
rtl: false,
animate: 'fromRight',
createdOn: new Date(),
destroyedOn: new Date()
};
it('Service instantiates', it('Success method',
inject([NotificationsService], (service: NotificationsService) => { inject([NotificationsService], (service: NotificationsService) => {
expect(service instanceof NotificationsService).toBeTruthy(); const notification = service.success('Title', 'Content');
}) expect(notification.id !== undefined).toBeTruthy();
); expect(notification.type).toBe(NotificationType.Success);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Content');
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
it('If override is not set, id is randomly generated', it('Error method',
inject([NotificationsService], (service: NotificationsService) => { inject([NotificationsService], (service: NotificationsService) => {
const notification = service.error('Title', 'Content');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Error);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Content');
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
let notificationEvent: NotificationEvent = null; it('Warning method',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.warning('Title', 'Content');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Warning);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Content');
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
service.emitter.subscribe(item => notificationEvent = item); it('Info method',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.info('Title', 'Content');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe(NotificationType.Info);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Content');
expect(notification.html).toBeUndefined();
expect(notification.options.timeOut).toBe(0);
expect(notification.options.clickToClose).toBeTruthy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Scale);
})
);
service.set(defaultNotification, true); it('Html content',
inject([NotificationsService], (service: NotificationsService) => {
expect(notificationEvent.command).toBe('set'); const options = new NotificationOptions(
expect(notificationEvent.notification.id !== '0').toBeTruthy(); 10000,
}) false,
); NotificationAnimationsType.Rotate);
const html = '<p>I\'m a mock test</p>';
it('If override id is set its used', const notification = service.success(null, null, options, html);
inject([NotificationsService], (service: NotificationsService) => { expect(notification.id !== undefined).toBeTruthy();
let notificationEvent: NotificationEvent = null, expect(notification.type).toBe(NotificationType.Success);
override = {id: '1'}; expect(notification.title).toBeNull();
expect(notification.content).toBeNull();
service.emitter.subscribe(item => notificationEvent = item); expect(notification.html).not.toBeNull();
expect(notification.options.timeOut).toBe(10000);
service.set(Object.assign(defaultNotification, {override: override}), true); expect(notification.options.clickToClose).toBeFalsy();
expect(notification.options.animate).toBe(NotificationAnimationsType.Rotate);
expect(notificationEvent.notification.id).toBe('1'); })
}) );
);
it('Success method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.success('Title', 'Message');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('success');
expect(notification.icon).toBe(defaultIcons.success);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Error method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.error('Title', 'Message');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('error');
expect(notification.icon).toBe(defaultIcons.error);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Alert method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.alert('Title', 'Message');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('alert');
expect(notification.icon).toBe(defaultIcons.alert);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Info method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.info('Title', 'Message');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('info');
expect(notification.icon).toBe(defaultIcons.info);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Warn method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.warn('Title', 'Message');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('warn');
expect(notification.icon).toBe(defaultIcons.warn);
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Bare method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.bare('Title', 'Message');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('bare');
expect(notification.icon).toBe('bare');
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Create method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.create('Title', 'Message', 'create');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('create');
// expect(notification.icon).toBe('bare');
expect(notification.title).toBe('Title');
expect(notification.content).toBe('Message');
expect(notification.override).toBeUndefined();
expect(notification.html).toBeUndefined();
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Html method',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
let notification: Notification = service.html('<B>Title</B>', 'success');
expect(notification.id !== undefined).toBeTruthy();
expect(notification.type).toBe('success');
expect(notification.icon).toBe('bare');
expect(notification.title).toBeUndefined();
expect(notification.content).toBeUndefined();
expect(notification.override).toBeUndefined();
expect(notification.html).toBe('<B>Title</B>');
expect(notification.state).toBeUndefined();
expect(notification.createdOn).toBeUndefined();
expect(notification.destroyedOn).toBeUndefined();
})
);
it('Empty remove emits cleanAll command',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
service.remove();
expect(notificationEvent.command).toBe('cleanAll');
})
);
it('Remove with id emits clean command',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
service.remove('1');
expect(notificationEvent.command).toBe('clean');
expect(notificationEvent.id).toBe('1');
})
);
}); });

View File

@@ -15,8 +15,7 @@ import { DomSanitizer } from '@angular/platform-browser';
@Injectable() @Injectable()
export class NotificationsService { export class NotificationsService {
constructor(private store: Store<Notification>, constructor(private store: Store<Notification>) {
private domSanitizer: DomSanitizer,) {
} }
private add(notification: Notification) { private add(notification: Notification) {