diff --git a/src/app/shared/notifications/notifications.service.spec.ts b/src/app/shared/notifications/notifications.service.spec.ts index 12bf738b03..6dca013b6e 100644 --- a/src/app/shared/notifications/notifications.service.spec.ts +++ b/src/app/shared/notifications/notifications.service.spec.ts @@ -1,263 +1,104 @@ -import {inject, TestBed} from '@angular/core/testing'; -import {NotificationsService} from './notifications.service'; -import {defaultIcons} from '../interfaces/icons'; -import {NotificationEvent} from '../interfaces/notification-event.type'; -import {Notification} from '../interfaces/notification.type'; +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 { StoreModule } from '@ngrx/store'; +import { notificationsReducer } from './notifications.reducers'; describe('NotificationsService', () => { - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [], - providers: [NotificationsService], - }); + beforeEach(async () => { + TestBed.configureTestingModule({ + declarations: [NotificationComponent, NotificationsBoardComponent], + providers: [NotificationsService], + imports: [ + StoreModule.forRoot({notificationsReducer}), + ] }); + }); - let defaultNotification = { - id: '0', - title: 'Test title', - type: 'success', - 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('Default options', + inject([NotificationsService], (service: NotificationsService) => { + const notification = service.success('Title', 'Content'); + expect(notification.options.clickToClose).toBe(true); + }) + ); - it('Service instantiates', - inject([NotificationsService], (service: NotificationsService) => { - expect(service instanceof NotificationsService).toBeTruthy(); - }) - ); + 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('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', - inject([NotificationsService], (service: NotificationsService) => { + it('Error method', + 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); - - expect(notificationEvent.command).toBe('set'); - expect(notificationEvent.notification.id !== '0').toBeTruthy(); - }) - ); - - it('If override id is set its used', - inject([NotificationsService], (service: NotificationsService) => { - let notificationEvent: NotificationEvent = null, - override = {id: '1'}; - - service.emitter.subscribe(item => notificationEvent = item); - - service.set(Object.assign(defaultNotification, {override: override}), true); - - 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('Title', '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('Title'); - 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'); - }) - ); + it('Html content', + inject([NotificationsService], (service: NotificationsService) => { + const options = new NotificationOptions( + 10000, + false, + NotificationAnimationsType.Rotate); + const html = '

I\'m a mock test

'; + 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); + }) + ); }); diff --git a/src/app/shared/notifications/notifications.service.ts b/src/app/shared/notifications/notifications.service.ts index 8cae9506e4..cbebf01fbd 100644 --- a/src/app/shared/notifications/notifications.service.ts +++ b/src/app/shared/notifications/notifications.service.ts @@ -15,8 +15,7 @@ import { DomSanitizer } from '@angular/platform-browser'; @Injectable() export class NotificationsService { - constructor(private store: Store, - private domSanitizer: DomSanitizer,) { + constructor(private store: Store) { } private add(notification: Notification) {