-
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.
fixes: v0.1.4 - Fixed useAsync, added useInterval, added tests
- Loading branch information
1 parent
5beff29
commit bd71cc1
Showing
8 changed files
with
2,299 additions
and
130 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 |
---|---|---|
|
@@ -8,5 +8,6 @@ node_modules | |
src | ||
test | ||
|
||
jest.config.ts | ||
package-lock.json | ||
tsconfig.json |
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,14 @@ | ||
import type {Config} from '@jest/types'; | ||
|
||
// Sync object | ||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
verbose: true, | ||
clearMocks: true, | ||
testEnvironment: 'jsdom', | ||
// transform: { | ||
// [`^.+\\.tsx?$`]: `ts-jest`, | ||
// }, | ||
} | ||
|
||
export default config |
Large diffs are not rendered by default.
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
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,31 @@ | ||
import { CallFn } from '../async/types'; | ||
import { DependencyList, useEffect } from 'react'; | ||
import { useLatest } from '../generic/useLatest'; | ||
|
||
/** | ||
* Simply set an interval by providing a callback and delay, the interval | ||
* is automatically mounted and unmounted. | ||
* | ||
* (?) When delay changes the interval is updated, to disable it just | ||
* provide null or undefined as delay. | ||
* | ||
* (?) Additionally a list of dependencies can be provided. The dependencies | ||
* will cause the interval to be rescheduled (delay is implicitly a | ||
* dependency) | ||
* | ||
* todo : add enable fn to return a tear down function | ||
*/ | ||
export function useInterval(fn: CallFn, ms?: number | null | undefined, deps?: DependencyList) { | ||
|
||
const lastFn = useLatest(fn) | ||
|
||
useEffect(() => { | ||
|
||
if (ms === undefined || ms === null) return undefined; | ||
|
||
const interval = setInterval(() => lastFn.current(), ms || 0) | ||
return () => clearInterval(interval) | ||
|
||
}, [ms, ...(deps || [])]) | ||
|
||
} |
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,180 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { useAsync } from '../../src'; | ||
|
||
describe('useAsync', () => { | ||
|
||
function factory(resolve: boolean = true, result: string = 'done', ms: number = 0) { | ||
return () => { | ||
return new Promise((res, rej) => { | ||
const wait = setTimeout(() => { | ||
clearTimeout(wait); | ||
(resolve ? res : rej)(result) | ||
}, ms) | ||
}) | ||
} | ||
} | ||
|
||
function hook() { | ||
return renderHook( | ||
({ fn }) => useAsync(fn), | ||
{ initialProps: { fn: factory(true) }} | ||
) | ||
} | ||
|
||
it('should be defined', () => { | ||
expect(useAsync).toBeDefined() | ||
}) | ||
|
||
it('should be initialized', async () => { | ||
const h = hook() | ||
expect(h.result.current).toMatchObject({ | ||
isLoading: false, | ||
isCompleted: false, | ||
isSucceed: false, | ||
isFailed: false, | ||
// result: undefined, | ||
// error: undefined, | ||
}) | ||
expect(h.result.current).not.toHaveProperty('result') | ||
expect(h.result.current).not.toHaveProperty('error') | ||
}) | ||
|
||
describe('run()', () => { | ||
|
||
it('should resolve', async () => { | ||
const h = hook() | ||
|
||
// const res = await h.result.current.runAsync() | ||
act(() => { | ||
h.result.current.run() | ||
}) | ||
await h.waitForNextUpdate() | ||
|
||
expect(h.result.current).toMatchObject({ | ||
isLoading: false, | ||
isCompleted: true, | ||
isSucceed: true, | ||
isFailed: false, | ||
result: 'done', | ||
}) | ||
}) | ||
|
||
it('should fail', async () => { | ||
const h = hook() | ||
await h.rerender({ fn: factory(false) }) | ||
|
||
// let err; | ||
// try { | ||
// await h.result.current.runAsync() | ||
// } catch (e) { | ||
// err = e | ||
// } | ||
|
||
act(() => { | ||
h.result.current.run() | ||
}) | ||
await h.waitForNextUpdate() | ||
|
||
expect(h.result.current).toMatchObject({ | ||
isLoading: false, | ||
isCompleted: true, | ||
isSucceed: false, | ||
isFailed: true, | ||
error: 'done', | ||
}) | ||
}) | ||
|
||
}) | ||
|
||
// todo : how to wait for the promise ? | ||
// describe('runAsync()', () => { | ||
// | ||
// it('should resolve', async () => { | ||
// const h = hook() | ||
// | ||
// let res = 'xxx' | ||
// await act(async () => { | ||
// return h.result.current.runAsync() | ||
// }) | ||
// | ||
// await h.waitForNextUpdate() | ||
// | ||
// expect(h.result.current).toMatchObject({ | ||
// isLoading: false, | ||
// isCompleted: true, | ||
// isSucceed: true, | ||
// isFailed: false, | ||
// result: res, | ||
// }) | ||
// }) | ||
// | ||
// it('should fail', async () => { | ||
// const h = hook() | ||
// await h.rerender({ fn: factory(false) }) | ||
// | ||
// // let err; | ||
// // try { | ||
// // await h.result.current.runAsync() | ||
// // } catch (e) { | ||
// // err = e | ||
// // } | ||
// | ||
// h.result.current.run() | ||
// await h.waitForNextUpdate() | ||
// | ||
// expect(h.result.current).toMatchObject({ | ||
// isLoading: false, | ||
// isCompleted: true, | ||
// isSucceed: false, | ||
// isFailed: true, | ||
// error: 'done', | ||
// }) | ||
// }) | ||
// | ||
// }) | ||
|
||
it('supports unstable function', async () => { | ||
const h = hook() | ||
await h.rerender({ fn: factory(true, 'v1') }) | ||
await h.rerender({ fn: factory(true, 'v2') }) | ||
|
||
act(() => { | ||
h.result.current.run() | ||
}) | ||
await h.waitForNextUpdate() | ||
|
||
expect(h.result.current.result).toEqual('v2') | ||
}) | ||
|
||
it('is loading', async () => { | ||
jest.useFakeTimers() | ||
|
||
const h = hook() | ||
await h.rerender({ fn: factory(true, 'slow', 1000) }) | ||
|
||
act(() => { | ||
h.result.current.run() | ||
}) | ||
|
||
expect(h.result.current).toMatchObject({ | ||
isLoading: true, | ||
isCompleted: false, | ||
isSucceed: false, | ||
isFailed: false, | ||
}) | ||
|
||
act(() => { | ||
jest.runAllTimers() | ||
}) | ||
|
||
await h.waitForNextUpdate() | ||
expect(h.result.current).toMatchObject({ | ||
isLoading: false, | ||
isCompleted: true, | ||
isSucceed: true, | ||
isFailed: false, | ||
result: 'slow', | ||
}) | ||
}) | ||
|
||
}) |
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