import { keyBy } from 'lodash'; import { generatedComputedStates, hasState, Item, ItemState } from './silke-nested-sortable-utils'; let id = 0; const getItem = (children?: Item[]): Item => { return { key: 'item' + id++, children: children?.map((item) => item.key) }; }; describe('SilkeNestedSortableUtils', () => { beforeEach(() => { id = 0; }); it('should generate computed items', () => { const nestedChildren = [getItem()]; const children = [getItem(), getItem(nestedChildren)]; const items = [getItem(children), getItem(), ...children, ...nestedChildren]; const selected = [items[0].key]; const computed = generatedComputedStates(keyBy(items, 'key'), selected, selected); expect(hasState(computed[0][0], ItemState.roundTop)).toBe(true); expect(hasState(computed[0][0], ItemState.selected)).toBe(true); expect(hasState(computed[0][0], ItemState.roundBottom)).toBe(false); for (let i = 1; i < 4; i++) { expect(hasState(computed[i][0], ItemState.childOfSelected)).toBe(true); expect(hasState(computed[i][0], ItemState.roundTop)).toBe(false); expect(hasState(computed[i][0], ItemState.roundBottom)).toBe(i === 3); } }); it('should round bottom of last selected', () => { const items = [getItem(), getItem(), getItem(), getItem()]; const keys = items.map((item) => item.key); const selected = [items[1].key, items[2].key]; const computed = generatedComputedStates(keyBy(items, 'key'), keys, selected); expect(hasState(computed[1][0], ItemState.selected)).toBe(true); expect(hasState(computed[1][0], ItemState.roundTop)).toBe(true); expect(hasState(computed[1][0], ItemState.roundBottom)).toBe(false); expect(hasState(computed[2][0], ItemState.selected)).toBe(true); expect(hasState(computed[2][0], ItemState.roundTop)).toBe(false); expect(hasState(computed[2][0], ItemState.roundBottom)).toBe(true); }); it('should round bottom of last childOfSelected', () => { const children = [getItem()]; const items = [getItem(children), ...children]; const computed = generatedComputedStates(keyBy(items, 'key'), [items[0].key], [items[0].key]); expect(hasState(computed[0][0], ItemState.selected)).toBe(true); expect(hasState(computed[0][0], ItemState.roundBottom)).toBe(false); expect(hasState(computed[1][0], ItemState.roundBottom)).toBe(true); expect(hasState(computed[1][0], ItemState.selected)).toBe(false); expect(hasState(computed[1][0], ItemState.childOfSelected)).toBe(true); }); });