import 'reflect-metadata'; import { requiredMocks, } from './../../../../test-mocks'; requiredMocks(jest); import { ElementRef, } from '@angular/core'; import { FixedButXScrollableDirective, } from './fixed-but-x-scrollable.directive'; const initFixedButXScrollable = ( document?: Document, window?: Window, elementRef?: ElementRef, ) => { return new FixedButXScrollableDirective( document, window, elementRef, ); }; describe('ngOnInit', () => { // tslint:disable-next-line test('It sets the document scroll event to the scroll event', () => { const document = {} as Document; const addEventListenerMock = jest.fn(); document.addEventListener = addEventListenerMock; const fixedButXScrollableDirective = initFixedButXScrollable( document, ); fixedButXScrollableDirective.ngOnInit(); expect( addEventListenerMock.mock.calls[0][0], ).toBe('scroll'); }); // tslint:disable-next-line test('On document scroll it sets the left position of the elementRef to the window scroll position', () => { const document = {} as Document; const addEventListenerMock = jest.fn(); document.addEventListener = addEventListenerMock; const elementRef = { nativeElement: { style: { transform: 'translateX(100px)', }, }, } as ElementRef; const window = { scrollX: 1000, } as Window; const fixedButXScrollableDirective = initFixedButXScrollable( document, window, elementRef, ); fixedButXScrollableDirective.ngOnInit(); addEventListenerMock.mock.calls[0][1](); expect( elementRef.nativeElement.style .transform, ).toBe( 'translateX(-1000px)', ); }); }); describe('ngOnDestroy', () => { // tslint:disable-next-line test('It removes the scroll event from the document', () => { const document = {} as Document; document.removeEventListener = jest.fn(); const fixedButXScrollableDirective = initFixedButXScrollable( document, ); fixedButXScrollableDirective.scrollEvent = () => { return undefined; }; fixedButXScrollableDirective.ngOnDestroy(); expect( document.removeEventListener, ).toHaveBeenCalledWith( 'scroll', fixedButXScrollableDirective.scrollEvent, ); }); });