<hr>
- <div *ngFor="let notification of notifications">
+ <div *ngFor="let notification of notifications; let i = index">
<div class="card border-0 mb-3">
<div class="row no-gutters">
<div class="col-md-3 text-center">
</div>
<div class="col-md-9">
<div class="card-body p-0">
+ <button class="btn btn-link float-right mt-0 pt-0"
+ title="Remove notification"
+ i18n-title
+ (click)="remove(i); $event.stopPropagation()">
+ <i [ngClass]="[icons.trash]"></i>
+ </button>
+
<h6 class="card-title bold">{{ notification.title }}</h6>
<p class="card-text"
[innerHtml]="notification.message"></p>
});
};
+ const addNotifications = (quantity) => {
+ for (let index = 0; index < quantity; index++) {
+ service.show(NotificationType.info, `${index}`);
+ tick(510);
+ }
+ };
+
beforeEach(() => {
spyOn(service, 'show').and.callThrough();
service.cancel(service['justShownTimeoutId']);
}));
it('should never have more then 10 notifications', fakeAsync(() => {
- for (let index = 0; index < 15; index++) {
- service.show(NotificationType.info, 'Simple test');
- tick(510);
- }
+ addNotifications(15);
expect(service['dataSource'].getValue().length).toBe(10);
}));
message: '<ul><li>Error occurred in path a</li><li>Error occurred in path b</li></ul>'
});
}));
+
+ it('should remove a single notification', fakeAsync(() => {
+ addNotifications(5);
+ let messages = service['dataSource'].getValue().map((notification) => notification.title);
+ expect(messages).toEqual(['4', '3', '2', '1', '0']);
+ service.remove(2);
+ messages = service['dataSource'].getValue().map((notification) => notification.title);
+ expect(messages).toEqual(['4', '3', '1', '0']);
+ }));
+
+ it('should remove all notifications', fakeAsync(() => {
+ addNotifications(5);
+ expect(service['dataSource'].getValue().length).toBe(5);
+ service.removeAll();
+ expect(service['dataSource'].getValue().length).toBe(0);
+ }));
});
describe('notification queue', () => {
this.dataSource.next([]);
}
+ /**
+ * Removes a single saved notifications
+ */
+ remove(index: number) {
+ const recent = this.dataSource.getValue();
+ recent.splice(index, 1);
+ this.dataSource.next(recent);
+ localStorage.setItem(this.KEY, JSON.stringify(recent));
+ }
+
/**
* Method used for saving a shown notification (check show() method).
*/
save(notification: CdNotification) {
const recent = this.dataSource.getValue();
recent.push(notification);
+ recent.sort((a, b) => (a.timestamp > b.timestamp ? -1 : 1));
while (recent.length > 10) {
- recent.shift();
+ recent.pop();
}
this.dataSource.next(recent);
localStorage.setItem(this.KEY, JSON.stringify(recent));