|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupRenderingTest } from 'ember-qunit'; |
| 3 | +import { findAll, render, triggerEvent } from '@ember/test-helpers'; |
| 4 | +import hbs from 'htmlbars-inline-precompile'; |
| 5 | +import { A } from '@ember/array'; |
| 6 | + |
| 7 | +module( |
| 8 | + 'Integration | Component | drag-sort targetIndex regression test', |
| 9 | + function (hooks) { |
| 10 | + setupRenderingTest(hooks); |
| 11 | + |
| 12 | + test('service targetIndex should not be set immediately on dragstart', async function (assert) { |
| 13 | + // This test prevents regression of the bug where targetIndex was being set |
| 14 | + // immediately on dragstart, which interfered with dragover events in modern Ember |
| 15 | + |
| 16 | + const items = A([ |
| 17 | + { name: 'Item 1' }, |
| 18 | + { name: 'Item 2' }, |
| 19 | + { name: 'Item 3' }, |
| 20 | + ]); |
| 21 | + |
| 22 | + this.setProperties({ items }); |
| 23 | + |
| 24 | + await render(hbs` |
| 25 | + <DragSortList |
| 26 | + @items={{this.items}} |
| 27 | + @group="test" |
| 28 | + as |item| |
| 29 | + > |
| 30 | + <div class="the-item">{{item.name}}</div> |
| 31 | + </DragSortList> |
| 32 | + `); |
| 33 | + |
| 34 | + const dragSort = this.owner.lookup('service:drag-sort'); |
| 35 | + const itemElements = findAll('.dragSortItem'); |
| 36 | + |
| 37 | + // Initial state - targetIndex should be null |
| 38 | + assert.strictEqual( |
| 39 | + dragSort.targetIndex, |
| 40 | + null, |
| 41 | + 'targetIndex should initially be null', |
| 42 | + ); |
| 43 | + |
| 44 | + // Start dragging the second item (index 1) |
| 45 | + await triggerEvent(itemElements[1], 'dragstart', { |
| 46 | + dataTransfer: new DataTransfer(), |
| 47 | + }); |
| 48 | + |
| 49 | + // CRITICAL: targetIndex should remain null after dragstart |
| 50 | + // It should only be set later during actual dragover events |
| 51 | + // The bug was setting targetIndex = index - 1 immediately, which caused |
| 52 | + // CSS class conflicts and prevented dragover events from firing properly |
| 53 | + assert.strictEqual( |
| 54 | + dragSort.targetIndex, |
| 55 | + null, |
| 56 | + 'targetIndex should remain null after dragstart - it should only be set during dragover', |
| 57 | + ); |
| 58 | + |
| 59 | + // Clean up |
| 60 | + await triggerEvent(itemElements[1], 'dragend'); |
| 61 | + }); |
| 62 | + }, |
| 63 | +); |
0 commit comments