|
| 1 | +import {QueryList, ViewChildren, Component} from '@angular/core'; |
| 2 | +import {fakeAsync, TestBed, ComponentFixture, inject} from '@angular/core/testing'; |
| 3 | +import { |
| 4 | + createMouseEvent, |
| 5 | + dispatchMouseEvent, |
| 6 | + createTouchEvent, |
| 7 | + dispatchTouchEvent, |
| 8 | +} from '@angular/cdk/testing'; |
| 9 | +import {CdkDragDropRegistry} from './drag-drop-registry'; |
| 10 | +import {DragDropModule} from './drag-drop-module'; |
| 11 | +import {CdkDrag} from './drag'; |
| 12 | +import {CdkDrop} from './drop'; |
| 13 | + |
| 14 | +describe('DragDropRegistry', () => { |
| 15 | + let fixture: ComponentFixture<SimpleDropZone>; |
| 16 | + let testComponent: SimpleDropZone; |
| 17 | + let registry: CdkDragDropRegistry; |
| 18 | + |
| 19 | + beforeEach(fakeAsync(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + imports: [DragDropModule], |
| 22 | + declarations: [SimpleDropZone], |
| 23 | + }).compileComponents(); |
| 24 | + |
| 25 | + fixture = TestBed.createComponent(SimpleDropZone); |
| 26 | + testComponent = fixture.componentInstance; |
| 27 | + fixture.detectChanges(); |
| 28 | + |
| 29 | + inject([CdkDragDropRegistry], (c: CdkDragDropRegistry) => { |
| 30 | + registry = c; |
| 31 | + })(); |
| 32 | + })); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + registry.ngOnDestroy(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should be able to start dragging an item', () => { |
| 39 | + const firstItem = testComponent.dragItems.first; |
| 40 | + |
| 41 | + expect(registry.isDragging(firstItem)).toBe(false); |
| 42 | + registry.startDragging(firstItem, createMouseEvent('mousedown')); |
| 43 | + expect(registry.isDragging(firstItem)).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should be able to stop dragging an item', () => { |
| 47 | + const firstItem = testComponent.dragItems.first; |
| 48 | + |
| 49 | + registry.startDragging(firstItem, createMouseEvent('mousedown')); |
| 50 | + expect(registry.isDragging(firstItem)).toBe(true); |
| 51 | + |
| 52 | + registry.stopDragging(firstItem); |
| 53 | + expect(registry.isDragging(firstItem)).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should stop dragging an item if it is removed', () => { |
| 57 | + const firstItem = testComponent.dragItems.first; |
| 58 | + |
| 59 | + registry.startDragging(firstItem, createMouseEvent('mousedown')); |
| 60 | + expect(registry.isDragging(firstItem)).toBe(true); |
| 61 | + |
| 62 | + registry.remove(firstItem); |
| 63 | + expect(registry.isDragging(firstItem)).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should dispatch `mousemove` events after starting to drag via the mouse', () => { |
| 67 | + const spy = jasmine.createSpy('pointerMove spy'); |
| 68 | + const subscription = registry.pointerMove.subscribe(spy); |
| 69 | + |
| 70 | + registry.startDragging(testComponent.dragItems.first, createMouseEvent('mousedown')); |
| 71 | + dispatchMouseEvent(document, 'mousemove'); |
| 72 | + |
| 73 | + expect(spy).toHaveBeenCalled(); |
| 74 | + |
| 75 | + subscription.unsubscribe(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should dispatch `touchmove` events after starting to drag via touch', () => { |
| 79 | + const spy = jasmine.createSpy('pointerMove spy'); |
| 80 | + const subscription = registry.pointerMove.subscribe(spy); |
| 81 | + |
| 82 | + registry.startDragging(testComponent.dragItems.first, |
| 83 | + createTouchEvent('touchstart') as TouchEvent); |
| 84 | + dispatchTouchEvent(document, 'touchmove'); |
| 85 | + |
| 86 | + expect(spy).toHaveBeenCalled(); |
| 87 | + |
| 88 | + subscription.unsubscribe(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should dispatch `mouseup` events after ending the drag via the mouse', () => { |
| 92 | + const spy = jasmine.createSpy('pointerUp spy'); |
| 93 | + const subscription = registry.pointerUp.subscribe(spy); |
| 94 | + |
| 95 | + registry.startDragging(testComponent.dragItems.first, createMouseEvent('mousedown')); |
| 96 | + dispatchMouseEvent(document, 'mouseup'); |
| 97 | + |
| 98 | + expect(spy).toHaveBeenCalled(); |
| 99 | + |
| 100 | + subscription.unsubscribe(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should dispatch `touchend` events after ending the drag via touch', () => { |
| 104 | + const spy = jasmine.createSpy('pointerUp spy'); |
| 105 | + const subscription = registry.pointerUp.subscribe(spy); |
| 106 | + |
| 107 | + registry.startDragging(testComponent.dragItems.first, |
| 108 | + createTouchEvent('touchstart') as TouchEvent); |
| 109 | + dispatchTouchEvent(document, 'touchend'); |
| 110 | + |
| 111 | + expect(spy).toHaveBeenCalled(); |
| 112 | + |
| 113 | + subscription.unsubscribe(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should complete the pointer event streams on destroy', () => { |
| 117 | + const pointerUpSpy = jasmine.createSpy('pointerUp complete spy'); |
| 118 | + const pointerMoveSpy = jasmine.createSpy('pointerMove complete spy'); |
| 119 | + const pointerUpSubscription = registry.pointerUp.subscribe(undefined, undefined, pointerUpSpy); |
| 120 | + const pointerMoveSubscription = |
| 121 | + registry.pointerMove.subscribe(undefined, undefined, pointerMoveSpy); |
| 122 | + |
| 123 | + registry.ngOnDestroy(); |
| 124 | + |
| 125 | + expect(pointerUpSpy).toHaveBeenCalled(); |
| 126 | + expect(pointerMoveSpy).toHaveBeenCalled(); |
| 127 | + |
| 128 | + pointerUpSubscription.unsubscribe(); |
| 129 | + pointerMoveSubscription.unsubscribe(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should not throw when trying to register the same container again', () => { |
| 133 | + expect(() => registry.register(testComponent.dropInstances.first)).not.toThrow(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should throw when trying to register a different container with the same id', () => { |
| 137 | + expect(() => { |
| 138 | + testComponent.showDuplicateContainer = true; |
| 139 | + fixture.detectChanges(); |
| 140 | + }).toThrowError(/Drop instance with id \"items\" has already been registered/); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should be able to get a drop container by its id', () => { |
| 144 | + expect(registry.getDropContainer('items')).toBe(testComponent.dropInstances.first); |
| 145 | + expect(registry.getDropContainer('does-not-exist')).toBeFalsy(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not prevent the default `touchmove` actions when nothing is being dragged', () => { |
| 149 | + expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(false); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should prevent the default `touchmove` action when an item is being dragged', () => { |
| 153 | + registry.startDragging(testComponent.dragItems.first, |
| 154 | + createTouchEvent('touchstart') as TouchEvent); |
| 155 | + expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(true); |
| 156 | + }); |
| 157 | + |
| 158 | +}); |
| 159 | + |
| 160 | +@Component({ |
| 161 | + template: ` |
| 162 | + <cdk-drop id="items" [data]="items"> |
| 163 | + <div *ngFor="let item of items" cdkDrag>{{item}}</div> |
| 164 | + </cdk-drop> |
| 165 | +
|
| 166 | + <cdk-drop id="items" *ngIf="showDuplicateContainer"></cdk-drop> |
| 167 | + ` |
| 168 | +}) |
| 169 | +export class SimpleDropZone { |
| 170 | + @ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>; |
| 171 | + @ViewChildren(CdkDrop) dropInstances: QueryList<CdkDrop>; |
| 172 | + items = ['Zero', 'One', 'Two', 'Three']; |
| 173 | + showDuplicateContainer = false; |
| 174 | +} |
0 commit comments