Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions test/regressions/TestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as React from 'react';
import { useLocation } from 'react-router';
import { styled } from '@mui/material/styles';
import GlobalStyles from '@mui/material/GlobalStyles';
import { fakeClock, setupFakeClock } from '../utils/setupFakeClock'; // eslint-disable-line
// eslint-disable-next-line import/no-relative-packages
import { fakeClock, setupFakeClock } from '../utils/setupFakeClock';

const StyledBox = styled('div', {
shouldForwardProp: (prop) => prop !== 'isDataGridTest' && prop !== 'isDataGridPivotTest',
Expand Down Expand Up @@ -75,15 +76,16 @@ function TestViewer(props: any) {
}

function MockTime(props: React.PropsWithChildren<{ shouldAdvanceTime: boolean }>) {
const [ready, setReady] = React.useState(false);
const [dispose, setDispose] = React.useState<(() => void) | null>(null);
const [prevShouldAdvanceTime, setPrevShouldAdvanceTime] = React.useState(props.shouldAdvanceTime);
Comment on lines +79 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need state for these? Wouldn't refs work?

Copy link
Member Author

@Janpot Janpot Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per https://react.dev/reference/react/useRef#caveats, I don't believe a ref would be safe under concurrent rendering.

Do not write or read ref.current during rendering, except for initialization. This makes your component’s behavior unpredictable.


React.useEffect(() => {
const dispose = setupFakeClock(props.shouldAdvanceTime);
setReady(true);
return dispose;
}, [props.shouldAdvanceTime]);
if (!dispose || prevShouldAdvanceTime !== props.shouldAdvanceTime) {
dispose?.();
setDispose(() => setupFakeClock(props.shouldAdvanceTime));
setPrevShouldAdvanceTime(props.shouldAdvanceTime);
}

return ready ? props.children : null;
Copy link
Member Author

@Janpot Janpot Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little side-quest in some weird code I ran into. It's problematic when shouldAdvanceTime prop changes while ready is true it would render the children once with the wrong fake clock. We can get rid of the effect and instead set up the clock straight in the render.

return props.children;
}

function LoadFont(props: any) {
Expand Down
16 changes: 5 additions & 11 deletions test/regressions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import { createBrowserRouter, RouterProvider, Outlet, NavLink, useNavigate } from 'react-router';
import { Globals } from '@react-spring/web';
import { setupFakeClock, restoreFakeClock } from '../utils/setupFakeClock'; // eslint-disable-line
import { generateTestLicenseKey, setupTestLicenseKey } from '../utils/testLicense'; // eslint-disable-line
// eslint-disable-next-line import/no-relative-packages
import '../utils/setupFakeClock';
// eslint-disable-next-line import/no-relative-packages
import { generateTestLicenseKey, setupTestLicenseKey } from '../utils/testLicense';
import TestViewer from './TestViewer';
import type { Test } from './testsBySuite';
import { type Test, testsBySuite } from './testsBySuite';

setupTestLicenseKey(generateTestLicenseKey(new Date('2099-01-01')));

Expand All @@ -29,17 +31,9 @@ window.muiFixture = {
},
};

let testsBySuite: typeof import('./testsBySuite').testsBySuite;

main();

async function main() {
setupFakeClock();

testsBySuite = (await import('./testsBySuite')).testsBySuite;

restoreFakeClock();

ReactDOM.createRoot(document.getElementById('react-root')!).render(<App />);
}

Expand Down
21 changes: 21 additions & 0 deletions test/regressions/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import { defineConfig, transformWithEsbuild } from 'vite';
import react from '@vitejs/plugin-react';
import * as fs from 'fs/promises';
import { alias } from '../../vitest.shared.mts';

export default defineConfig({
Expand All @@ -20,6 +21,26 @@ export default defineConfig({
worker: {
format: 'es',
},
optimizeDeps: {
esbuildOptions: {
plugins: [
{
name: 'js-as-jsx',
setup(build) {
build.onLoad({ filter: /\.js$/ }, async (args) => {
if (args.path.includes('/node_modules/')) {
return null;
}

const contents = await fs.readFile(args.path, 'utf8');

return { contents, loader: 'jsx' };
});
},
},
],
},
},
plugins: [
react(),
{
Expand Down
Loading