@@ -96,7 +96,7 @@ describe('CdkDrag', () => {
9696 const fixture = createComponent ( StandaloneDraggable ) ;
9797 fixture . detectChanges ( ) ;
9898
99- const cleanup = makePageScrollable ( ) ;
99+ const cleanup = makeScrollable ( ) ;
100100 const dragElement = fixture . componentInstance . dragElement . nativeElement ;
101101
102102 scrollTo ( 0 , 500 ) ;
@@ -126,7 +126,7 @@ describe('CdkDrag', () => {
126126 fixture . detectChanges ( ) ;
127127
128128 const dragElement = fixture . componentInstance . dragElement . nativeElement ;
129- const cleanup = makePageScrollable ( ) ;
129+ const cleanup = makeScrollable ( ) ;
130130
131131 scrollTo ( 0 , 500 ) ;
132132 expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
@@ -256,7 +256,7 @@ describe('CdkDrag', () => {
256256 fixture . detectChanges ( ) ;
257257
258258 const dragElement = fixture . componentInstance . dragElement . nativeElement ;
259- const cleanup = makePageScrollable ( ) ;
259+ const cleanup = makeScrollable ( ) ;
260260
261261 scrollTo ( 0 , 500 ) ;
262262 expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
@@ -285,7 +285,7 @@ describe('CdkDrag', () => {
285285 fixture . detectChanges ( ) ;
286286
287287 const dragElement = fixture . componentInstance . dragElement . nativeElement ;
288- const cleanup = makePageScrollable ( ) ;
288+ const cleanup = makeScrollable ( ) ;
289289
290290 scrollTo ( 0 , 500 ) ;
291291 expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
@@ -2034,7 +2034,7 @@ describe('CdkDrag', () => {
20342034
20352035 const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
20362036 const list = fixture . componentInstance . dropInstance . element . nativeElement ;
2037- const cleanup = makePageScrollable ( ) ;
2037+ const cleanup = makeScrollable ( ) ;
20382038 scrollTo ( 0 , 10 ) ;
20392039 let listRect = list . getBoundingClientRect ( ) ; // Note that we need to measure after scrolling.
20402040
@@ -2060,6 +2060,43 @@ describe('CdkDrag', () => {
20602060 cleanup ( ) ;
20612061 } ) ) ;
20622062
2063+ it ( 'should update the boundary if a parent is scrolled while dragging' , fakeAsync ( ( ) => {
2064+ const fixture = createComponent ( DraggableInScrollableParentContainer ) ;
2065+ fixture . componentInstance . boundarySelector = '.cdk-drop-list' ;
2066+ fixture . detectChanges ( ) ;
2067+
2068+ const container : HTMLElement = fixture . nativeElement . querySelector ( '.container' ) ;
2069+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
2070+ const list = fixture . componentInstance . dropInstance . element . nativeElement ;
2071+ const cleanup = makeScrollable ( 'vertical' , container ) ;
2072+ container . scrollTop = 10 ;
2073+ let listRect = list . getBoundingClientRect ( ) ; // Note that we need to measure after scrolling.
2074+
2075+ startDraggingViaMouse ( fixture , item ) ;
2076+ startDraggingViaMouse ( fixture , item , listRect . right , listRect . bottom ) ;
2077+ flush ( ) ;
2078+ dispatchMouseEvent ( document , 'mousemove' , listRect . right , listRect . bottom ) ;
2079+ fixture . detectChanges ( ) ;
2080+
2081+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
2082+ let previewRect = preview . getBoundingClientRect ( ) ;
2083+
2084+ // Different browsers round the scroll position differently so
2085+ // assert that the offsets are within a pixel of each other.
2086+ expect ( Math . abs ( previewRect . bottom - listRect . bottom ) ) . toBeLessThan ( 2 ) ;
2087+
2088+ container . scrollTop = 0 ;
2089+ dispatchFakeEvent ( container , 'scroll' ) ;
2090+ fixture . detectChanges ( ) ;
2091+ listRect = list . getBoundingClientRect ( ) ; // We need to update these since we've scrolled.
2092+ dispatchMouseEvent ( document , 'mousemove' , listRect . right , listRect . bottom ) ;
2093+ fixture . detectChanges ( ) ;
2094+ previewRect = preview . getBoundingClientRect ( ) ;
2095+
2096+ expect ( Math . abs ( previewRect . bottom - listRect . bottom ) ) . toBeLessThan ( 2 ) ;
2097+ cleanup ( ) ;
2098+ } ) ) ;
2099+
20632100 it ( 'should clear the id from the preview' , fakeAsync ( ( ) => {
20642101 const fixture = createComponent ( DraggableInDropZone ) ;
20652102 fixture . detectChanges ( ) ;
@@ -2375,7 +2412,7 @@ describe('CdkDrag', () => {
23752412 fakeAsync ( ( ) => {
23762413 const fixture = createComponent ( DraggableInDropZone ) ;
23772414 fixture . detectChanges ( ) ;
2378- const cleanup = makePageScrollable ( ) ;
2415+ const cleanup = makeScrollable ( ) ;
23792416
23802417 scrollTo ( 0 , 500 ) ;
23812418 assertDownwardSorting ( fixture , fixture . componentInstance . dragItems . map ( item => {
@@ -2396,7 +2433,7 @@ describe('CdkDrag', () => {
23962433 fakeAsync ( ( ) => {
23972434 const fixture = createComponent ( DraggableInDropZone ) ;
23982435 fixture . detectChanges ( ) ;
2399- const cleanup = makePageScrollable ( ) ;
2436+ const cleanup = makeScrollable ( ) ;
24002437
24012438 scrollTo ( 0 , 500 ) ;
24022439 assertUpwardSorting ( fixture , fixture . componentInstance . dragItems . map ( item => {
@@ -2893,7 +2930,7 @@ describe('CdkDrag', () => {
28932930 it ( 'should keep the preview next to the trigger if the page was scrolled' , fakeAsync ( ( ) => {
28942931 const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
28952932 fixture . detectChanges ( ) ;
2896- const cleanup = makePageScrollable ( ) ;
2933+ const cleanup = makeScrollable ( ) ;
28972934 const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
28982935
28992936 startDraggingViaMouse ( fixture , item , 50 , 50 ) ;
@@ -3485,7 +3522,7 @@ describe('CdkDrag', () => {
34853522 const fixture = createComponent ( DraggableInDropZone ) ;
34863523 fixture . detectChanges ( ) ;
34873524
3488- const cleanup = makePageScrollable ( ) ;
3525+ const cleanup = makeScrollable ( ) ;
34893526 const item = fixture . componentInstance . dragItems . first . element . nativeElement ;
34903527 const viewportRuler = TestBed . inject ( ViewportRuler ) ;
34913528 const viewportSize = viewportRuler . getViewportSize ( ) ;
@@ -3506,7 +3543,7 @@ describe('CdkDrag', () => {
35063543 const fixture = createComponent ( DraggableInDropZone ) ;
35073544 fixture . detectChanges ( ) ;
35083545
3509- const cleanup = makePageScrollable ( ) ;
3546+ const cleanup = makeScrollable ( ) ;
35103547 const item = fixture . componentInstance . dragItems . first . element . nativeElement ;
35113548 const viewportRuler = TestBed . inject ( ViewportRuler ) ;
35123549 const viewportSize = viewportRuler . getViewportSize ( ) ;
@@ -3529,7 +3566,7 @@ describe('CdkDrag', () => {
35293566 const fixture = createComponent ( DraggableInDropZone ) ;
35303567 fixture . detectChanges ( ) ;
35313568
3532- const cleanup = makePageScrollable ( 'horizontal' ) ;
3569+ const cleanup = makeScrollable ( 'horizontal' ) ;
35333570 const item = fixture . componentInstance . dragItems . first . element . nativeElement ;
35343571 const viewportRuler = TestBed . inject ( ViewportRuler ) ;
35353572 const viewportSize = viewportRuler . getViewportSize ( ) ;
@@ -3550,7 +3587,7 @@ describe('CdkDrag', () => {
35503587 const fixture = createComponent ( DraggableInDropZone ) ;
35513588 fixture . detectChanges ( ) ;
35523589
3553- const cleanup = makePageScrollable ( 'horizontal' ) ;
3590+ const cleanup = makeScrollable ( 'horizontal' ) ;
35543591 const item = fixture . componentInstance . dragItems . first . element . nativeElement ;
35553592 const viewportRuler = TestBed . inject ( ViewportRuler ) ;
35563593 const viewportSize = viewportRuler . getViewportSize ( ) ;
@@ -3587,7 +3624,7 @@ describe('CdkDrag', () => {
35873624 list . style . margin = '0' ;
35883625
35893626 const listRect = list . getBoundingClientRect ( ) ;
3590- const cleanup = makePageScrollable ( ) ;
3627+ const cleanup = makeScrollable ( ) ;
35913628
35923629 scrollTo ( 0 , viewportRuler . getViewportSize ( ) . height * 5 ) ;
35933630 list . scrollTop = 50 ;
@@ -3625,7 +3662,7 @@ describe('CdkDrag', () => {
36253662 list . style . margin = '0' ;
36263663
36273664 const listRect = list . getBoundingClientRect ( ) ;
3628- const cleanup = makePageScrollable ( ) ;
3665+ const cleanup = makeScrollable ( ) ;
36293666
36303667 scrollTo ( 0 , viewportRuler . getViewportSize ( ) . height * 5 ) ;
36313668 list . scrollTop = 0 ;
@@ -4744,7 +4781,7 @@ describe('CdkDrag', () => {
47444781 fixture . detectChanges ( ) ;
47454782
47464783 // Make the page scrollable and scroll the items out of view.
4747- const cleanup = makePageScrollable ( ) ;
4784+ const cleanup = makeScrollable ( ) ;
47484785 scrollTo ( 0 , 4000 ) ;
47494786 dispatchFakeEvent ( document , 'scroll' ) ;
47504787 fixture . detectChanges ( ) ;
@@ -5984,11 +6021,13 @@ function getElementSibligsByPosition(element: Element, direction: 'top' | 'left'
59846021 * Adds a large element to the page in order to make it scrollable.
59856022 * @returns Function that should be used to clean up after the test is done.
59866023 */
5987- function makePageScrollable ( direction : 'vertical' | 'horizontal' = 'vertical' ) {
6024+ function makeScrollable (
6025+ direction : 'vertical' | 'horizontal' = 'vertical' ,
6026+ element = document . body ) {
59886027 const veryTallElement = document . createElement ( 'div' ) ;
59896028 veryTallElement . style . width = direction === 'vertical' ? '100%' : '4000px' ;
59906029 veryTallElement . style . height = direction === 'vertical' ? '2000px' : '5px' ;
5991- document . body . appendChild ( veryTallElement ) ;
6030+ element . appendChild ( veryTallElement ) ;
59926031
59936032 return ( ) => {
59946033 scrollTo ( 0 , 0 ) ;
0 commit comments