-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nav-link): migrates to mui components
Migrates that NavLink to use Mui Components re #143
- Loading branch information
Showing
6 changed files
with
121 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { faBars } from '@fortawesome/free-solid-svg-icons'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { NextRouter } from 'next/router'; | ||
import NavLink from './NavLink'; | ||
|
||
jest.mock<typeof import('next/router')>('next/router', () => ({ | ||
...jest.requireActual<typeof import('next/router')>('next/router'), | ||
useRouter: () => | ||
({ | ||
asPath: '/', | ||
} as NextRouter), | ||
})); | ||
|
||
describe('<NavLink />', () => { | ||
const setupNavLink = (props: object = { label: 'Link' }) => { | ||
return render(<NavLink {...props} />); | ||
}; | ||
|
||
it('renders the NavLink', () => { | ||
expect.assertions(1); | ||
|
||
setupNavLink(); | ||
|
||
expect(screen.getByRole('link')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders the NavLink with the correct href', () => { | ||
expect.assertions(1); | ||
|
||
setupNavLink({ href: '/' }); | ||
|
||
expect(screen.getByRole('link')).toHaveAttribute('href', '/'); | ||
}); | ||
|
||
it('should render the label', () => { | ||
expect.assertions(1); | ||
|
||
setupNavLink({ label: 'Home' }); | ||
|
||
expect(screen.getByRole('link', { name: 'Home' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render the children', () => { | ||
expect.assertions(1); | ||
|
||
setupNavLink({ children: 'Home' }); | ||
|
||
expect(screen.getByText('Home')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render the children over the label', () => { | ||
expect.assertions(2); | ||
|
||
setupNavLink({ children: 'Home', label: 'Nope' }); | ||
|
||
expect(screen.getByText('Home')).toBeInTheDocument(); | ||
expect(screen.queryByText('Nope')).toBeNull(); | ||
}); | ||
|
||
it('should display an icon', () => { | ||
expect.assertions(2); | ||
|
||
const icon = <FontAwesomeIcon icon={faBars} data-testid="icon" />; | ||
|
||
setupNavLink({ icon }); | ||
|
||
expect(screen.getByTestId('icon')).toBeInTheDocument(); | ||
expect(screen.getByTestId('icon')).toHaveAttribute('data-icon', 'bars'); | ||
}); | ||
|
||
it('should use the default style', () => { | ||
expect.assertions(1); | ||
|
||
setupNavLink({ href: '/test' }); | ||
|
||
expect(screen.getByRole('link')).toHaveStyle('background: var(--chakra-colors-blue-700);'); | ||
}); | ||
|
||
it('should use the transparent color scheme', () => { | ||
expect.assertions(1); | ||
|
||
setupNavLink({ href: '/test', variant: 'transparent' }); | ||
|
||
expect(screen.getByRole('link')).toHaveStyle('background-color: transparent'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import NextLink from 'next/link'; | ||
import { Button, ButtonProps } from '@mui/material'; | ||
|
||
export interface NavLinkProps extends ButtonProps { | ||
href?: string; | ||
label?: React.ReactNode; | ||
children?: React.ReactNode; | ||
|
||
icon?: React.ReactNode; | ||
|
||
component?: React.ElementType; | ||
} | ||
|
||
function NavLink({ href = '#', label, children, icon, ...props }: NavLinkProps) { | ||
return ( | ||
<Button component={NextLink} href={href} startIcon={icon} {...props}> | ||
{children || label} | ||
</Button> | ||
); | ||
} | ||
|
||
NavLink.defaultProps = { | ||
href: '#', | ||
label: '', | ||
children: null, | ||
icon: null, | ||
}; | ||
|
||
export default NavLink; |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.