-
Notifications
You must be signed in to change notification settings - Fork 9
Allow users to configure custom link component #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
virginiacc
wants to merge
6
commits into
main
Choose a base branch
from
vcc-link-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab781e1
Add ability to configure DSR to use router link component.
virginiacc 6d154d8
Link test updates
virginiacc 553a1f6
Revert link icon changes.
virginiacc 69dd2ed
Restore icon test
virginiacc 1e8ba7d
Use relative imports
virginiacc bbf6a8c
Remove react-router dependency and update DSRContext imports and prov…
virginiacc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,20 @@ | ||
| import { ReactNode } from "react"; | ||
| import type { JSXElement } from "~/src/types/jsx-element"; | ||
|
virginiacc marked this conversation as resolved.
Outdated
|
||
|
|
||
| export interface BaseLinkProperties { | ||
| to: string | undefined; | ||
| children: ReactNode; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| export const BaseLink = ({ | ||
| to, | ||
| children, | ||
| ...others | ||
| }: BaseLinkProperties): JSXElement | null => { | ||
| return ( | ||
| <a href={to} {...others}> | ||
| {children} | ||
| </a> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,16 +1,35 @@ | ||||||
| import '@testing-library/jest-dom'; | ||||||
| import { render, screen } from '@testing-library/react'; | ||||||
| import { MemoryRouter } from 'react-router'; | ||||||
| import Link, { LinkText, ListLink } from './link'; | ||||||
| import { DSRContext } from '~/src/context/dsr-context'; | ||||||
|
virginiacc marked this conversation as resolved.
Outdated
|
||||||
| import { ReactNode } from 'react'; | ||||||
| import type { JSXElement } from "~/src/types/jsx-element"; | ||||||
|
virginiacc marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| describe('<Link />', () => { | ||||||
| const linkBaseProperties = { | ||||||
| href: '/foo/bar', | ||||||
| to: '/foo/bar', | ||||||
| 'data-testid': 'link-test-id', | ||||||
| label: 'some link', | ||||||
| }; | ||||||
| const testId = linkBaseProperties['data-testid']; | ||||||
|
|
||||||
| interface CustomLinkProperties { | ||||||
| to: string | undefined; | ||||||
| children: ReactNode; | ||||||
| } | ||||||
|
|
||||||
| const CustomLinkComponent = ({ | ||||||
| to, | ||||||
| children, | ||||||
| ...others | ||||||
| }: CustomLinkProperties): JSXElement | null => { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The null is unnecessary here since the custom type returns that.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in bbf6a8c |
||||||
| return ( | ||||||
| <a href={to} {...others} data-testid='link-component-from-context'> | ||||||
| {children} | ||||||
| </a> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| it('Type: "default"', () => { | ||||||
| render(<Link {...linkBaseProperties} />); | ||||||
| const link = screen.getByTestId(testId); | ||||||
|
|
@@ -79,42 +98,28 @@ describe('<Link />', () => { | |||||
| expect(link).toHaveAttribute('target', '_blank'); | ||||||
| }); | ||||||
|
|
||||||
| it('Option: isRouterLink - it renders a router link', () => { | ||||||
| it('Context: uses link component configured in context', () => { | ||||||
| render( | ||||||
| <MemoryRouter initialEntries={['/foo/bar']}> | ||||||
| <Link {...linkBaseProperties} isRouterLink /> | ||||||
| </MemoryRouter>, | ||||||
| <DSRContext value={{LinkComponent:CustomLinkComponent}}> | ||||||
| <Link {...linkBaseProperties}> | ||||||
| <span data-testid='link-child'>Child</span> | ||||||
| </Link> | ||||||
| </DSRContext>, | ||||||
| ); | ||||||
|
|
||||||
| const link = screen.getByRole('link', { name: /some link/i }); | ||||||
| expect(link).toHaveAttribute('href', '/foo/bar'); | ||||||
| expect(screen.getByTestId('link-component-from-context')).toBeInTheDocument(); | ||||||
|
|
||||||
| }); | ||||||
|
|
||||||
| it('Option: isRouterLink - it renders children and icons', async () => { | ||||||
| it('Context: uses base link component by default', () => { | ||||||
| render( | ||||||
| <MemoryRouter initialEntries={['/foo/bar']}> | ||||||
| <Link {...linkBaseProperties} isRouterLink iconLeft='left'> | ||||||
| <Link {...linkBaseProperties}> | ||||||
| <span data-testid='link-child'>Child</span> | ||||||
| </Link> | ||||||
| </MemoryRouter>, | ||||||
| </Link>, | ||||||
| ); | ||||||
|
|
||||||
| const link = screen.getByRole('link', { name: /some link/i }); | ||||||
| expect(link).toHaveClass('a-link'); | ||||||
| expect(screen.getByTestId('link-child')).toBeInTheDocument(); | ||||||
| expect(screen.getByText('some link')).toHaveClass('a-link__text'); | ||||||
| expect(await screen.findByTestId('link-icon-left')).toBeInTheDocument(); | ||||||
| }); | ||||||
|
|
||||||
| it('Option: isRouterLink - it requires href', () => { | ||||||
| const brokenProperties = { | ||||||
| ...linkBaseProperties, | ||||||
| href: undefined as unknown as string, | ||||||
| }; | ||||||
| expect(screen.queryByTestId('link-component-from-context')).not.toBeInTheDocument(); | ||||||
|
|
||||||
| expect(() => render(<Link {...brokenProperties} isRouterLink />)).toThrow( | ||||||
| 'Link component: href is a required attribute when isRouterLink is true', | ||||||
| ); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
|
|
@@ -131,7 +136,7 @@ describe('<ListLink>', () => { | |||||
| const testId = 'list-link'; | ||||||
|
|
||||||
| it('includes all expected elements', () => { | ||||||
| render(<ListLink data-testid={testId} href='/foo/bar' label='Test text' />); | ||||||
| render(<ListLink data-testid={testId} to='/foo/bar' label='Test text' />); | ||||||
| // ListItem | ||||||
| const listItem = screen.getByRole('listitem'); | ||||||
| expect(listItem).toBeInTheDocument(); | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.