Skip to content

Commit e484ae4

Browse files
authored
test(SwipeCell): improve test coverage (#810)
1 parent ccbe414 commit e484ae4

File tree

3 files changed

+805
-16
lines changed

3 files changed

+805
-16
lines changed

site/test-coverage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
stepper: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
5555
steps: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
5656
sticky: { statements: '67.85%', branches: '30%', functions: '85.71%', lines: '69.09%' },
57-
swipeCell: { statements: '4.42%', branches: '0%', functions: '0%', lines: '4.67%' },
57+
swipeCell: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
5858
swiper: { statements: '3.77%', branches: '0.9%', functions: '1.4%', lines: '3.89%' },
5959
switch: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
6060
tabBar: { statements: '10%', branches: '0%', functions: '0%', lines: '10.81%' },

src/swipe-cell/SwipeCell.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ export interface SwipeCellProps extends TdSwipeCellProps, StyledProps {}
2525

2626
const threshold = '50%';
2727

28+
export const syncOpenedState = (
29+
rootRef: React.RefObject<HTMLDivElement>,
30+
opened: SwipeCellProps['opened'],
31+
getOpenedSide: (opened: SwipeCellProps['opened']) => SideType | undefined,
32+
expand: (side: SideType) => void,
33+
close: () => void,
34+
) => {
35+
if (!rootRef.current) return;
36+
37+
const side = getOpenedSide(opened);
38+
39+
if (side === 'left' || side === 'right') {
40+
// 初始化 expanded,等待 dom 加载完,获取 left/right 宽度后无动画设置展开状态
41+
// 防止 left/right 为列表时,获取真实宽度有误
42+
setTimeout(() => {
43+
expand(side);
44+
}, 100);
45+
} else {
46+
close();
47+
}
48+
};
49+
2850
const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) => {
2951
const props = useDefaultProps<SwipeCellProps>(originProps, swipeCellDefaultProps);
3052

@@ -49,10 +71,10 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
4971
return;
5072
}
5173
if (isArray(opened)) {
52-
if (open[1] && rightRef.current) {
74+
if (opened[1] && rightRef.current) {
5375
return 'right';
5476
}
55-
if (open[0] && leftRef.current) {
77+
if (opened[0] && leftRef.current) {
5678
return 'left';
5779
}
5880
}
@@ -130,7 +152,7 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
130152
} else {
131153
close();
132154
}
133-
window.setTimeout(() => {
155+
setTimeout(() => {
134156
ctx.dragging = false;
135157
});
136158
} else {
@@ -156,18 +178,7 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
156178
}));
157179

158180
useLayoutEffect(() => {
159-
if (!rootRef.current) return;
160-
const side = getOpenedSide(opened);
161-
162-
if (['left', 'right'].includes(side)) {
163-
// 初始化 expanded,等待 dom 加载完,获取 left/right 宽度后无动画设置展开状态
164-
// 防止 left/right 为列表时,获取真实宽度有误
165-
setTimeout(() => {
166-
expand(side as SideType);
167-
}, 100);
168-
} else {
169-
close();
170-
}
181+
syncOpenedState(rootRef, opened, getOpenedSide, expand, close);
171182
// 可以保证expand,close正常执行
172183
// eslint-disable-next-line react-hooks/exhaustive-deps
173184
}, [opened, rootRef.current]);

0 commit comments

Comments
 (0)