import 'reflect-metadata'; import { requiredMocks, } from './../../../../test-mocks'; requiredMocks(jest); import { ElementRef, } from '@angular/core'; import { DomElementLocationService, } from './dom-element-location.service'; const initDomElementLocationService = ( window?: Window, ) => { return new DomElementLocationService( window, ); }; describe('getCenterPointOfElement', () => { // tslint:disable-next-line test('It returns the top of the element plus half the height', () => { const domElementLocationService = initDomElementLocationService(); const elementRef = { nativeElement: {}, } as ElementRef; elementRef.nativeElement.getBoundingClientRect = jest.fn().mockReturnValue({ height: 50, top: 10, }); expect( domElementLocationService .getCenterPointOfElement( elementRef, ), ).toBe(35); }); }); describe('getMostCentralElement', () => { // tslint:disable-next-line test('If there is just one element it returns that element', () => { const window = { innerHeight: 100, } as Window; const domElementLocationService = initDomElementLocationService( window, ); const elementRefOne = {} as ElementRef; domElementLocationService.getCenterPointOfElement = jest.fn().mockReturnValue(0); const result = domElementLocationService.getMostCentralElement( [ elementRefOne, ], ); expect(result.mostCentralIndex).toBe(0); expect(result.mostCentral).toBe( elementRefOne, ); }); // tslint:disable-next-line test('It returns the element with the center point closest to the window center point', () => { const window = { innerHeight: 100, } as Window; const domElementLocationService = initDomElementLocationService( window, ); const elementRefOne = {} as ElementRef; const elementRefTwo = {} as ElementRef; const elementRefThree = {} as ElementRef; domElementLocationService.getCenterPointOfElement = jest.fn().mockImplementation((elementRef) => { if (elementRef === elementRefOne) { return 0; } else if (elementRef === elementRefTwo) { return 50; } return 100; }); const result = domElementLocationService.getMostCentralElement( [ elementRefOne, elementRefTwo, elementRefThree, ], ); expect(result.mostCentralIndex).toBe(1); expect(result.mostCentral).toBe( elementRefTwo, ); }); });