diff --git a/code/core/src/component-testing/components/Panel.tsx b/code/core/src/component-testing/components/Panel.tsx index e0eb32c063ca..ced73de7b8f3 100644 --- a/code/core/src/component-testing/components/Panel.tsx +++ b/code/core/src/component-testing/components/Panel.tsx @@ -250,7 +250,7 @@ export const Panel = memo<{ refId?: string; storyId: string; storyUrl: string }> }, []); const lastStoryId = useRef(undefined); - const lastRenderId = useRef(0); + const latestRenderId = useRef(0); const emit = useChannel( { [EVENTS.CALL]: setCall, @@ -273,9 +273,18 @@ export const Panel = memo<{ refId?: string; storyId: string; storyUrl: string }> return; } - lastStoryId.current = event.storyId; - lastRenderId.current = Math.max(lastRenderId.current, event.renderId || 0); - if (lastRenderId.current !== event.renderId) { + // Update lastRenderId and lastStoryId. When we switch stories, lastRenderId's + // value might decrease if our users have mocked Date.now() via addons or + // manually in their code, so we must reset it. + if (lastStoryId.current === event.storyId) { + latestRenderId.current = Math.max(latestRenderId.current, event.renderId || 0); + } else { + latestRenderId.current = event.renderId || 0; + lastStoryId.current = event.storyId; + } + + // Bail out if concurrent renders are ongoing for the same story (only keep the latest one). + if (latestRenderId.current !== event.renderId) { return; }