-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from JohnsonMao/feature/posts-list
✨ add posts list and clamp utils
- Loading branch information
Showing
9 changed files
with
130 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use client'; | ||
|
||
import { memo } from 'react'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import Link from '@/components/Link'; | ||
import List from '@/components/List'; | ||
import { clamp } from '@/utils/math'; | ||
import Article from '../Article'; | ||
|
||
type InfiniteListProps = { | ||
items: DataFrontmatter[]; | ||
}; | ||
|
||
const MemoArticle = memo(Article); | ||
|
||
function InfiniteList({ items }: InfiniteListProps) { | ||
const searchParams = useSearchParams(); | ||
const total = items.length; | ||
const clampLimit = clamp(1, total); | ||
const limit = clampLimit(parseInt(searchParams.get('limit') || '10', 10)); | ||
|
||
return ( | ||
<> | ||
<List Item={MemoArticle} items={items.slice(0, limit)} /> | ||
{limit < total && ( | ||
<Link href={`?limit=${clampLimit(limit + 10)}`} replace scroll={false}> | ||
更多文章 | ||
</Link> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default InfiniteList; |
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,37 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import InfiniteList from '../InfiniteList'; | ||
|
||
jest.mock('next/navigation', () => ({ | ||
useRouter() { | ||
return { | ||
prefetch: () => null, | ||
}; | ||
}, | ||
useSearchParams() { | ||
return new URLSearchParams(); | ||
}, | ||
usePathname() { | ||
return ''; | ||
}, | ||
})); | ||
|
||
describe('InfiniteList component', () => { | ||
it('should render correct element', () => { | ||
const generateMockPosts = (count: number): DataFrontmatter[] => | ||
new Array(count).fill(null).map((_, i) => ({ | ||
id: `test-id-${i}`, | ||
title: `test-title-${i}`, | ||
date: '2024/1/4', | ||
description: `test-description-${i}`, | ||
categories: [], | ||
tags: [], | ||
})); | ||
render(<InfiniteList items={generateMockPosts(20)} />); | ||
|
||
const link = screen.getByRole('link', { name: '更多文章' }); | ||
const list = screen.getByRole('list'); | ||
|
||
expect(list).toBeInTheDocument(); | ||
expect(link).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
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
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