49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
|
|
import { describe, expect, it } from 'vitest'
|
||
|
|
import {
|
||
|
|
computeDropPatches,
|
||
|
|
computeMovePatches,
|
||
|
|
computeReorderPatches,
|
||
|
|
sortByOrder,
|
||
|
|
} from './reorder.js'
|
||
|
|
|
||
|
|
const items = [
|
||
|
|
{ id: 'a', sort_order: 0 },
|
||
|
|
{ id: 'b', sort_order: 10 },
|
||
|
|
{ id: 'c', sort_order: 20 },
|
||
|
|
]
|
||
|
|
|
||
|
|
describe('reorder', () => {
|
||
|
|
it('sorts by sort_order then id', () => {
|
||
|
|
const unsorted = [
|
||
|
|
{ id: 'c', sort_order: 20 },
|
||
|
|
{ id: 'a', sort_order: 0 },
|
||
|
|
{ id: 'b', sort_order: 10 },
|
||
|
|
]
|
||
|
|
expect(sortByOrder(unsorted).map((i) => i.id)).toEqual(['a', 'b', 'c'])
|
||
|
|
})
|
||
|
|
|
||
|
|
it('computes patches when moving down', () => {
|
||
|
|
const patches = computeMovePatches(items, 'a', 'down')
|
||
|
|
expect(patches).toEqual(
|
||
|
|
expect.arrayContaining([
|
||
|
|
{ id: 'a', sort_order: 10 },
|
||
|
|
{ id: 'b', sort_order: 0 },
|
||
|
|
]),
|
||
|
|
)
|
||
|
|
expect(patches).toHaveLength(2)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('computes patches for drag drop', () => {
|
||
|
|
const patches = computeDropPatches(items, 'c', 'a')
|
||
|
|
expect(patches).toEqual([
|
||
|
|
{ id: 'c', sort_order: 0 },
|
||
|
|
{ id: 'a', sort_order: 10 },
|
||
|
|
{ id: 'b', sort_order: 20 },
|
||
|
|
])
|
||
|
|
})
|
||
|
|
|
||
|
|
it('returns empty when indices unchanged', () => {
|
||
|
|
expect(computeReorderPatches(items, 1, 1)).toEqual([])
|
||
|
|
})
|
||
|
|
})
|