import React from 'react'
import {render} from '@testing-library/react'
import {
Router,
Link,
createHistory,
createMemorySource,
LocationProvider,
} from '@reach/router'
import '@testing-library/jest-dom'
const About = () => <div>You are on the about page</div>
const Home = () => <div>You are home</div>
const NoMatch = () => <div>No match</div>
function App() {
return (
<div>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Router>
<Home path="/" />
<About path="/about" />
<NoMatch default />
</Router>
</div>
)
}
function renderWithRouter(
ui,
{route = '/', history = createHistory(createMemorySource(route))} = {},
) {
return {
...render(<LocationProvider history={history}>{ui}</LocationProvider>),
history,
}
}
test('full app rendering/navigating', async () => {
const {
container,
history: {navigate},
} = renderWithRouter(<App />)
const appContainer = container
expect(appContainer.innerHTML).toMatch('You are home')
await navigate('/about')
expect(container.innerHTML).toMatch('You are on the about page')
})
test('landing on a bad page', () => {
const {container} = renderWithRouter(<App />, {
route: '/something-that-does-not-match',
})
expect(container.innerHTML).toMatch('No match')
})
const Routes = () => (
<Router>
<SomeComponent path="/some-component/:id" />
</Router>
)
function renderWithRouterWrapper(
ui,
{route = '/', history = createHistory(createMemorySource(route))} = {},
) {
return {
...render(
<LocationProvider history={history}>
<Router>{ui}</Router>
</LocationProvider>,
),
history,
}
}
test('renders the component with params', () => {
renderWithRouterWrapper(<SomeComponent path="/some-component/:id" />, {
route: '/some-component/1',
})
})