-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taro): 新增 useAutoStopPullDownRefresh
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as Taro from '@tarojs/taro' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { wait } from '../utils' | ||
|
||
describe('useAutoStopPullDownRefresh', () => { | ||
let callback: () => any | ||
const usePullDownRefresh: any = jest.fn().mockImplementation(cb => { | ||
callback = cb | ||
}) | ||
const stopPullDownRefresh: any = jest.fn() | ||
|
||
beforeAll(() => { | ||
jest.mock( | ||
'@tarojs/taro', | ||
() => | ||
({ | ||
usePullDownRefresh, | ||
stopPullDownRefresh, | ||
} as typeof Taro), | ||
) | ||
}) | ||
|
||
test('表现正常', async () => { | ||
const { useAutoStopPullDownRefresh } = await import( | ||
'./useAutoStopPullDownRefresh' | ||
) | ||
const cb = jest.fn() | ||
renderHook(() => useAutoStopPullDownRefresh(cb)) | ||
expect(usePullDownRefresh).toBeCalled().toBeCalledTimes(1) | ||
expect(stopPullDownRefresh).not.toBeCalled() | ||
expect(callback).toBeFunction() | ||
callback() | ||
expect(stopPullDownRefresh).toBeCalled().toBeCalledTimes(1) | ||
expect(cb).toBeCalled().toBeCalledTimes(1) | ||
|
||
const cbAsync = jest.fn().mockImplementation(async () => wait(10)) | ||
renderHook(() => useAutoStopPullDownRefresh(cbAsync)) | ||
callback() | ||
expect(cbAsync).toBeCalled().toBeCalledTimes(1) | ||
expect(stopPullDownRefresh).toBeCalled().toBeCalledTimes(1) | ||
await wait(11) | ||
expect(stopPullDownRefresh).toBeCalled().toBeCalledTimes(2) | ||
}) | ||
}) |
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,18 @@ | ||
import { isPromiseLike } from '../utils' | ||
import { stopPullDownRefresh, usePullDownRefresh } from '@tarojs/taro' | ||
|
||
/** | ||
* 同 `Taro.usePullDownRefresh`,不过在回调函数完成后会自动调用 `Taro.stopPullDownRefresh()`。 | ||
* | ||
* @param callback 回调函数 | ||
*/ | ||
export function useAutoStopPullDownRefresh(callback: () => any) { | ||
usePullDownRefresh(() => { | ||
const res = callback() | ||
if (isPromiseLike(res)) { | ||
res.then(() => stopPullDownRefresh()) | ||
} else { | ||
stopPullDownRefresh() | ||
} | ||
}) | ||
} |