From a07625032fc22e9a331c110dc52137d7aa488019 Mon Sep 17 00:00:00 2001 From: Markus Sanin Date: Wed, 31 Jul 2024 12:45:48 +0200 Subject: [PATCH 1/2] Fix error `TypeError: items[0] is undefined` --- addon/src/modifiers/sortable-item.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/src/modifiers/sortable-item.js b/addon/src/modifiers/sortable-item.js index 1c767bc6..cf8215b5 100644 --- a/addon/src/modifiers/sortable-item.js +++ b/addon/src/modifiers/sortable-item.js @@ -826,7 +826,7 @@ export default class SortableItemModifier extends Modifier { */ get transitionDuration() { const items = this.sortableGroup.sortedItems.filter((x) => !x.isDragging && !x.isDropping); - let el = items[0].element ?? this.element; // Fallback when only one element is present in list + let el = items[0]?.element ?? this.element; // Fallback when only one element is present in list let rule = getComputedStyle(el).transitionDuration; let match = rule.match(/([\d.]+)([ms]*)/); From 5d5826874765ed3b3e1413596a051642e36ba557 Mon Sep 17 00:00:00 2001 From: Markus Sanin Date: Sat, 3 Aug 2024 10:44:41 +0200 Subject: [PATCH 2/2] Add test for `transitionDuration` when only one item is present which will be moved --- .../modifiers/sortable-item-test.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test-app/tests/integration/modifiers/sortable-item-test.js diff --git a/test-app/tests/integration/modifiers/sortable-item-test.js b/test-app/tests/integration/modifiers/sortable-item-test.js new file mode 100644 index 00000000..0d54f003 --- /dev/null +++ b/test-app/tests/integration/modifiers/sortable-item-test.js @@ -0,0 +1,36 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'ember-qunit'; +import { find, render } from '@ember/test-helpers'; +import { set } from '@ember/object'; +import { drag } from 'ember-sortable/test-support'; +import hbs from 'htmlbars-inline-precompile'; + +module('Integration | Modifier | sortable-item', function (hooks) { + setupRenderingTest(hooks); + + test('Drag works with one item', async function (assert) { + this.items = ['Uno']; + + this.update = (items) => { + set(this, 'items', items); + }; + + await render(hbs` +
    + {{#each this.items as |item|}} +
  1. {{item}}
  2. + {{/each}} +
+ `); + + await drag('mouse', '[data-test-item]', () => { + return { dy: 10 }; + }); + + assert.equal(contents('#test-list'), 'Uno'); + }); + + function contents(selector) { + return find(selector).textContent.replace(/⇕/g, '').replace(/\s+/g, ' ').replace(/^\s+/, '').replace(/\s+$/, ''); + } +});