1 | import React from 'react';
|
2 | import { Link, Route, Router, Switch } from 'react-router-dom';
|
3 | import { withRouter } from 'react-router';
|
4 | import { createMemoryHistory } from 'history';
|
5 | import { render, fireEvent, cleanup } from '@testing-library/react';
|
6 | import { BreadcrumbItem } from 'reactstrap';
|
7 | import Breadcrumbs from '../index';
|
8 |
|
9 | const StartPage = () => (
|
10 | <div>
|
11 | Start page content
|
12 | <Breadcrumbs active="react-router-demo-page">
|
13 | <BreadcrumbItem>
|
14 | <Link to="react-router-destination">React Router Breadcrumb</Link>
|
15 | </BreadcrumbItem>
|
16 | </Breadcrumbs>
|
17 | </div>
|
18 | );
|
19 |
|
20 | const ReactRouterDestination = () => <div>parent page of start page</div>;
|
21 | const NoMatch = () => <div>No match</div>;
|
22 |
|
23 | const LocationDisplay = withRouter(({ location }) => (
|
24 | <div data-testid="location-display">{location.pathname}</div>
|
25 | ));
|
26 |
|
27 | function App() {
|
28 | return (
|
29 | <div>
|
30 | <Switch>
|
31 | <Route exact path="/start" component={StartPage} />
|
32 | <Route
|
33 | path="/react-router-destination"
|
34 | component={ReactRouterDestination}
|
35 | />
|
36 | <Route component={NoMatch} />
|
37 | </Switch>
|
38 | <LocationDisplay />
|
39 | </div>
|
40 | );
|
41 | }
|
42 |
|
43 | afterEach(cleanup);
|
44 |
|
45 | function renderWithRouter(
|
46 | ui,
|
47 | {
|
48 | route = '/start',
|
49 | history = createMemoryHistory({ initialEntries: [route] }),
|
50 | } = {}
|
51 | ) {
|
52 | return {
|
53 | ...render(<Router history={history}>{ui}</Router>),
|
54 |
|
55 |
|
56 |
|
57 | history,
|
58 | };
|
59 | }
|
60 |
|
61 | test('full app rendering/navigating', () => {
|
62 | const { container, getByText } = renderWithRouter(<App />);
|
63 | expect(container.innerHTML).toMatch('Start page content');
|
64 | const leftClick = { button: 0 };
|
65 | fireEvent.click(getByText(/React Router Breadcrumb/i), leftClick);
|
66 | expect(container.innerHTML).toMatch('parent page of start page');
|
67 | });
|
68 |
|
69 | test('landing on a bad page', () => {
|
70 | const { container } = renderWithRouter(<App />, {
|
71 | route: '/something-that-does-not-match',
|
72 | });
|
73 | expect(container.innerHTML).toMatch('No match');
|
74 | });
|
75 |
|
76 | test('rendering a component that uses a single breadcrumb', () => {
|
77 | const route = '/some-route';
|
78 | const { getByTestId } = renderWithRouter(<LocationDisplay />, { route });
|
79 | expect(getByTestId('location-display').textContent).toBe(route);
|
80 | });
|
81 |
|
82 | const StartPageMultiBreadcrumbItems = () => (
|
83 | <div>
|
84 | Start page content
|
85 | <Breadcrumbs active="react-router-demo-page">
|
86 | <BreadcrumbItem>
|
87 | <a href="/static/link">Static Breadcrumb</a>
|
88 | </BreadcrumbItem>
|
89 | <BreadcrumbItem>
|
90 | <Link to="react-router-destination">React Router Breadcrumb</Link>
|
91 | </BreadcrumbItem>
|
92 | </Breadcrumbs>
|
93 | </div>
|
94 | );
|
95 |
|
96 | function AppMultiBreadcrumb() {
|
97 | return (
|
98 | <div>
|
99 | <Switch>
|
100 | <Route exact path="/start" component={StartPageMultiBreadcrumbItems} />
|
101 | <Route
|
102 | path="/react-router-destination"
|
103 | component={ReactRouterDestination}
|
104 | />
|
105 | <Route component={NoMatch} />
|
106 | </Switch>
|
107 | <LocationDisplay />
|
108 | </div>
|
109 | );
|
110 | }
|
111 |
|
112 | test('full app rendering with multiple breadcrumbs', () => {
|
113 | const { container, getByText } = renderWithRouter(<AppMultiBreadcrumb />);
|
114 | expect(container.innerHTML).toMatch('Start page content');
|
115 | const leftClick = { button: 0 };
|
116 | fireEvent.click(getByText(/React Router Breadcrumb/i), leftClick);
|
117 | // normally I'd use a data-testid, but just wanted to show this is also possible
|
118 | expect(container.innerHTML).toMatch('parent page of start page');
|
119 | });
|
120 |
|
121 | const StartPageNoBreadcrumbItems = () => (
|
122 | <div>
|
123 | Start page content
|
124 | <Breadcrumbs active="react-router-demo-page" />
|
125 | </div>
|
126 | );
|
127 |
|
128 | function AppNoBreadcrumbItems() {
|
129 | return (
|
130 | <div>
|
131 | <Switch>
|
132 | <Route exact path="/start" component={StartPageNoBreadcrumbItems} />
|
133 | <Route component={NoMatch} />
|
134 | </Switch>
|
135 | <LocationDisplay />
|
136 | </div>
|
137 | );
|
138 | }
|
139 |
|
140 | test('full app rendering with no breadcrumbitems', () => {
|
141 | const { container } = renderWithRouter(<AppNoBreadcrumbItems />);
|
142 | expect(container.innerHTML).toMatch('Start page content');
|
143 | });
|