Skip to content

Commit

Permalink
♻️ refactor: 重构 uuid 在测试环境下返回确定的值
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed Sep 7, 2023
1 parent e949227 commit d5b9f2e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7,017 deletions.
2 changes: 1 addition & 1 deletion src/SortableList/container/StoreUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const StoreUpdater = forwardRef(
value.forEach((__, index) => {
const key = draft[index];
if (key === undefined) {
draft[index] = getUUID();
draft[index] = getUUID(index);
}
});
return draft;
Expand Down
2 changes: 1 addition & 1 deletion src/SortableList/store/keyManagerReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const keyManagerReducer = (value: any[], payload: SortableListDispatchPay
case 'addItem':
const { index = value.length } = payload;
return produce(value, (draft) => {
draft.splice(index, 0, getUUID());
draft.splice(index, 0, getUUID(index));
return draft;
});

Expand Down
8 changes: 2 additions & 6 deletions src/SortableList/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { useEffect, useState } from 'react';

const defaultInitializer = (index: number) => index;

const env = this === window ? 'browser' : 'node';

export function createRange<T = number>(
length: number,
initializer: (index: number) => any = defaultInitializer,
Expand Down Expand Up @@ -67,8 +65,6 @@ export const getIndexOfActiveItem = (list: any[], id?: UniqueIdentifier) => {
return id ? list.findIndex((item) => item === id) : -1;
};

let id = 0;

export const getUUID = () => {
return env === 'browser' ? crypto.randomUUID() : id++;
export const getUUID = (index) => {
return process.env.NODE_ENV === 'test' ? `test-${index}` : crypto.randomUUID();
};
Loading

0 comments on commit d5b9f2e

Please sign in to comment.