|
| 1 | +/* |
| 2 | + * -- copyright |
| 3 | + * OpenProject is an open source project management software. |
| 4 | + * Copyright (C) the OpenProject GmbH |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License version 3. |
| 8 | + * |
| 9 | + * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 10 | + * Copyright (C) 2006-2013 Jean-Philippe Lang |
| 11 | + * Copyright (C) 2010-2013 the ChiliProject Team |
| 12 | + * |
| 13 | + * This program is free software; you can redistribute it and/or |
| 14 | + * modify it under the terms of the GNU General Public License |
| 15 | + * as published by the Free Software Foundation; either version 2 |
| 16 | + * of the License, or (at your option) any later version. |
| 17 | + * |
| 18 | + * This program is distributed in the hope that it will be useful, |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + * GNU General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU General Public License |
| 24 | + * along with this program; if not, write to the Free Software |
| 25 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 26 | + * |
| 27 | + * See COPYRIGHT and LICENSE files for more details. |
| 28 | + * ++ |
| 29 | + */ |
| 30 | + |
| 31 | +import GenericDragAndDropController from './generic-drag-and-drop.controller'; |
| 32 | + |
| 33 | +describe('GenericDragAndDropController', () => { |
| 34 | + let controller:GenericDragAndDropController; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + controller = Object.create(GenericDragAndDropController.prototype) as GenericDragAndDropController; |
| 38 | + }); |
| 39 | + |
| 40 | + function setValue(name:'handleValue'|'handleSelectorValue', value:boolean|string) { |
| 41 | + Object.defineProperty(controller, name, { value, configurable: true }); |
| 42 | + } |
| 43 | + |
| 44 | + function draggableRow() { |
| 45 | + const row = document.createElement('li'); |
| 46 | + row.className = 'Box-row Box-row--draggable'; |
| 47 | + row.tabIndex = 0; |
| 48 | + row.dataset.draggableId = '42'; |
| 49 | + row.dataset.draggableType = 'story'; |
| 50 | + row.dataset.dropUrl = '/drop'; |
| 51 | + return row; |
| 52 | + } |
| 53 | + |
| 54 | + function callCanStartDrag(el:Element|null|undefined, handle:Element|null|undefined):boolean { |
| 55 | + const canStartDrag = Reflect.get(controller, 'canStartDrag') as ( |
| 56 | + this:GenericDragAndDropController, |
| 57 | + el:Element|null|undefined, |
| 58 | + handle:Element|null|undefined |
| 59 | + ) => boolean; |
| 60 | + |
| 61 | + return canStartDrag.call(controller, el, handle); |
| 62 | + } |
| 63 | + |
| 64 | + function callAriaPressedTarget(el:Element):Element|null { |
| 65 | + const ariaPressedTarget = Reflect.get(controller, 'ariaPressedTarget') as ( |
| 66 | + this:GenericDragAndDropController, |
| 67 | + el:Element |
| 68 | + ) => Element|null; |
| 69 | + |
| 70 | + return ariaPressedTarget.call(controller, el); |
| 71 | + } |
| 72 | + |
| 73 | + function callResolveMirrorContainer():Element { |
| 74 | + const resolveMirrorContainer = Reflect.get(controller, 'resolveMirrorContainer') as ( |
| 75 | + this:GenericDragAndDropController |
| 76 | + ) => Element; |
| 77 | + |
| 78 | + return resolveMirrorContainer.call(controller); |
| 79 | + } |
| 80 | + |
| 81 | + describe('canStartDrag', () => { |
| 82 | + it('allows dragging a draggable row in handle-less mode', () => { |
| 83 | + const row = draggableRow(); |
| 84 | + |
| 85 | + setValue('handleValue', false); |
| 86 | + setValue('handleSelectorValue', '.DragHandle'); |
| 87 | + |
| 88 | + expect(callCanStartDrag(row, row)).toBeTrue(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('rejects rows that are not draggable in handle-less mode', () => { |
| 92 | + const row = document.createElement('li'); |
| 93 | + row.className = 'Box-row'; |
| 94 | + row.tabIndex = 0; |
| 95 | + |
| 96 | + setValue('handleValue', false); |
| 97 | + setValue('handleSelectorValue', '.DragHandle'); |
| 98 | + |
| 99 | + expect(callCanStartDrag(row, row)).toBeFalse(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('rejects empty placeholder rows in handle-less mode', () => { |
| 103 | + const row = draggableRow(); |
| 104 | + row.dataset.emptyListItem = 'true'; |
| 105 | + |
| 106 | + setValue('handleValue', false); |
| 107 | + setValue('handleSelectorValue', '.DragHandle'); |
| 108 | + |
| 109 | + expect(callCanStartDrag(row, row)).toBeFalse(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('rejects interactive descendants in handle-less mode', () => { |
| 113 | + const row = draggableRow(); |
| 114 | + const button = document.createElement('button'); |
| 115 | + row.appendChild(button); |
| 116 | + |
| 117 | + setValue('handleValue', false); |
| 118 | + setValue('handleSelectorValue', '.DragHandle'); |
| 119 | + |
| 120 | + expect(callCanStartDrag(row, button)).toBeFalse(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('allows drag handles in handle mode', () => { |
| 124 | + const row = draggableRow(); |
| 125 | + const handle = document.createElement('button'); |
| 126 | + handle.className = 'DragHandle'; |
| 127 | + row.appendChild(handle); |
| 128 | + |
| 129 | + setValue('handleValue', true); |
| 130 | + setValue('handleSelectorValue', '.DragHandle'); |
| 131 | + |
| 132 | + expect(callCanStartDrag(row, handle)).toBeTrue(); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('ariaPressedTarget', () => { |
| 137 | + it('returns null in handle-less mode', () => { |
| 138 | + const row = draggableRow(); |
| 139 | + |
| 140 | + setValue('handleValue', false); |
| 141 | + setValue('handleSelectorValue', '.DragHandle'); |
| 142 | + |
| 143 | + expect(callAriaPressedTarget(row)).toBeNull(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('returns the handle element in handle mode', () => { |
| 147 | + const row = draggableRow(); |
| 148 | + const handle = document.createElement('button'); |
| 149 | + handle.className = 'DragHandle'; |
| 150 | + row.appendChild(handle); |
| 151 | + |
| 152 | + setValue('handleValue', true); |
| 153 | + setValue('handleSelectorValue', '.DragHandle'); |
| 154 | + |
| 155 | + expect(callAriaPressedTarget(row)).toBe(handle); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('resolveMirrorContainer', () => { |
| 160 | + it('returns the configured mirror container target when present', () => { |
| 161 | + const mirrorContainer = document.createElement('div'); |
| 162 | + |
| 163 | + Object.defineProperty(controller, 'hasMirrorContainerTarget', { value: true, configurable: true }); |
| 164 | + Object.defineProperty(controller, 'mirrorContainerTarget', { value: mirrorContainer, configurable: true }); |
| 165 | + |
| 166 | + expect(callResolveMirrorContainer()).toBe(mirrorContainer); |
| 167 | + }); |
| 168 | + |
| 169 | + it('falls back to document.body when no mirror container target exists', () => { |
| 170 | + Object.defineProperty(controller, 'hasMirrorContainerTarget', { value: false, configurable: true }); |
| 171 | + |
| 172 | + expect(callResolveMirrorContainer()).toBe(document.body); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments