-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pass pageSWRs to swrDataToOffset * fix format and type checker * tiny performance improvement * fix tsconfig
- Loading branch information
1 parent
d9b9d99
commit 80c4b3f
Showing
3 changed files
with
90 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { useEffect } from 'react' | ||
import { cleanup, render, waitForDomChange } from '@testing-library/react' | ||
|
||
import useSWR, { useSWRPages } from '../src' | ||
|
||
describe('useSWRPages', () => { | ||
afterEach(cleanup) | ||
|
||
it('should render the first page componet', async () => { | ||
function Page() { | ||
const { pages } = useSWRPages<number, string, any>( | ||
'page-1', | ||
({ offset }) => { | ||
return 'page ' + (offset || 0) | ||
}, | ||
() => 0 | ||
) | ||
|
||
return pages | ||
} | ||
const { container } = render(<Page />) | ||
expect(container.textContent).toMatchInlineSnapshot(`"page 0"`) | ||
}) | ||
|
||
it('should render the multiple pages', async () => { | ||
function Page() { | ||
const { pages, pageCount, isLoadingMore, loadMore } = useSWRPages< | ||
number, | ||
string, | ||
any | ||
>( | ||
'page-2', | ||
({ offset, withSWR }) => { | ||
const { data } = withSWR(useSWR(String(offset || 0), v => v)) | ||
return 'page ' + data + ', ' | ||
}, | ||
(_, pageSWRs) => pageSWRs.length | ||
) | ||
|
||
useEffect(() => { | ||
// load next page if the current one is ready | ||
if (pageCount <= 2 && !isLoadingMore) loadMore() | ||
}, [pageCount, isLoadingMore]) | ||
|
||
return pages | ||
} | ||
const { container } = render(<Page />) | ||
await waitForDomChange({ container }) // mount | ||
// should have 3 pages | ||
expect(container.textContent).toMatchInlineSnapshot( | ||
`"page 0, page 1, page 2, "` | ||
) | ||
}) | ||
}) |