diff --git a/packages/sortable-list/src/store/index.test.ts b/packages/sortable-list/src/store/index.test.ts index de949ba8..92060d4e 100644 --- a/packages/sortable-list/src/store/index.test.ts +++ b/packages/sortable-list/src/store/index.test.ts @@ -10,54 +10,18 @@ describe('useStore', () => { expect(result.current.data).toEqual([]); }); - describe('updateTableStore', () => { - describe('添加item', () => { - it('基础添加', () => { - const { result } = renderHook(() => useStore()); - - act(() => { - result.current.addItem({ id: '333' }); - }); - - expect(result.current.data).toEqual([{ id: '333' }]); - }); - - it('携带索引的添加', () => { - const { result } = renderHook(() => useStore()); - - act(() => { - result.current.internalUpdateData([ - { id: '1' }, - { id: '2' }, - { id: '3' }, - ]); - - result.current.addItem({ id: '4' }, 0); - }); - - expect(result.current.data).toEqual([ - { id: '4' }, - { id: '1' }, - { id: '2' }, - { id: '3' }, - ]); - }); - }); - it('删除item', () => { + describe('添加item', () => { + it('基础添加', () => { const { result } = renderHook(() => useStore()); act(() => { - result.current.internalUpdateData([ - { id: '1' }, - { id: '2' }, - { id: '3' }, - ]); - result.current.removeItem('2'); + result.current.addItem({ id: '333' }); }); - expect(result.current.data).toEqual([{ id: '1' }, { id: '3' }]); + expect(result.current.data).toEqual([{ id: '333' }]); }); - it('重排序item', () => { + + it('携带索引的添加', () => { const { result } = renderHook(() => useStore()); act(() => { @@ -66,14 +30,48 @@ describe('useStore', () => { { id: '2' }, { id: '3' }, ]); - result.current.reorder(2, 0); + + result.current.addItem({ id: '4' }, 0); }); expect(result.current.data).toEqual([ + { id: '4' }, + { id: '1' }, + { id: '2' }, { id: '3' }, + ]); + }); + }); + it('删除item', () => { + const { result } = renderHook(() => useStore()); + + act(() => { + result.current.internalUpdateData([ { id: '1' }, { id: '2' }, + { id: '3' }, ]); + result.current.removeItem('2'); }); + + expect(result.current.data).toEqual([{ id: '1' }, { id: '3' }]); + }); + it('重排序item', () => { + const { result } = renderHook(() => useStore()); + + act(() => { + result.current.internalUpdateData([ + { id: '1' }, + { id: '2' }, + { id: '3' }, + ]); + result.current.reorder(2, 0); + }); + + expect(result.current.data).toEqual([ + { id: '3' }, + { id: '1' }, + { id: '2' }, + ]); }); }); diff --git a/packages/sortable-list/src/store/index.ts b/packages/sortable-list/src/store/index.ts index 5477093c..180feefe 100644 --- a/packages/sortable-list/src/store/index.ts +++ b/packages/sortable-list/src/store/index.ts @@ -50,9 +50,9 @@ const createStore = () => if (typeof addIndex !== 'number') { // 如果没有提供添加位的 index 值,默认添加在最后 state.push(item); + } else { + state.splice(addIndex, 0, item); } - - state.splice(addIndex, 0, item); }); internalUpdateData(nextData);