From 359c382fa8938cafea5b69bef931f2df91071a97 Mon Sep 17 00:00:00 2001 From: Eugen Ciur <eugen@papermerge.com> Date: Sun, 13 Mar 2022 07:48:13 +0100 Subject: [PATCH] basic tests for reposition_items --- app/utils/array.js | 19 +++++--- tests/unit/utils/array-test.js | 81 +++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/app/utils/array.js b/app/utils/array.js index 6f36cd3..f80ba54 100644 --- a/app/utils/array.js +++ b/app/utils/array.js @@ -37,6 +37,7 @@ function merge_items(item_id, items) { return result_items; } + function reposition_items({items, selected_ids, drop_pos}) { let selected_items = [], remaining_items = Array.from(items), @@ -48,16 +49,20 @@ function reposition_items({items, selected_ids, drop_pos}) { idx = remaining_items.findIndex(p => get_id(p) == item_id); - extracted_item = remaining_items.slice(idx, 1); - selected_items.push(extracted_item); + extracted_item = remaining_items.slice(idx, idx + 1); + remaining_items.splice(idx, 1); + selected_items.push(...extracted_item); }); - for (i=0, j=0; i < items.length; j++) { + for (i=0, j=0; i < items.length;) { if (i == drop_pos) { - result.push(selected_items); - i += selected_items.length + 1; + result.push(...selected_items); + i += selected_items.length; } else { // i < drop_pos || i > drop_pos - result.push(remaining_items[j]); + if ( j < remaining_items.length ) { + result.push(remaining_items[j]); + j++; + } i++; } } @@ -69,4 +74,4 @@ export { get_id, merge_items, reposition_items, -} \ No newline at end of file +} diff --git a/tests/unit/utils/array-test.js b/tests/unit/utils/array-test.js index d03e9d4..538e3dd 100644 --- a/tests/unit/utils/array-test.js +++ b/tests/unit/utils/array-test.js @@ -62,7 +62,86 @@ module('Unit | Utils | Array', function () { expected_result.forEach((page, index) => { assert.strictEqual(page.id, actual_result[index].id); }); + }); + + + test('reposition_items 2', function(assert) { + let items, + selected_ids = ['2', '3'], + drop_pos = 0, + actual_result, + expected_result; + + items = [ + new Page('1'), new Page('2'), new Page('3'), new Page('4') + ]; + + expected_result = [ + new Page('2'), new Page('3'), new Page('1'), new Page('4') + ] + + actual_result = reposition_items({ + items, selected_ids, drop_pos + }); + assert.expect( expected_result.length ); + + expected_result.forEach((page, index) => { + assert.strictEqual(page.id, actual_result[index].id); + }); + }); + + + test('reposition_items 3', function(assert) { + let items, + selected_ids = ['1'], + drop_pos = 3, + actual_result, + expected_result; + + items = [ + new Page('1'), new Page('2'), new Page('3'), new Page('4') + ]; + + expected_result = [ + new Page('2'), new Page('3'), new Page('4'), new Page('1') + ] + + actual_result = reposition_items({ + items, selected_ids, drop_pos + }); + + assert.expect( expected_result.length ); + + expected_result.forEach((page, index) => { + assert.strictEqual(page.id, actual_result[index].id); + }); }); -}); + test('reposition_items 4', function(assert) { + let items, + selected_ids = ['2', '3'], + drop_pos = 3, + actual_result, + expected_result; + + items = [ + new Page('1'), new Page('2'), new Page('3'), new Page('4') + ]; + + expected_result = [ + new Page('1'), new Page('4'), new Page('2'), new Page('3') + ] + + actual_result = reposition_items({ + items, selected_ids, drop_pos + }); + + assert.expect( expected_result.length ); + + expected_result.forEach((page, index) => { + assert.strictEqual(page.id, actual_result[index].id); + }); + }); + +}); -- GitLab