Skip to content
Snippets Groups Projects
Commit 359c382f authored by Eugen Ciur's avatar Eugen Ciur
Browse files

basic tests for reposition_items

parent 9249922b
Branches
No related tags found
No related merge requests found
...@@ -37,6 +37,7 @@ function merge_items(item_id, items) { ...@@ -37,6 +37,7 @@ function merge_items(item_id, items) {
return result_items; return result_items;
} }
function reposition_items({items, selected_ids, drop_pos}) { function reposition_items({items, selected_ids, drop_pos}) {
let selected_items = [], let selected_items = [],
remaining_items = Array.from(items), remaining_items = Array.from(items),
...@@ -48,16 +49,20 @@ function reposition_items({items, selected_ids, drop_pos}) { ...@@ -48,16 +49,20 @@ function reposition_items({items, selected_ids, drop_pos}) {
idx = remaining_items.findIndex(p => get_id(p) == item_id); idx = remaining_items.findIndex(p => get_id(p) == item_id);
extracted_item = remaining_items.slice(idx, 1); extracted_item = remaining_items.slice(idx, idx + 1);
selected_items.push(extracted_item); 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) { if (i == drop_pos) {
result.push(selected_items); result.push(...selected_items);
i += selected_items.length + 1; i += selected_items.length;
} else { // i < drop_pos || i > drop_pos } else { // i < drop_pos || i > drop_pos
if ( j < remaining_items.length ) {
result.push(remaining_items[j]); result.push(remaining_items[j]);
j++;
}
i++; i++;
} }
} }
......
...@@ -62,7 +62,86 @@ module('Unit | Utils | Array', function () { ...@@ -62,7 +62,86 @@ module('Unit | Utils | Array', function () {
expected_result.forEach((page, index) => { expected_result.forEach((page, index) => {
assert.strictEqual(page.id, actual_result[index].id); 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);
}); });
}); });
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment