mirror of
https://github.com/nexu-io/open-design.git
synced 2026-06-01 03:14:35 +07:00
* fix(web/router): defer popstate dispatch to microtask navigate() previously dispatched a synchronous popstate event after mutating window.history, which caused React 18 to emit: Cannot update a component (Router) while rendering a different component (App). To locate the bad setState() call inside App, follow the stack trace as described in https://react.dev/link/setstate-in-render This happens whenever a caller invokes navigate() from inside a useState updater (e.g. App.tsx:479 routing first-run users through the onboarding panel from inside the setConfig() update). The synchronous popstate dispatch reaches useRoute() subscribers which then call setRoute() while the parent component is still rendering. Defer the popstate dispatch to a microtask. The window.history call itself stays synchronous so the URL bar updates immediately; only subscriber updates are pushed past the current render commit, which removes the warning without changing observable behaviour for any existing caller. * fix(web/router): cover deferred navigation timing --------- Co-authored-by: Visionboost <contact@visionboost.fr> Co-authored-by: Siri-Ray <2667192167@qq.com>
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { act, cleanup, render, screen, waitFor } from '@testing-library/react';
|
|
import { useEffect, useState } from 'react';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { navigate, useRoute } from '../src/router';
|
|
|
|
function RouteLabel() {
|
|
const route = useRoute();
|
|
const label = route.kind === 'home' ? route.view : route.kind;
|
|
return <div data-testid="route-label">{label}</div>;
|
|
}
|
|
|
|
function NavigateFromUpdater() {
|
|
const [didNavigate, setDidNavigate] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (didNavigate) return;
|
|
setDidNavigate(() => {
|
|
navigate({ kind: 'home', view: 'onboarding' }, { replace: true });
|
|
return true;
|
|
});
|
|
}, [didNavigate]);
|
|
|
|
return <RouteLabel />;
|
|
}
|
|
|
|
async function flushMicrotasks() {
|
|
await act(async () => {
|
|
await Promise.resolve();
|
|
});
|
|
}
|
|
|
|
describe('navigate / useRoute timing', () => {
|
|
let consoleError: ReturnType<typeof vi.spyOn>;
|
|
|
|
beforeEach(() => {
|
|
window.history.replaceState(null, '', '/');
|
|
consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
consoleError.mockRestore();
|
|
window.history.replaceState(null, '', '/');
|
|
});
|
|
|
|
it('updates history synchronously and notifies listeners after the microtask boundary', async () => {
|
|
const onPop = vi.fn();
|
|
window.addEventListener('popstate', onPop);
|
|
|
|
navigate({ kind: 'home', view: 'onboarding' }, { replace: true });
|
|
|
|
expect(window.location.pathname).toBe('/onboarding');
|
|
expect(onPop).not.toHaveBeenCalled();
|
|
|
|
await flushMicrotasks();
|
|
|
|
expect(onPop).toHaveBeenCalledTimes(1);
|
|
window.removeEventListener('popstate', onPop);
|
|
});
|
|
|
|
it('updates route subscribers after render-phase updater navigation without React warnings', async () => {
|
|
render(<NavigateFromUpdater />);
|
|
|
|
await flushMicrotasks();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByTestId('route-label').textContent).toBe('onboarding');
|
|
});
|
|
expect(window.location.pathname).toBe('/onboarding');
|
|
|
|
const warningCalls = consoleError.mock.calls.filter((call: unknown[]) =>
|
|
String(call[0]).includes('Cannot update a component'),
|
|
);
|
|
expect(warningCalls).toEqual([]);
|
|
});
|
|
});
|