Skip to content

Commit 038f7ab

Browse files
authored
fix: quick switch between microtasks should have correct status (#11)
1 parent e4439eb commit 038f7ab

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/hooks/useStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export default function useStatus(
126126
}
127127
}, [status]);
128128

129-
const [startStep, step] = useStepQueue((newStep) => {
129+
const [startStep, step] = useStepQueue(status, (newStep) => {
130130
// Only prepare step can be skip
131131
if (newStep === STEP_PREPARE) {
132132
const onPrepare = eventHandlers[STEP_PREPARE];

src/hooks/useStepQueue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
STEP_START,
77
STEP_ACTIVATED,
88
STEP_NONE,
9+
MotionStatus,
910
} from '../interface';
1011
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
1112
import useNextFrame from './useNextFrame';
@@ -27,6 +28,7 @@ export function isActive(step: StepStatus) {
2728
}
2829

2930
export default (
31+
status: MotionStatus,
3032
callback: (
3133
step: StepStatus,
3234
) => Promise<void> | void | typeof SkipStep | typeof DoStep,
@@ -68,7 +70,7 @@ export default (
6870
});
6971
}
7072
}
71-
}, [step]);
73+
}, [status, step]);
7274

7375
React.useEffect(
7476
() => () => {

tests/CSSMotion.spec.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('CSSMotion', () => {
186186
expect(boxNode.hasClass('transition-appear-active')).toBeFalsy();
187187
});
188188

189-
it('quick switch should have correct status', () => {
189+
it('quick switch should have correct status', async () => {
190190
const wrapper = mount(
191191
<CSSMotion motionName="transition">
192192
{({ style, className }) => (
@@ -208,7 +208,23 @@ describe('CSSMotion', () => {
208208
wrapper.update();
209209
});
210210

211-
const boxNode = wrapper.find('.motion-box');
211+
let boxNode = wrapper.find('.motion-box');
212+
expect(boxNode.hasClass('transition')).toBeTruthy();
213+
expect(boxNode.hasClass('transition-leave')).toBeTruthy();
214+
expect(boxNode.hasClass('transition-leave-active')).toBeTruthy();
215+
216+
wrapper.setProps({ visible: true });
217+
await act(() => {
218+
return Promise.resolve().then(() => {
219+
wrapper.setProps({ visible: false });
220+
});
221+
});
222+
act(() => {
223+
jest.runAllTimers();
224+
wrapper.update();
225+
});
226+
227+
boxNode = wrapper.find('.motion-box');
212228
expect(boxNode.hasClass('transition')).toBeTruthy();
213229
expect(boxNode.hasClass('transition-leave')).toBeTruthy();
214230
expect(boxNode.hasClass('transition-leave-active')).toBeTruthy();

0 commit comments

Comments
 (0)