@@ -37,6 +37,20 @@ import {CdkDrag} from './drag';
37
37
import { CdkDragHandle } from './drag-handle' ;
38
38
import { CdkDropList } from './drop-list' ;
39
39
import { CdkDropListGroup } from './drop-list-group' ;
40
+ import {
41
+ assertDownwardSorting ,
42
+ assertUpwardSorting ,
43
+ continueDraggingViaTouch ,
44
+ dragElementViaMouse ,
45
+ dragElementViaTouch ,
46
+ getElementIndexByPosition ,
47
+ getElementSibligsByPosition ,
48
+ makeScrollable ,
49
+ startDraggingViaMouse ,
50
+ startDraggingViaTouch ,
51
+ stopDraggingViaTouch ,
52
+ tickAnimationFrames ,
53
+ } from './test-utils.spec' ;
40
54
41
55
const ITEM_HEIGHT = 25 ;
42
56
const ITEM_WIDTH = 75 ;
@@ -8501,201 +8515,3 @@ class DraggableNgContainerWithAlternateRoot {
8501
8515
@ViewChild ( 'dragRoot' ) dragRoot : ElementRef < HTMLElement > ;
8502
8516
@ViewChild ( CdkDrag ) dragInstance : CdkDrag ;
8503
8517
}
8504
-
8505
- /**
8506
- * Drags an element to a position on the page using the mouse.
8507
- * @param fixture Fixture on which to run change detection.
8508
- * @param element Element which is being dragged.
8509
- * @param x Position along the x axis to which to drag the element.
8510
- * @param y Position along the y axis to which to drag the element.
8511
- */
8512
- function dragElementViaMouse (
8513
- fixture : ComponentFixture < any > ,
8514
- element : Element ,
8515
- x : number ,
8516
- y : number ,
8517
- ) {
8518
- startDraggingViaMouse ( fixture , element ) ;
8519
-
8520
- dispatchMouseEvent ( document , 'mousemove' , x , y ) ;
8521
- fixture . changeDetectorRef . markForCheck ( ) ;
8522
- fixture . detectChanges ( ) ;
8523
-
8524
- dispatchMouseEvent ( document , 'mouseup' , x , y ) ;
8525
- fixture . changeDetectorRef . markForCheck ( ) ;
8526
- fixture . detectChanges ( ) ;
8527
- }
8528
-
8529
- /**
8530
- * Dispatches the events for starting a drag sequence.
8531
- * @param fixture Fixture on which to run change detection.
8532
- * @param element Element on which to dispatch the events.
8533
- * @param x Position along the x axis to which to drag the element.
8534
- * @param y Position along the y axis to which to drag the element.
8535
- */
8536
- function startDraggingViaMouse (
8537
- fixture : ComponentFixture < any > ,
8538
- element : Element ,
8539
- x ?: number ,
8540
- y ?: number ,
8541
- ) {
8542
- dispatchMouseEvent ( element , 'mousedown' , x , y ) ;
8543
- fixture . changeDetectorRef . markForCheck ( ) ;
8544
- fixture . detectChanges ( ) ;
8545
-
8546
- dispatchMouseEvent ( document , 'mousemove' , x , y ) ;
8547
- fixture . changeDetectorRef . markForCheck ( ) ;
8548
- fixture . detectChanges ( ) ;
8549
- }
8550
-
8551
- /**
8552
- * Drags an element to a position on the page using a touch device.
8553
- * @param fixture Fixture on which to run change detection.
8554
- * @param element Element which is being dragged.
8555
- * @param x Position along the x axis to which to drag the element.
8556
- * @param y Position along the y axis to which to drag the element.
8557
- */
8558
- function dragElementViaTouch (
8559
- fixture : ComponentFixture < any > ,
8560
- element : Element ,
8561
- x : number ,
8562
- y : number ,
8563
- ) {
8564
- startDraggingViaTouch ( fixture , element ) ;
8565
- continueDraggingViaTouch ( fixture , x , y ) ;
8566
- stopDraggingViaTouch ( fixture , x , y ) ;
8567
- }
8568
-
8569
- /**
8570
- * @param fixture Fixture on which to run change detection.
8571
- * @param element Element which is being dragged.
8572
- */
8573
- function startDraggingViaTouch ( fixture : ComponentFixture < any > , element : Element ) {
8574
- dispatchTouchEvent ( element , 'touchstart' ) ;
8575
- fixture . changeDetectorRef . markForCheck ( ) ;
8576
- fixture . detectChanges ( ) ;
8577
-
8578
- dispatchTouchEvent ( document , 'touchmove' ) ;
8579
- fixture . changeDetectorRef . markForCheck ( ) ;
8580
- fixture . detectChanges ( ) ;
8581
- }
8582
-
8583
- /**
8584
- * @param fixture Fixture on which to run change detection.
8585
- * @param x Position along the x axis to which to drag the element.
8586
- * @param y Position along the y axis to which to drag the element.
8587
- */
8588
- function continueDraggingViaTouch ( fixture : ComponentFixture < any > , x : number , y : number ) {
8589
- dispatchTouchEvent ( document , 'touchmove' , x , y ) ;
8590
- fixture . changeDetectorRef . markForCheck ( ) ;
8591
- fixture . detectChanges ( ) ;
8592
- }
8593
-
8594
- /**
8595
- * @param fixture Fixture on which to run change detection.
8596
- * @param x Position along the x axis to which to drag the element.
8597
- * @param y Position along the y axis to which to drag the element.
8598
- */
8599
- function stopDraggingViaTouch ( fixture : ComponentFixture < any > , x : number , y : number ) {
8600
- dispatchTouchEvent ( document , 'touchend' , x , y ) ;
8601
- fixture . changeDetectorRef . markForCheck ( ) ;
8602
- fixture . detectChanges ( ) ;
8603
- }
8604
-
8605
- /** Gets the index of an element among its siblings, based on their position on the page. */
8606
- function getElementIndexByPosition ( element : Element , direction : 'top' | 'left' ) {
8607
- return getElementSibligsByPosition ( element , direction ) . indexOf ( element ) ;
8608
- }
8609
-
8610
- /** Gets the siblings of an element, sorted by their position on the page. */
8611
- function getElementSibligsByPosition ( element : Element , direction : 'top' | 'left' ) {
8612
- return element . parentElement
8613
- ? Array . from ( element . parentElement . children ) . sort ( ( a , b ) => {
8614
- return a . getBoundingClientRect ( ) [ direction ] - b . getBoundingClientRect ( ) [ direction ] ;
8615
- } )
8616
- : [ ] ;
8617
- }
8618
-
8619
- /**
8620
- * Adds a large element to the page in order to make it scrollable.
8621
- * @returns Function that should be used to clean up after the test is done.
8622
- */
8623
- function makeScrollable (
8624
- direction : 'vertical' | 'horizontal' = 'vertical' ,
8625
- element = document . body ,
8626
- ) {
8627
- const veryTallElement = document . createElement ( 'div' ) ;
8628
- veryTallElement . style . width = direction === 'vertical' ? '100%' : '4000px' ;
8629
- veryTallElement . style . height = direction === 'vertical' ? '2000px' : '5px' ;
8630
- element . appendChild ( veryTallElement ) ;
8631
-
8632
- return ( ) => {
8633
- scrollTo ( 0 , 0 ) ;
8634
- veryTallElement . remove ( ) ;
8635
- } ;
8636
- }
8637
-
8638
- /**
8639
- * Asserts that sorting an element down works correctly.
8640
- * @param fixture Fixture against which to run the assertions.
8641
- * @param items Array of items against which to test sorting.
8642
- */
8643
- function assertDownwardSorting ( fixture : ComponentFixture < any > , items : Element [ ] ) {
8644
- const draggedItem = items [ 0 ] ;
8645
- const { top, left} = draggedItem . getBoundingClientRect ( ) ;
8646
-
8647
- startDraggingViaMouse ( fixture , draggedItem , left , top ) ;
8648
-
8649
- const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
8650
-
8651
- // Drag over each item one-by-one going downwards.
8652
- for ( let i = 0 ; i < items . length ; i ++ ) {
8653
- const elementRect = items [ i ] . getBoundingClientRect ( ) ;
8654
-
8655
- // Add a few pixels to the top offset so we get some overlap.
8656
- dispatchMouseEvent ( document , 'mousemove' , elementRect . left , elementRect . top + 5 ) ;
8657
- fixture . changeDetectorRef . markForCheck ( ) ;
8658
- fixture . detectChanges ( ) ;
8659
- expect ( getElementIndexByPosition ( placeholder , 'top' ) ) . toBe ( i ) ;
8660
- }
8661
-
8662
- dispatchMouseEvent ( document , 'mouseup' ) ;
8663
- fixture . changeDetectorRef . markForCheck ( ) ;
8664
- fixture . detectChanges ( ) ;
8665
- flush ( ) ;
8666
- }
8667
-
8668
- /**
8669
- * Asserts that sorting an element up works correctly.
8670
- * @param fixture Fixture against which to run the assertions.
8671
- * @param items Array of items against which to test sorting.
8672
- */
8673
- function assertUpwardSorting ( fixture : ComponentFixture < any > , items : Element [ ] ) {
8674
- const draggedItem = items [ items . length - 1 ] ;
8675
- const { top, left} = draggedItem . getBoundingClientRect ( ) ;
8676
-
8677
- startDraggingViaMouse ( fixture , draggedItem , left , top ) ;
8678
-
8679
- const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
8680
-
8681
- // Drag over each item one-by-one going upwards.
8682
- for ( let i = items . length - 1 ; i > - 1 ; i -- ) {
8683
- const elementRect = items [ i ] . getBoundingClientRect ( ) ;
8684
-
8685
- // Remove a few pixels from the bottom offset so we get some overlap.
8686
- dispatchMouseEvent ( document , 'mousemove' , elementRect . left , elementRect . bottom - 5 ) ;
8687
- fixture . changeDetectorRef . markForCheck ( ) ;
8688
- fixture . detectChanges ( ) ;
8689
- expect ( getElementIndexByPosition ( placeholder , 'top' ) ) . toBe ( i ) ;
8690
- }
8691
-
8692
- dispatchMouseEvent ( document , 'mouseup' ) ;
8693
- fixture . changeDetectorRef . markForCheck ( ) ;
8694
- fixture . detectChanges ( ) ;
8695
- flush ( ) ;
8696
- }
8697
-
8698
- /** Ticks the specified amount of `requestAnimationFrame`-s. */
8699
- function tickAnimationFrames ( amount : number ) {
8700
- tick ( 16.6 * amount ) ; // Angular turns rAF calls into 16.6ms timeouts in tests.
8701
- }
0 commit comments