diff --git a/src/app/shared/notifications/notification/notification.component.spec.ts b/src/app/shared/notifications/notification/notification.component.spec.ts index 7b7ee57d26..2bded57636 100644 --- a/src/app/shared/notifications/notification/notification.component.spec.ts +++ b/src/app/shared/notifications/notification/notification.component.spec.ts @@ -1,4 +1,4 @@ -import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing'; import { BrowserModule, By } from '@angular/platform-browser'; import { ChangeDetectorRef, DebugElement } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @@ -16,6 +16,7 @@ import { Notification } from '../models/notification.model'; import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core'; import { TranslateLoaderMock } from '../../mocks/translate-loader.mock'; import { storeModuleConfig } from '../../../app.reducer'; +import { BehaviorSubject } from 'rxjs'; describe('NotificationComponent', () => { @@ -83,6 +84,8 @@ describe('NotificationComponent', () => { deContent = fixture.debugElement.query(By.css('.notification-content')); elContent = deContent.nativeElement; elType = fixture.debugElement.query(By.css('.notification-icon')).nativeElement; + + spyOn(comp, 'remove'); }); it('should create component', () => { @@ -124,4 +127,51 @@ describe('NotificationComponent', () => { expect(elContent.innerHTML).toEqual(htmlContent); }); + describe('dismiss countdown', () => { + const TIMEOUT = 5000; + let isPaused$: BehaviorSubject; + + beforeEach(() => { + isPaused$ = new BehaviorSubject(false); + comp.isPaused$ = isPaused$; + comp.notification = { + id: '1', + type: NotificationType.Info, + title: 'Notif. title', + content: 'test', + options: Object.assign( + new NotificationOptions(), + { timeout: TIMEOUT } + ), + html: true + }; + }); + + it('should remove notification after timeout', fakeAsync(() => { + comp.ngOnInit(); + tick(TIMEOUT); + expect(comp.remove).toHaveBeenCalled(); + })); + + describe('isPaused$', () => { + it('should pause countdown on true', fakeAsync(() => { + comp.ngOnInit(); + tick(TIMEOUT / 2); + isPaused$.next(true); + tick(TIMEOUT); + expect(comp.remove).not.toHaveBeenCalled(); + })); + + it('should resume paused countdown on false', fakeAsync(() => { + comp.ngOnInit(); + tick(TIMEOUT / 4); + isPaused$.next(true); + tick(TIMEOUT / 4); + isPaused$.next(false); + tick(TIMEOUT); + expect(comp.remove).toHaveBeenCalled(); + })); + }); + }); + }); diff --git a/src/app/shared/notifications/notifications-board/notifications-board.component.spec.ts b/src/app/shared/notifications/notifications-board/notifications-board.component.spec.ts index dad667cf3d..1d3faabdaa 100644 --- a/src/app/shared/notifications/notifications-board/notifications-board.component.spec.ts +++ b/src/app/shared/notifications/notifications-board/notifications-board.component.spec.ts @@ -1,5 +1,5 @@ import { ComponentFixture, inject, TestBed, waitForAsync } from '@angular/core/testing'; -import { BrowserModule } from '@angular/platform-browser'; +import { BrowserModule, By } from '@angular/platform-browser'; import { ChangeDetectorRef } from '@angular/core'; import { NotificationsService } from '../notifications.service'; @@ -14,6 +14,9 @@ import { NotificationType } from '../models/notification-type'; import { uniqueId } from 'lodash'; import { INotificationBoardOptions } from '../../../../config/notifications-config.interfaces'; import { NotificationsServiceStub } from '../../testing/notifications-service.stub'; +import { cold } from 'jasmine-marbles'; + +export const bools = { f: false, t: true }; describe('NotificationsBoardComponent', () => { let comp: NotificationsBoardComponent; @@ -67,6 +70,40 @@ describe('NotificationsBoardComponent', () => { it('should have two notifications', () => { expect(comp.notifications.length).toBe(2); + expect(fixture.debugElement.queryAll(By.css('ds-notification')).length).toBe(2); + }); + + describe('notification countdown', () => { + let wrapper; + + beforeEach(() => { + wrapper = fixture.debugElement.query(By.css('div.notifications-wrapper')); + }); + + it('should not be paused by default', () => { + expect(comp.isPaused$).toBeObservable(cold('f', bools)); + }); + + it('should pause on mouseenter', () => { + wrapper.triggerEventHandler('mouseenter'); + + expect(comp.isPaused$).toBeObservable(cold('t', bools)); + }); + + it('should resume on mouseleave', () => { + wrapper.triggerEventHandler('mouseenter'); + wrapper.triggerEventHandler('mouseleave'); + + expect(comp.isPaused$).toBeObservable(cold('f', bools)); + }); + + it('should be passed to all notifications', () => { + fixture.debugElement.queryAll(By.css('ds-notification')) + .map(node => node.componentInstance) + .forEach(notification => { + expect(notification.isPaused$).toEqual(comp.isPaused$); + }); + }); }); })