import type { GridRowId } from '../types' import { doesDropProduceChange, getIndexAfterDrop } from './drop' import type { Collection } from './types' const collection: Collection = { ids: ['1', '2', '3', '4'], entities: new Map([ ['1', { id: '1' }], ['2', { id: '2' }], ['3', { id: '3' }], ['4', { id: '4' }], ['11', { id: '11' }], ['12', { id: '12' }], ['13', { id: '13' }], ['22', { id: '22' }], ['44', { id: '44' }], ]), meta: new Map([ ['1', { type: 'tree', children: ['11', '12', '13', '14'] }], ['2', { type: 'tree', children: ['22'] }], ['4', { type: 'tree', children: ['44'] }], ]), } describe('drop', () => { describe('getIndexAfterDrop', () => { describe('when no beforeId is provided', () => { it('should return 0', () => { expect( getIndexAfterDrop( collection, new Set(['1']), null, null, null ) ).toBe(0) }) }) describe('when parentId is null', () => { it('should find the index within the root items', () => { expect( getIndexAfterDrop( collection, new Set(['11']), null, '1', null ) ).toBe(1) }) it('should exclude the currently dragged rows from the found index', () => { expect( getIndexAfterDrop( collection, new Set(['1', '2']), null, '4', null ) ).toBe(2) }) it('should return null if beforeId is not in list', () => { expect( getIndexAfterDrop( collection, new Set(['1']), null, '5', null ) ).toBe(null) }) }) describe('when parentId is provided', () => { it('should find the index within the parent items children', () => { expect( getIndexAfterDrop( collection, new Set(['22']), '1', '12', null ) ).toBe(2) }) it('should exclude the currently dragged rows from the found index', () => { expect( getIndexAfterDrop( collection, new Set(['11', '13']), '1', '12', null ) ).toBe(1) }) it('should return null if beforeId is not in list', () => { expect( getIndexAfterDrop( collection, new Set(['22']), '1', '15', null ) ).toBe(null) }) }) describe('when sortFn is provided', () => { const sortFn = jest.fn().mockReturnValue(['4', '2', '1']) expect( getIndexAfterDrop(collection, new Set(['3']), null, '2', sortFn) ).toBe(2) expect(sortFn).toBeCalledWith( ['1', '2', '4'], collection.entities, collection.meta ) }) }) describe('doesDropProduceChange', () => { let parentLookup: Map beforeEach(() => { parentLookup = new Map([ ['2', '1'], ['3', '1'], ]) }) describe('when in parent mode', () => { it('should report true if any parents change', () => { expect( doesDropProduceChange( { draggedRowIds: ['2', '3', '4'], resultIds: null, targetParentId: '1', }, 'parent', parentLookup, null ) ).toBe(true) }) it('should report false if no parents change', () => { expect( doesDropProduceChange( { draggedRowIds: ['2', '3'], resultIds: null, targetParentId: '1', }, 'parent', parentLookup, null ) ).toBe(false) }) }) describe('when in default mode', () => { it('should report true if any parents change', () => { expect( doesDropProduceChange( { draggedRowIds: ['2', '3', '4'], resultIds: ['2', '3', '4'], targetParentId: '1', }, 'default', parentLookup, ['2', '3'] ) ).toBe(true) }) describe('with a parentId', () => { it('should report true if ids change', () => { expect( doesDropProduceChange( { draggedRowIds: ['2', '3'], resultIds: ['3', '2'], targetParentId: '1', }, 'default', parentLookup, ['2', '3'] ) ).toBe(true) }) it('should report false if ids remain the same', () => { expect( doesDropProduceChange( { draggedRowIds: ['2', '3'], resultIds: ['2', '3'], targetParentId: '1', }, 'default', parentLookup, ['2', '3'] ) ).toBe(false) }) }) describe('with a null parentId', () => { it('should report true if ids change', () => { expect( doesDropProduceChange( { draggedRowIds: ['1', '4'], resultIds: ['1', '4'], targetParentId: null, }, 'default', parentLookup, ['4', '1'] ) ).toBe(true) }) it('should report false if ids remain the same', () => { expect( doesDropProduceChange( { draggedRowIds: ['1', '4'], resultIds: ['1', '4'], targetParentId: null, }, 'default', parentLookup, ['1', '4'] ) ).toBe(false) }) }) }) }) })