How would I be able to test the router in the code below? When using React you are able to use MemoryRouter to pass initialEntries to mock a route change but I cannot find an alternative for preact-router. I looked at the Preact docs and the preact-router docs but I am unable to find a clear solution.
import 'preact/debug';
import { h, render } from 'preact';
import HomePage from './pages/homepage';
import Router from 'preact-router';
import AsyncRoute from 'preact-async-route';
import './styles/index.scss';
const App = () => (
<Router>
<HomePage path="/" />
<AsyncRoute
path="/admin"
getComponent={ () => import('./pages/admin').then(module => module.default) }
/>
</Router>
);
export default App;
This is a little old, but I figured I would share what I found.
The first and quickest thing to do is to just use the
route
function inpreact-router
.While this works, I don't like that it relies on the brower's real history. Luckily, the
<Router>
component accepts ahistory
prop of typeCustomHistory
. So you can use an in-memory implementation of a History API to make this happen. I think I've seen docs that suggest using thehistory
package - however I had to make an adjustmentNext, update your app to accept a
history
property to pass to the<Router>
Finally, just update the tests to wire your custom history to the app.