Skip to content

Commit a885d0c

Browse files
committed
test(scrollpoint): test scroll behaviour and custom scroll points
1 parent 1a8660b commit a885d0c

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

projects/ng-sortgrid/src/lib/helpers/scroll-helper.service.spec.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Scroll helper', () => {
1010
innerWidth: 0,
1111
scrollBy: () => {
1212
}
13-
}
13+
} as any
1414
};
1515
let scrollSpy: SpyObj<any>;
1616

@@ -21,57 +21,65 @@ describe('Scroll helper', () => {
2121

2222
describe('Top scroll', () => {
2323

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);
24+
it(`should scroll to the top with the default scroll speed when we drag over
25+
the top viewport + scroll buffer`, () => {
26+
documentMock.defaultView.scrollY = 0;
27+
const event = {
28+
pageY: 40
29+
};
30+
sut.scrollIfNecessary(event);
2931
expect(scrollSpy).toHaveBeenCalledWith({top: -50, behavior: 'smooth'});
3032
});
3133

3234
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});
35+
documentMock.defaultView.scrollY = 0;
36+
const event = {
37+
pageY: 110
38+
};
39+
sut.scrollIfNecessary(event, {top: 140});
3740
expect(scrollSpy).toHaveBeenCalledWith({top: -50, behavior: 'smooth'});
3841
});
3942

4043
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+
documentMock.defaultView.scrollY = 0;
45+
const event = {
46+
pageY: 40
47+
};
4448
const scrollSpeed = 100;
45-
sut.scrollIfNecessary(element, {}, scrollSpeed);
49+
sut.scrollIfNecessary(event, {}, scrollSpeed);
4650
expect(scrollSpy).toHaveBeenCalledWith({top: -scrollSpeed, behavior: 'smooth'});
4751
});
4852

4953
});
5054

5155
describe('Bottom scroll', () => {
5256

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);
57+
it('should scroll to the bottom with the default scroll speed when we drag over the bottom viewport - scroll buffer', () => {
58+
documentMock.defaultView.scrollY = 0;
59+
documentMock.defaultView.innerHeight = 100;
60+
const event = {
61+
pageY: 80
62+
};
63+
sut.scrollIfNecessary(event);
5864
expect(scrollSpy).toHaveBeenCalledWith({top: 50, behavior: 'smooth'});
5965
});
6066

6167
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});
68+
documentMock.defaultView.scrollY = 0;
69+
const event = {
70+
pageY: 141
71+
};
72+
sut.scrollIfNecessary(event, {bottom: 140});
6673
expect(scrollSpy).toHaveBeenCalledWith({top: 50, behavior: 'smooth'});
6774
});
6875

6976
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;
77+
documentMock.defaultView.scrollY = 0;
78+
const event = {
79+
pageY: 110
80+
};
7381
const scrollSpeed = 100;
74-
sut.scrollIfNecessary(element, {}, scrollSpeed);
82+
sut.scrollIfNecessary(event, {}, scrollSpeed);
7583
expect(scrollSpy).toHaveBeenCalledWith({top: scrollSpeed, behavior: 'smooth'});
7684
});
7785

projects/ng-sortgrid/src/lib/helpers/scroll-helper.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ export class ScrollHelperService {
3333
}
3434

3535
private isTopScrollNeeded(currentPosition: number, scrollPointTop: number): boolean {
36-
return scrollPointTop ? currentPosition < scrollPointTop + this.SCROLL_BUFFER : currentPosition < 20;
36+
return scrollPointTop ? currentPosition < scrollPointTop :
37+
currentPosition < this.SCROLL_BUFFER;
3738
}
3839

3940
private isBottomScrollNeeded(currentPosition: number, scrollPointBottom: number): boolean {
40-
return scrollPointBottom ?
41-
currentPosition < scrollPointBottom : currentPosition > this.window.innerHeight - this.SCROLL_BUFFER;
41+
return scrollPointBottom ? currentPosition > scrollPointBottom :
42+
currentPosition > this.window.innerHeight - this.SCROLL_BUFFER;
4243
}
4344
}

projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {NgsgItemDirective} from './ngsg-item.directive';
2-
import createSpyObj = jasmine.createSpyObj;
32

43
import {NgsgSortService} from './ngsg-sort.service';
54
import {NgsgSelectionService} from './ngsg-selection.service';
65
import {NgsgReflectService} from './ngsg-reflect.service';
76
import {NgsgStoreService} from './ngsg-store.service';
87
import {NgsgEventsService} from './ngsg-events.service';
8+
import createSpyObj = jasmine.createSpyObj;
99
import createSpy = jasmine.createSpy;
1010

1111
describe('NgsgItemDirective', () => {
@@ -220,18 +220,32 @@ describe('NgsgItemDirective', () => {
220220
expect(consoleWarnSpy).toHaveBeenCalledWith(expectedWarniningMessage);
221221
});
222222

223-
it('should call the scrollHelper once we drag', () => {
224-
const event = {
225-
target: 'Some target'
226-
};
227-
const scrollPointTop = 20;
228-
const scrollSpeed = 50;
229-
sut.scrollPointTop = scrollPointTop;
230-
sut.scrollSpeed = scrollSpeed;
231-
spyOn(scrollHelperService, 'scrollIfNecessary');
232-
233-
sut.drag(event);
234-
expect(scrollHelperService.scrollIfNecessary)
235-
.toHaveBeenCalledWith(event.target, {top: scrollPointTop}, scrollSpeed);
223+
describe('Drag', () => {
224+
225+
it('should not call the scrollHelper if autoScroll is set to false', () => {
226+
spyOn(scrollHelperService, 'scrollIfNecessary');
227+
sut.drag({});
228+
expect(scrollHelperService.scrollIfNecessary).not.toHaveBeenCalled();
229+
});
230+
231+
it('should call the scrollHelper with the event, the scrollpoints and the scrollspeed', () => {
232+
spyOn(scrollHelperService, 'scrollIfNecessary');
233+
const event = 'A very cool event';
234+
const scrollPointTop = 10;
235+
const scrollPointBottom = 80;
236+
const scrollSpeed = 100;
237+
238+
sut.scrollPointTop = scrollPointTop;
239+
sut.scrollPointBottom = scrollPointBottom;
240+
sut.scrollSpeed = scrollSpeed;
241+
sut.autoScroll = true;
242+
243+
sut.drag(event);
244+
expect(scrollHelperService.scrollIfNecessary).toHaveBeenCalledWith(event, {
245+
top: scrollPointTop,
246+
bottom: scrollPointBottom
247+
}, scrollSpeed);
248+
});
249+
236250
});
237251
});

0 commit comments

Comments
 (0)