-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show on index contributors from github
- Loading branch information
Showing
12 changed files
with
306 additions
and
313 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
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 |
---|---|---|
@@ -1,19 +1,62 @@ | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import { Contributor } from '@/lib/mdx/mdx-utils' | ||
|
||
import { Contributors } from './contributors' | ||
|
||
// HOW TO TEST ASYNC COMPONENTS: https://github.com/vercel/next.js/issues/47131 | ||
/** | ||
* @param {function} Component | ||
* @param {*} props | ||
* @returns {Promise<()=>JSX.Element>} | ||
*/ | ||
async function resolvedComponent(Component, props) { | ||
Check failure on line 11 in components/contributors/contributors.test.tsx GitHub Actions / Typecheck 🧐
|
||
const ComponentResolved = await Component(props) | ||
return () => ComponentResolved | ||
} | ||
|
||
describe('<Contributors />', () => { | ||
test('renders correctly', () => { | ||
const contributors: Contributor[] = [{ github_username: 'nsdonato' }] | ||
global.fetch = jest.fn(() => | ||
Check failure on line 17 in components/contributors/contributors.test.tsx GitHub Actions / Typecheck 🧐
|
||
Promise.resolve({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve([ | ||
{ | ||
id: 1, | ||
login: 'nsdonato', | ||
avatar_url: 'https://example.com/avatar1.jpg', | ||
html_url: 'https://example.com/profile1', | ||
contributions: 0, | ||
}, | ||
]), | ||
}) | ||
) | ||
|
||
test('renders correctly from index', async () => { | ||
const AsyncRscComponent = await resolvedComponent(Contributors, { | ||
isIndex: true, | ||
}) | ||
render(<AsyncRscComponent />) | ||
|
||
const avatar = screen.getByTestId('contributor-nsdonato') | ||
|
||
expect(screen.getByText('Colaboradores')).toBeInTheDocument() | ||
expect( | ||
screen.getByText('Gracias a todxs los que aportaron en este proyecto ✨') | ||
).toBeInTheDocument() | ||
expect(avatar).toBeInTheDocument() | ||
}) | ||
|
||
render(<Contributors contributors={contributors} />) | ||
const avatar = screen.getByRole('img', { | ||
name: /nsdonato/i, | ||
}) | ||
test('renders correctly from slug', async () => { | ||
const AsyncRscComponent = await resolvedComponent(Contributors, { | ||
isIndex: false, | ||
contributors: [ | ||
{ | ||
github_username: 'vamoacodear', | ||
}, | ||
], | ||
}) | ||
render(<AsyncRscComponent />) | ||
|
||
expect(screen.getByText('Colaboradores')).toBeInTheDocument() | ||
expect(avatar).toBeInTheDocument() | ||
}) | ||
const avatar = screen.getByTestId('contributor-vamoacodear') | ||
expect(avatar).toBeInTheDocument() | ||
}) | ||
}) |
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,43 +1,54 @@ | ||
import Image from 'next/image' | ||
|
||
import { Contributor } from '@/lib/mdx/mdx-utils' | ||
import { adaptContributorsFromMdx } from '@/services/github/adapters' | ||
import { getContributorsFromGitHub } from '@/services/github/get-contributors' | ||
|
||
import { WebLink } from '../web-link/web-link' | ||
|
||
type ContributorsProp = { | ||
contributors: Contributor[] | [] | ||
contributors?: Contributor[] | ||
isIndex?: boolean | ||
} | ||
|
||
export const Contributors = ({ contributors }: ContributorsProp) => { | ||
if (!contributors?.length) { | ||
return null | ||
} | ||
export const Contributors = async ({ | ||
contributors = [], | ||
isIndex = false, | ||
}: ContributorsProp) => { | ||
let data = [] | ||
|
||
if (isIndex) { | ||
data = await getContributorsFromGitHub() | ||
} else { | ||
data = adaptContributorsFromMdx(contributors) | ||
} | ||
|
||
return ( | ||
<div className='card bg-base-100 shadow-lg ring-1 mt-4 p-4 w-fit'> | ||
<h2 className='card-title mb-4 text-xl'>Colaboradores</h2> | ||
<p className='mb-4'> | ||
Gracias a todxs los que aportaron en este documento! ✨ | ||
</p> | ||
<ul className='flex flex-wrap gap-2'> | ||
{contributors.map(({ github_username }) => { | ||
return ( | ||
<li key={`contributor-${github_username}`}> | ||
<WebLink | ||
href={`https://github.com/${github_username}`} | ||
> | ||
<Image | ||
loading='lazy' | ||
src={`https://github.com/${github_username}.png?size=80`} | ||
width='40' | ||
height='40' | ||
alt={`Usuarix contribuidxr - ${github_username}`} | ||
className='rounded-full' | ||
/> | ||
</WebLink> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
</div> | ||
) | ||
return ( | ||
<div className='card bg-base-100 shadow-lg ring-1 mt-4 p-4 w-fit'> | ||
<h2 className='card-title mb-4 text-xl'>Colaboradores</h2> | ||
<p className='mb-4'> | ||
Gracias a todxs los que aportaron en este{' '} | ||
{isIndex ? 'proyecto' : 'documento!'} ✨ | ||
</p> | ||
<ul className='flex flex-wrap gap-2'> | ||
{data.map(contributor => { | ||
return ( | ||
<li key={`contributor-${contributor.name}`}> | ||
<WebLink href={contributor.profile}> | ||
<Image | ||
data-testid={`contributor-${contributor.name}`} | ||
loading='lazy' | ||
src={contributor.avatar} | ||
width='40' | ||
height='40' | ||
alt={`Usuarix contribuidxr - ${contributor.name}`} | ||
className='rounded-full' | ||
/> | ||
</WebLink> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.