@@ -17,6 +17,20 @@ function waitUntilScrolledTo(overlay, date, callback) {
1717 }
1818}
1919
20+ async function customizeFixture ( { initialPosition, monthScrollerItems, monthScrollerOffset } ) {
21+ const overlay = fixtureSync ( `<vaadin-date-picker-overlay-content></vaadin-date-picker-overlay-content>` ) ;
22+ const monthScroller = overlay . $ . monthScroller ;
23+ monthScroller . style . setProperty ( '--vaadin-infinite-scroller-buffer-offset' , monthScrollerOffset ) ;
24+ monthScroller . style . height = `${ 270 * monthScrollerItems } px` ;
25+ overlay . i18n = getDefaultI18n ( ) ;
26+ overlay . $ . monthScroller . bufferSize = 3 ;
27+ overlay . $ . yearScroller . bufferSize = 3 ;
28+ overlay . initialPosition = initialPosition || new Date ( ) ;
29+ await nextRender ( ) ;
30+
31+ return overlay ;
32+ }
33+
2034describe ( 'overlay' , ( ) => {
2135 let overlay ;
2236
@@ -315,58 +329,123 @@ describe('overlay', () => {
315329 overlay . $ . yearScroller . _debouncerUpdateClones . flush ( ) ;
316330 expect ( getFirstVisibleItem ( overlay . $ . yearScroller , offset ) . firstElementChild . textContent ) . to . contain ( '2000' ) ;
317331 } ) ;
332+
333+ describe ( 'height(visible area) < height(item)' , ( ) => {
334+ let overlay , monthScroller ;
335+
336+ beforeEach ( async ( ) => {
337+ overlay = await customizeFixture ( {
338+ initialPosition : new Date ( 2021 , 1 , 1 ) ,
339+ monthScrollerItems : 0.5 ,
340+ monthScrollerOffset : 0 ,
341+ } ) ;
342+ monthScroller = overlay . $ . monthScroller ;
343+ } ) ;
344+
345+ it ( 'should scroll to a sub-month position that approximately shows the week the date is in' , ( ) => {
346+ const initialPosition = monthScroller . position ;
347+ // Scroll to 15th
348+ overlay . scrollToDate ( new Date ( 2021 , 1 , 15 ) , false ) ;
349+ const positionOf15th = monthScroller . position ;
350+ expect ( positionOf15th ) . to . be . greaterThan ( initialPosition ) ;
351+ expect ( positionOf15th ) . to . be . lessThan ( initialPosition + 1 ) ;
352+ // Scroll to 28th
353+ overlay . scrollToDate ( new Date ( 2021 , 1 , 28 ) , false ) ;
354+ const positionOf28th = monthScroller . position ;
355+ expect ( positionOf28th ) . to . be . greaterThan ( initialPosition ) ;
356+ expect ( positionOf28th ) . to . be . greaterThan ( positionOf15th ) ;
357+ expect ( positionOf28th ) . to . be . lessThan ( initialPosition + 1 ) ;
358+ // Scroll to first of previous month
359+ overlay . scrollToDate ( new Date ( 2021 , 0 , 1 ) , false ) ;
360+ const firstOfPreviousMonthPosition = monthScroller . position ;
361+ expect ( firstOfPreviousMonthPosition ) . to . equal ( initialPosition - 1 ) ;
362+ // Scroll to first of next month
363+ overlay . scrollToDate ( new Date ( 2021 , 2 , 1 ) , false ) ;
364+ const firstOfNextMonthPosition = monthScroller . position ;
365+ expect ( firstOfNextMonthPosition ) . to . equal ( initialPosition + 1 ) ;
366+ } ) ;
367+ } ) ;
368+
369+ describe ( 'height(visible area) > height(item)' , ( ) => {
370+ let overlay , monthScroller ;
371+
372+ beforeEach ( async ( ) => {
373+ overlay = await customizeFixture ( {
374+ initialPosition : new Date ( 2021 , 1 , 1 ) ,
375+ monthScrollerItems : 3 ,
376+ monthScrollerOffset : 0 ,
377+ } ) ;
378+ monthScroller = overlay . $ . monthScroller ;
379+ } ) ;
380+
381+ it ( 'should always scroll to the exact position of the month that the date is in' , ( ) => {
382+ const initialPosition = monthScroller . position ;
383+ // Scroll to 15th
384+ overlay . scrollToDate ( new Date ( 2021 , 1 , 15 ) , false ) ;
385+ const positionOf15th = monthScroller . position ;
386+ expect ( positionOf15th ) . to . equal ( initialPosition ) ;
387+ // Scroll to 28th
388+ overlay . scrollToDate ( new Date ( 2021 , 1 , 28 ) , false ) ;
389+ const positionOf28th = monthScroller . position ;
390+ expect ( positionOf28th ) . to . equal ( initialPosition ) ;
391+ // Scroll to first of previous month
392+ overlay . scrollToDate ( new Date ( 2021 , 0 , 1 ) , false ) ;
393+ const firstOfPreviousMonthPosition = monthScroller . position ;
394+ expect ( firstOfPreviousMonthPosition ) . to . equal ( initialPosition - 1 ) ;
395+ // Scroll to first of next month
396+ overlay . scrollToDate ( new Date ( 2021 , 2 , 1 ) , false ) ;
397+ const firstOfNextMonthPosition = monthScroller . position ;
398+ expect ( firstOfNextMonthPosition ) . to . equal ( initialPosition + 1 ) ;
399+ } ) ;
400+ } ) ;
318401 } ) ;
319402
320403 describe ( 'revealDate' , ( ) => {
321404 let overlay , monthScroller ;
322405
323- async function fixtureOverlayContent ( { monthScrollerItems, monthScrollerOffset } ) {
324- overlay = fixtureSync ( `
325- <vaadin-date-picker-overlay-content></vaadin-date-picker-overlay-content>
326- ` ) ;
327- monthScroller = overlay . $ . monthScroller ;
328- monthScroller . style . setProperty ( '--vaadin-infinite-scroller-buffer-offset' , monthScrollerOffset ) ;
329- monthScroller . style . height = `calc(var(--vaadin-infinite-scroller-item-height) * ${ monthScrollerItems } )` ;
330- overlay . i18n = getDefaultI18n ( ) ;
331- overlay . $ . monthScroller . bufferSize = 3 ;
332- overlay . $ . yearScroller . bufferSize = 3 ;
333- overlay . initialPosition = new Date ( 2021 , 1 , 1 ) ;
334- await nextRender ( ) ;
335- }
336-
337406 describe ( 'height(visible area) < height(item)' , ( ) => {
338407 beforeEach ( async ( ) => {
339- await fixtureOverlayContent ( {
408+ overlay = await customizeFixture ( {
409+ initialPosition : new Date ( 2021 , 1 , 1 ) ,
340410 monthScrollerItems : 0.5 ,
341411 monthScrollerOffset : 0 ,
342412 } ) ;
413+ monthScroller = overlay . $ . monthScroller ;
343414 } ) ;
344415
345- it ( 'should scroll when the month is above the visible area' , ( ) => {
346- const position = monthScroller . position ;
416+ it ( 'should scroll to a position that approximately shows the week the date is in' , ( ) => {
417+ // Starting on first of February
418+ const initialPosition = monthScroller . position ;
419+ // Scroll to 15th
420+ overlay . revealDate ( new Date ( 2021 , 1 , 15 ) , false ) ;
421+ const positionOf15th = monthScroller . position ;
422+ expect ( positionOf15th ) . to . be . greaterThan ( initialPosition ) ;
423+ expect ( positionOf15th ) . to . be . lessThan ( initialPosition + 1 ) ;
424+ // Scroll to 28th
425+ overlay . revealDate ( new Date ( 2021 , 1 , 28 ) , false ) ;
426+ const positionOf28th = monthScroller . position ;
427+ expect ( positionOf28th ) . to . be . greaterThan ( initialPosition ) ;
428+ expect ( positionOf28th ) . to . be . greaterThan ( positionOf15th ) ;
429+ expect ( positionOf28th ) . to . be . lessThan ( initialPosition + 1 ) ;
430+ // Scroll to first of previous month
347431 overlay . revealDate ( new Date ( 2021 , 0 , 1 ) , false ) ;
348- expect ( monthScroller . position ) . to . equal ( position - 1 ) ;
349- } ) ;
350-
351- it ( 'should not scroll when the month is the same' , ( ) => {
352- const position = monthScroller . position ;
353- overlay . revealDate ( new Date ( 2021 , 1 , 10 ) , false ) ;
354- expect ( monthScroller . position ) . to . equal ( position ) ;
355- } ) ;
356-
357- it ( 'should scroll when the month is below the visible area' , ( ) => {
358- const position = monthScroller . position ;
432+ const firstOfPreviousMonthPosition = monthScroller . position ;
433+ expect ( firstOfPreviousMonthPosition ) . to . equal ( initialPosition - 1 ) ;
434+ // Scroll to first of next month
359435 overlay . revealDate ( new Date ( 2021 , 2 , 1 ) , false ) ;
360- expect ( monthScroller . position ) . to . equal ( position + 1 ) ;
436+ const firstOfNextMonthPosition = monthScroller . position ;
437+ expect ( firstOfNextMonthPosition ) . to . equal ( initialPosition + 1 ) ;
361438 } ) ;
362439 } ) ;
363440
364441 describe ( 'height(visible area) > height(item)' , ( ) => {
365442 beforeEach ( async ( ) => {
366- await fixtureOverlayContent ( {
443+ overlay = await customizeFixture ( {
444+ initialPosition : new Date ( 2021 , 1 , 1 ) ,
367445 monthScrollerItems : 2 ,
368446 monthScrollerOffset : 0 ,
369447 } ) ;
448+ monthScroller = overlay . $ . monthScroller ;
370449 } ) ;
371450
372451 it ( 'should scroll when the month is above the visible area' , ( ) => {
@@ -390,10 +469,12 @@ describe('overlay', () => {
390469
391470 describe ( 'offset' , ( ) => {
392471 beforeEach ( async ( ) => {
393- await fixtureOverlayContent ( {
472+ overlay = await customizeFixture ( {
473+ initialPosition : new Date ( 2021 , 1 , 1 ) ,
394474 monthScrollerItems : 3 ,
395475 monthScrollerOffset : '10%' ,
396476 } ) ;
477+ monthScroller = overlay . $ . monthScroller ;
397478 } ) ;
398479
399480 it ( 'should scroll when the month is above the visible area' , ( ) => {
0 commit comments