-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle links with specified platform and add tests (#6890)
* Handle links with specified platform and add tests * Refactor out href logic into hook * Fix typo * Remove uncessary comment * Revert "Refactor out href logic into hook" This reverts commit 9bb57f7. * Typo * Update jest setup to mock router context * Add comment for doMock * Remove commented out lines
- Loading branch information
Showing
3 changed files
with
158 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import { createContext } from 'react'; | ||
// Setup file to extend jest-dom, referenced in packages.json under "jest" | ||
import '@testing-library/jest-dom'; | ||
|
||
// Adding this doMock help mock next/link in MDXLink.test.tsx | ||
// From here: https://github.com/vercel/next.js/issues/43769#issuecomment-1735620746 | ||
jest.doMock('next/dist/shared/lib/router-context.shared-runtime.js', () => ({ | ||
RouterContext: createContext(true) | ||
})); | ||
|
||
if (typeof window.URL.createObjectURL === 'undefined') { | ||
window.URL.createObjectURL = jest.fn(); | ||
} |
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
140 changes: 140 additions & 0 deletions
140
src/components/MDXComponents/__tests__/MDXLink.test.tsx
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,140 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { MDXLink } from '../MDXLink'; | ||
|
||
const routerMock = { | ||
__esModule: true, | ||
useRouter: () => { | ||
return { | ||
query: { | ||
platform: '' | ||
}, | ||
asPath: '' | ||
}; | ||
} | ||
}; | ||
|
||
jest.mock('next/router', () => routerMock); | ||
|
||
describe('MDXLink', () => { | ||
it('should render external link', () => { | ||
const externalUrl = 'https://amazon.com'; | ||
const linkText = 'External Site'; | ||
|
||
render( | ||
<MDXLink href={externalUrl} hash={undefined}> | ||
{linkText} | ||
</MDXLink> | ||
); | ||
|
||
const linkElement = screen.getByRole('link', { name: linkText }); | ||
expect(linkElement).toBeInTheDocument(); | ||
expect(linkElement).toHaveAttribute('href', externalUrl); | ||
expect(linkElement).toHaveAttribute('rel', 'noopener noreferrer'); | ||
}); | ||
|
||
it('should render internal link', () => { | ||
const href = '/[platform]/build-a-backend/existing-resources/cli'; | ||
const linkText = 'Internal link'; | ||
|
||
routerMock.useRouter = () => { | ||
return { | ||
query: { | ||
platform: 'react' | ||
}, | ||
asPath: '/react/build-a-backend/current-page/' | ||
}; | ||
}; | ||
|
||
render( | ||
<MDXLink href={href} hash={undefined}> | ||
{linkText} | ||
</MDXLink> | ||
); | ||
|
||
const linkElement = screen.getByRole('link', { name: linkText }); | ||
expect(linkElement).toBeInTheDocument(); | ||
expect(linkElement).toHaveAttribute( | ||
'href', | ||
href.replace('[platform]', 'react') | ||
); | ||
}); | ||
|
||
it('should render internal link with hash', () => { | ||
const href = | ||
'/[platform]/build-a-backend/existing-resources/cli#test-hash-title'; | ||
const linkText = 'Internal link'; | ||
|
||
routerMock.useRouter = () => { | ||
return { | ||
query: { | ||
platform: 'react' | ||
}, | ||
asPath: '/react/build-a-backend/current-page/' | ||
}; | ||
}; | ||
|
||
render( | ||
<MDXLink href={href} hash={undefined}> | ||
{linkText} | ||
</MDXLink> | ||
); | ||
|
||
const linkElement = screen.getByRole('link', { name: linkText }); | ||
expect(linkElement).toBeInTheDocument(); | ||
expect(linkElement).toHaveAttribute( | ||
'href', | ||
href.replace('[platform]', 'react') | ||
); | ||
}); | ||
|
||
it('should render hash only link', () => { | ||
const href = '#test-hash-title'; | ||
const linkText = 'Internal link'; | ||
|
||
routerMock.useRouter = () => { | ||
return { | ||
query: { | ||
platform: 'react' | ||
}, | ||
asPath: '/react/build-a-backend/current-page/' | ||
}; | ||
}; | ||
|
||
render( | ||
<MDXLink href={href} hash={undefined}> | ||
{linkText} | ||
</MDXLink> | ||
); | ||
|
||
const linkElement = screen.getByRole('link', { name: linkText }); | ||
expect(linkElement).toBeInTheDocument(); | ||
expect(linkElement).toHaveAttribute( | ||
'href', | ||
'/react/build-a-backend/current-page#test-hash-title' | ||
); | ||
}); | ||
|
||
it('should render with specific platform when specified', () => { | ||
const href = '/react/build-a-backend/existing-resources/cli'; | ||
const linkText = 'Internal link'; | ||
|
||
routerMock.useRouter = () => { | ||
return { | ||
query: { | ||
platform: 'react' | ||
}, | ||
asPath: '/react/build-a-backend/current-page/' | ||
}; | ||
}; | ||
|
||
render( | ||
<MDXLink href={href} hash={undefined}> | ||
{linkText} | ||
</MDXLink> | ||
); | ||
|
||
const linkElement = screen.getByRole('link', { name: linkText }); | ||
expect(linkElement).toBeInTheDocument(); | ||
expect(linkElement).toHaveAttribute('href', href); | ||
}); | ||
}); |