-
Notifications
You must be signed in to change notification settings - Fork 81
/
main-02.test.js
55 lines (45 loc) · 1.7 KB
/
main-02.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
import 'jest-dom/extend-expect';
import 'react-testing-library/cleanup-after-each';
import React from 'react';
import {render as rtlRender, fireEvent} from 'react-testing-library';
import {Router} from 'react-router';
import {createMemoryHistory} from 'history';
import {Main} from '../src/main-02';
// create a custom render function that will render any UI inside a configurable
// Router
const render = (
ui,
// set a default options parameter
{
route = '/',
history = createMemoryHistory({initialEntries: [route]}),
...options
} = {}
) => {
// return everything react-testing-library's render returns, as well as history
return {
...rtlRender(<Router history={history}>{ui}</Router>),
history,
};
};
describe('Main', () => {
test('can navigate to about (using Router)', () => {
const history = createMemoryHistory({initialEntries: ['/']});
// use our custom render, making it appear as if we're simply rendering Main
const {getByTestId, getByText, queryByTestId} = render(<Main />);
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} = render(<Main />, {
route: '/foo',
});
expect(getByTestId('no-match-route')).toBeInTheDocument();
expect(queryByTestId('home-route')).not.toBeInTheDocument();
expect(queryByTestId('about-route')).not.toBeInTheDocument();
});
});