Skip to content

Commit

Permalink
✅ test: 修正单测
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Mar 8, 2022
1 parent 44ddeed commit 89307f0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
84 changes: 41 additions & 43 deletions packages/sortable-list/src/store/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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' },
]);
});
});
4 changes: 2 additions & 2 deletions packages/sortable-list/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 89307f0

Please sign in to comment.