|
| 1 | +import {ScrollHelperService} from './scroll-helper.service'; |
| 2 | +import SpyObj = jasmine.SpyObj; |
| 3 | + |
| 4 | +describe('Scroll helper', () => { |
| 5 | + |
| 6 | + let sut: ScrollHelperService; |
| 7 | + const documentMock = { |
| 8 | + defaultView: { |
| 9 | + innerHeight: 0, |
| 10 | + innerWidth: 0, |
| 11 | + scrollBy: () => { |
| 12 | + } |
| 13 | + } |
| 14 | + }; |
| 15 | + let scrollSpy: SpyObj<any>; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + sut = new ScrollHelperService(documentMock); |
| 19 | + scrollSpy = spyOn(documentMock.defaultView, 'scrollBy'); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('Top scroll', () => { |
| 23 | + |
| 24 | + it('should scroll to the top with the default scroll speed when we drag over the top viewport', () => { |
| 25 | + const element = { |
| 26 | + getBoundingClientRect: () => ({top: -100, left: 0, bottom: 0, right: 0}) |
| 27 | + } as any; |
| 28 | + sut.scrollIfNecessary(element); |
| 29 | + expect(scrollSpy).toHaveBeenCalledWith({top: -50, behavior: 'smooth'}); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should scroll to the top with the default scroll speed when we drag over the top scroll position', () => { |
| 33 | + const element = { |
| 34 | + getBoundingClientRect: () => ({top: 120, left: 0, bottom: 0, right: 0}) |
| 35 | + } as any; |
| 36 | + sut.scrollIfNecessary(element, {top: 140}); |
| 37 | + expect(scrollSpy).toHaveBeenCalledWith({top: -50, behavior: 'smooth'}); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should scroll to the top with the custom scroll speed when we drag over the top viewport', () => { |
| 41 | + const element = { |
| 42 | + getBoundingClientRect: () => ({top: -100, left: 0, bottom: 0, right: 0}) |
| 43 | + } as any; |
| 44 | + const scrollSpeed = 100; |
| 45 | + sut.scrollIfNecessary(element, {}, scrollSpeed); |
| 46 | + expect(scrollSpy).toHaveBeenCalledWith({top: -scrollSpeed, behavior: 'smooth'}); |
| 47 | + }); |
| 48 | + |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Bottom scroll', () => { |
| 52 | + |
| 53 | + it('should scroll to the bottom with the default scroll speed when we drag over the bottom viewport', () => { |
| 54 | + const element = { |
| 55 | + getBoundingClientRect: () => ({top: 100, left: 0, bottom: 20, right: 0}) |
| 56 | + } as any; |
| 57 | + sut.scrollIfNecessary(element); |
| 58 | + expect(scrollSpy).toHaveBeenCalledWith({top: 50, behavior: 'smooth'}); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should scroll to the bottom with the default scroll speed when we drag over the bottom scroll position', () => { |
| 62 | + const element = { |
| 63 | + getBoundingClientRect: () => ({top: 120, left: 0, bottom: 200, right: 0}) |
| 64 | + } as any; |
| 65 | + sut.scrollIfNecessary(element, {bottom: 140}); |
| 66 | + expect(scrollSpy).toHaveBeenCalledWith({top: 50, behavior: 'smooth'}); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should scroll to the top with the custom scroll speed when we drag over the top viewport', () => { |
| 70 | + const element = { |
| 71 | + getBoundingClientRect: () => ({top: 20, left: 0, bottom: 20, right: 0}) |
| 72 | + } as any; |
| 73 | + const scrollSpeed = 100; |
| 74 | + sut.scrollIfNecessary(element, {}, scrollSpeed); |
| 75 | + expect(scrollSpy).toHaveBeenCalledWith({top: scrollSpeed, behavior: 'smooth'}); |
| 76 | + }); |
| 77 | + |
| 78 | + }); |
| 79 | +}); |
0 commit comments