-
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.
- Loading branch information
Marc Pomar
committed
Jun 26, 2024
1 parent
4c49632
commit 5877b7a
Showing
8 changed files
with
629 additions
and
73 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 |
---|---|---|
|
@@ -3,4 +3,6 @@ src | |
babel.config.js | ||
tsconfig.json | ||
tsconfig.tsbuildinfo | ||
yarn.lock | ||
yarn.lock | ||
.test.js | ||
jest.config.js |
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,13 @@ | ||
module.exports = { | ||
moduleFileExtensions: ["ts", "tsx", "js", "json"], | ||
moduleNameMapper: { | ||
"\\.scss$": "identity-obj-proxy", | ||
"^@components/(.*)$": "<rootDir>/src/components/$1", | ||
}, | ||
collectCoverageFrom: ["<rootDir>/**/*.{ts, tsx}"], | ||
roots: ["<rootDir>"], | ||
testRegex: "(/tests/jest/.*|(\\.|/)(test|spec))\\.(ts|tsx)$", | ||
transform: { | ||
"^.+\\.(ts|tsx)$": "ts-jest", | ||
}, | ||
}; |
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 was deleted.
Oops, something went wrong.
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,92 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { act, renderHook, waitFor } from "@testing-library/react"; | ||
import { generateGetKey, useSWRPaginated } from "./useSWRPaginated"; | ||
import axios from "axios"; | ||
import { SWRConfig } from "swr"; | ||
import React from "react"; | ||
import { unstable_serialize } from "swr/infinite"; | ||
|
||
const testapi = axios.create({ baseURL: "https://api-content.faable.link" }); | ||
const fetcher = async (key) => { | ||
const res = await testapi.get(key); | ||
return res.data; | ||
}; | ||
|
||
const setupWrapper = | ||
(fallback?: any) => | ||
({ children }) => | ||
( | ||
<SWRConfig | ||
value={{ | ||
fetcher, | ||
fallback, | ||
}} | ||
> | ||
{children} | ||
</SWRConfig> | ||
); | ||
|
||
// test("without fallback data", async () => { | ||
// const wrapper = setupWrapper(); | ||
// const { result, waitForNextUpdate } = renderHook( | ||
// () => useSWRPaginated(generateGetKey("/content")), | ||
// { wrapper } | ||
// ); | ||
|
||
// expect(result.current.items).toEqual(undefined); | ||
// await waitForNextUpdate({ timeout: 5000 }); | ||
// expect(result.current.items.length).toEqual(29); | ||
// }); | ||
|
||
test("with fallback data", async () => { | ||
const key = "/content"; | ||
const data = await fetcher(key); | ||
const fallback = { | ||
[unstable_serialize(generateGetKey(key))]: [data], | ||
}; | ||
const wrapper = setupWrapper(fallback); | ||
const { result } = renderHook(() => useSWRPaginated(generateGetKey(key)), { | ||
wrapper, | ||
hydrate: true, | ||
}); | ||
// await act(async () => {}); | ||
// await waitFor(() => { | ||
// const swr = result.current; | ||
// expect(swr.data?.length).toEqual(1); | ||
// expect(swr.items?.length).toEqual(30); | ||
// }); | ||
expect(result.current.data?.length).toEqual(1); | ||
expect(result.current.items?.length).toEqual(30); | ||
// console.log(result.current.data); | ||
// console.log(result.current.items); | ||
}); | ||
|
||
// test("should use counter", () => { | ||
// let a = "hola"; | ||
// const { result } = renderHook(() => | ||
// useSWRPaginated(a ? generateGetKey("/demo?q=124") : null) | ||
// ); | ||
|
||
// expect(result.current.items).toStrictEqual([]); | ||
|
||
// expect(typeof result.current.setSize).toBe("function"); | ||
// }); | ||
|
||
// test("should use counter", () => { | ||
// let a = "hola"; | ||
// const { result } = renderHook(() => | ||
// useSWRPaginated<{ name: string }>( | ||
// a ? generateGetKey("/demo?q=124") : null, | ||
// { | ||
// refreshInterval: 3000, | ||
// } | ||
// ) | ||
// ); | ||
|
||
// expect(result.current.items).toStrictEqual([]); | ||
|
||
// expect(typeof result.current.setSize).toBe("function"); | ||
// }); |
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
Oops, something went wrong.