|
| 1 | +/* eslint-disable no-param-reassign */ |
1 | 2 | import $ from 'jquery'; |
2 | 3 | import { DateTime } from 'luxon'; |
3 | 4 |
|
@@ -52,6 +53,7 @@ describe('Date picker', () => { |
52 | 53 |
|
53 | 54 | afterEach(() => { |
54 | 55 | document.body.innerHTML = ''; |
| 56 | + window.innerHeight = 768; |
55 | 57 | }); |
56 | 58 |
|
57 | 59 | describe('Initialisation', () => { |
@@ -1085,4 +1087,43 @@ describe('Date picker', () => { |
1085 | 1087 | expect(expectedDate.getFullYear()).toEqual(today.getFullYear()); |
1086 | 1088 | }); |
1087 | 1089 | }); |
| 1090 | + |
| 1091 | + describe('Auto scroll', () => { |
| 1092 | + it('should not attempt to scroll calendar into view if autoScroll is set to false and calendar is not in view', () => { |
| 1093 | + testScrollsCalendarIntoView(false, false, 0); |
| 1094 | + }); |
| 1095 | + |
| 1096 | + it('should not scroll calendar into view if autoScroll is set to true but calendar is already in view', () => { |
| 1097 | + testScrollsCalendarIntoView(true, true, 0); |
| 1098 | + }); |
| 1099 | + |
| 1100 | + it('should scroll calendar into view if autoScroll is set to true and calendar is not in view', () => { |
| 1101 | + testScrollsCalendarIntoView(true, false, 1); |
| 1102 | + }); |
| 1103 | + |
| 1104 | + const testScrollsCalendarIntoView = (isAutoScrollOn, isInView, expectedScrolls) => { |
| 1105 | + datePicker(document.querySelector('.date-picker'), { |
| 1106 | + autoScroll: isAutoScrollOn, |
| 1107 | + }); |
| 1108 | + |
| 1109 | + const revealButton = document.querySelector('.date-picker__reveal'); |
| 1110 | + const calendar = document.querySelector('.date-picker__dialog'); |
| 1111 | + calendar.scrollIntoView = jest.fn(); |
| 1112 | + positionCalendarInViewport(calendar, isInView); |
| 1113 | + |
| 1114 | + $(revealButton).trigger('click'); |
| 1115 | + |
| 1116 | + expect(calendar.scrollIntoView.mock.calls.length).toBe(expectedScrolls); |
| 1117 | + }; |
| 1118 | + |
| 1119 | + const positionCalendarInViewport = (calendar, isInView) => { |
| 1120 | + calendar.getBoundingClientRect = () => ({ |
| 1121 | + bottom: 100, |
| 1122 | + top: 100, |
| 1123 | + left: 100, |
| 1124 | + right: 100, |
| 1125 | + }); |
| 1126 | + window.innerHeight = (isInView === true) ? 110 : 90; |
| 1127 | + }; |
| 1128 | + }); |
1088 | 1129 | }); |
0 commit comments