-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathreact-router-03.js
46 lines (43 loc) · 1.39 KB
/
react-router-03.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react'
import {Router} from 'react-router-dom'
import {createMemoryHistory} from 'history'
import {render as rtlRender, fireEvent} from '@testing-library/react'
import {Main} from '../main'
// normally you'd put this logic in your test utility file so it can be used
// for all of your tests.
function render(
ui,
{
route = '/',
history = createMemoryHistory({initialEntries: [route]}),
...renderOptions
} = {},
) {
function Wrapper({children}) {
return <Router history={history}>{children}</Router>
}
return {
...rtlRender(ui, {
wrapper: Wrapper,
...renderOptions,
}),
// adding `history` to the returned utilities to allow us
// to reference it in our tests (just try to avoid using
// this to test implementation details).
history,
}
}
test('main renders about and home and I can navigate to those pages', () => {
const {getByRole, getByText} = render(<Main />)
expect(getByRole('heading')).toHaveTextContent(/home/i)
fireEvent.click(getByText(/about/i))
expect(getByRole('heading')).toHaveTextContent(/about/i)
// you can use the `within` function to get queries for elements within the
// about screen
})
test('landing on a bad page shows no match component', () => {
const {getByRole} = render(<Main />, {
route: '/something-that-does-not-match',
})
expect(getByRole('heading')).toHaveTextContent(/404/i)
})