Skip to content

Commit 7ff4aa3

Browse files
committed
RouterTab.tsx: add types and add router dom types and unit test
1 parent bf21c46 commit 7ff4aa3

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed

client/common/RouterTab.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import { MemoryRouter } from 'react-router-dom';
4+
import Tab from './RouterTab';
5+
6+
describe('Tab', () => {
7+
it('renders a NavLink with correct text and link', () => {
8+
render(
9+
<MemoryRouter>
10+
<Tab to="/dashboard">Dashboard</Tab>
11+
</MemoryRouter>
12+
);
13+
14+
const linkElement = screen.getByText('Dashboard');
15+
expect(linkElement).toBeInTheDocument();
16+
expect(linkElement.getAttribute('href')).toBe('/dashboard');
17+
});
18+
19+
it('includes the dashboard-header class names', () => {
20+
const { container } = render(
21+
<MemoryRouter>
22+
<Tab to="/settings">Settings</Tab>
23+
</MemoryRouter>
24+
);
25+
26+
const listItem = container.querySelector('li');
27+
const link = container.querySelector('a');
28+
29+
expect(listItem).toHaveClass('dashboard-header__tab');
30+
expect(link).toHaveClass('dashboard-header__tab__title');
31+
});
32+
});

client/common/RouterTab.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import PropTypes from 'prop-types';
2-
import React from 'react';
1+
import React, { ReactNode } from 'react';
32
import { NavLink } from 'react-router-dom';
43

4+
type TabProps = {
5+
children: ReactNode,
6+
to: string
7+
};
58
/**
69
* Wraps the react-router `NavLink` with dashboard-header__tab styling.
710
*/
8-
const Tab = ({ children, to }) => (
11+
const Tab = ({ children, to }: TabProps) => (
912
<li className="dashboard-header__tab">
1013
<NavLink
1114
className="dashboard-header__tab__title"
@@ -17,9 +20,4 @@ const Tab = ({ children, to }) => (
1720
</li>
1821
);
1922

20-
Tab.propTypes = {
21-
children: PropTypes.string.isRequired,
22-
to: PropTypes.string.isRequired
23-
};
24-
2523
export default Tab;

package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"@types/node": "^16.18.126",
131131
"@types/react": "^16.14.0",
132132
"@types/react-dom": "^16.9.25",
133+
"@types/react-router-dom": "^5.3.3",
133134
"@typescript-eslint/eslint-plugin": "^5.62.0",
134135
"@typescript-eslint/parser": "^5.62.0",
135136
"babel-core": "^7.0.0-bridge.0",

0 commit comments

Comments
 (0)