Skip to content

Commit

Permalink
🚧 progress: Correctly await navigation in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Jan 8, 2025
1 parent d92d53f commit 9244396
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
16 changes: 8 additions & 8 deletions imports/_test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ const forgetHistory = async () => {
};

const mount = isAppTest()
? () => {
_router.navigate('/');
? async () => {
await _router.navigate('/');
}
: // eslint-disable-next-line @typescript-eslint/no-empty-function
() => {};
async () => {};

const unmount = isAppTest()
? () => {
_router.navigate('/_test/unmount');
? async () => {
await _router.navigate('/_test/unmount');
}
: () => {
: async () => {
_unmount();
};

Expand All @@ -136,9 +136,9 @@ export const client = (title, fn) => {
if (Meteor.isClient) {
const cleanup = async () => {
await logout();
unmount();
await unmount();
assertChangeStreamWatchersAreOff();
mount();
await mount();
await call(reset);
};

Expand Down
47 changes: 38 additions & 9 deletions imports/ui/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import React, {useEffect} from 'react';

import {
BrowserRouter,
Routes,
Route,
useNavigate,
type NavigateFunction,
type Path,
type NavigateOptions,
type To,
} from 'react-router-dom';

import {CacheProvider} from '@emotion/react';
Expand All @@ -19,6 +20,8 @@ import CssBaseline from '@mui/material/CssBaseline';

import isTest from '../app/isTest';

import createPromise from '../lib/async/createPromise';

import DateTimeLocalizationProvider from './i18n/DateTimeLocalizationProvider';
import CustomWholeWindowDropZone from './input/CustomWholeWindowDropZone';
import ModalProvider from './modal/ModelProvider';
Expand All @@ -33,21 +36,47 @@ export const muiCache = createCache({
prepend: true,
});

export type NavigateFunction = {
(to: To, options?: NavigateOptions): Promise<void>;
(delta: number): Promise<void>;
};

export const _router: {navigate: NavigateFunction} = {
navigate(_to: Partial<Path> | string | number) {
async navigate(_to: Partial<Path> | string | number) {
console.warn('Using unitialized test-only _navigate function call.');
},
};

const useNavigationEffect = () => {
const navigate = useNavigate();
useEffect(() => {
const _navigation = createPromise<void>();
_router.navigate = async (to: any) => {
navigate(to);
await _navigation.promise;
};

return () => {
_navigation.resolve();
};
}, [navigate]);
};

const UnmountRoute = () => {
useNavigationEffect();
return <div>test-only unmount route</div>;
};

const MainRoute = ({children}) => {
useNavigationEffect();
return {...children};
};

const WithTestRoutes = ({children}) => {
_router.navigate = useNavigate();
return (
<Routes>
<Route
element={<div>test-only unmount route</div>}
path="/_test/unmount"
/>
<Route element={{...children}} path="*" />
<Route element={<UnmountRoute />} path="/_test/unmount" />
<Route element={<MainRoute>{children}</MainRoute>} path="*" />
</Routes>
);
};
Expand Down

0 comments on commit 9244396

Please sign in to comment.