Skip to content
Snippets Groups Projects
Commit c102dd8b authored by Andrea Chiapparelli - 4Science's avatar Andrea Chiapparelli - 4Science
Browse files

fix test

parent 6397e6fa
Branches
Tags
No related merge requests found
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('Service instantiates',
inject([NotificationsService], (service: NotificationsService) => {
expect(service instanceof NotificationsService).toBeTruthy();
})
);
it('If override is not set, id is randomly generated',
inject([NotificationsService], (service: NotificationsService) => {
let notificationEvent: NotificationEvent = null;
service.emitter.subscribe(item => notificationEvent = item);
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('<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');
})
);
});
it('Default options',
inject([NotificationsService], (service: NotificationsService) => {
const notification = service.success('Title', 'Content');
expect(notification.options.clickToClose).toBe(true);
})
);
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('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);
})
);
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);
})
);
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);
})
);
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);
})
);
});
......@@ -15,8 +15,7 @@ import { DomSanitizer } from '@angular/platform-browser';
@Injectable()
export class NotificationsService {
constructor(private store: Store<Notification>,
private domSanitizer: DomSanitizer,) {
constructor(private store: Store<Notification>) {
}
private add(notification: Notification) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment