-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmain-01.test.js
67 lines (53 loc) · 2.24 KB
/
main-01.test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'jest-dom/extend-expect';
import 'react-testing-library/cleanup-after-each';
import React from 'react';
import {render, fireEvent} from 'react-testing-library';
// instead of using BrowserRouter component, we'll use the Router component
// directly so that we can pass in our own history explicitly
import {Router} from 'react-router';
// We can also do this by importing MemoryRouter from react-router-dom to save
// us a step in creating history
import {MemoryRouter} from 'react-router-dom';
// createMemoryHistory will allow us to create a history object that can be
// passed to Router
import {createMemoryHistory} from 'history';
import {Main} from '../src/main-01';
describe('Main', () => {
test('can navigate to about (using Router)', () => {
const history = createMemoryHistory({initialEntries: ['/']});
const {getByTestId, getByText, queryByTestId} = render(
<Router history={history}>
<Main />
</Router>
);
expect(getByTestId('home-route')).toBeInTheDocument();
expect(queryByTestId('about-route')).not.toBeInTheDocument();
const aboutLink = getByText(/about/i);
fireEvent.click(aboutLink);
expect(queryByTestId('home-route')).not.toBeInTheDocument();
expect(getByTestId('about-route')).toBeInTheDocument();
});
test('can navigate to about (using MemoryRouter)', () => {
const {debug, getByTestId, getByText, queryByTestId, rerender} = render(
<MemoryRouter initialEntries={['/']}>
<Main />
</MemoryRouter>
);
expect(getByTestId('home-route')).toBeInTheDocument();
expect(queryByTestId('about-route')).not.toBeInTheDocument();
const aboutLink = getByText(/about/i);
fireEvent.click(aboutLink);
expect(queryByTestId('home-route')).not.toBeInTheDocument();
expect(getByTestId('about-route')).toBeInTheDocument();
});
test('displays no match route when no match', () => {
const {debug, getByTestId, getByText, queryByTestId, rerender} = render(
<MemoryRouter initialEntries={['/foo']}>
<Main />
</MemoryRouter>
);
expect(getByTestId('no-match-route')).toBeInTheDocument();
expect(queryByTestId('home-route')).not.toBeInTheDocument();
expect(queryByTestId('about-route')).not.toBeInTheDocument();
});
});