-
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.
- Loading branch information
Showing
2 changed files
with
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as Taro from '@tarojs/taro' | ||
import { AnyFunction } from '../types' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
|
||
describe('useReachBottom.taro', () => { | ||
let useReachBottomCb: AnyFunction | ||
const useReachBottom: AnyFunction = jest | ||
.fn() | ||
.mockImplementation((cb: AnyFunction) => { | ||
useReachBottomCb = cb | ||
}) | ||
|
||
beforeAll(() => { | ||
jest.mock( | ||
'@tarojs/taro', | ||
() => | ||
({ | ||
useReachBottom: useReachBottom, | ||
} as typeof Taro), | ||
) | ||
}) | ||
|
||
test('表现正常', async () => { | ||
const { useReachBottom } = await import('./useReachBottom.taro') | ||
const cb = jest.fn() | ||
renderHook(() => useReachBottom(cb)) | ||
// 初始立即触发一次 | ||
expect(cb).toBeCalled().toBeCalledTimes(1) | ||
expect(useReachBottomCb).toBeFunction().toBe(cb) | ||
}) | ||
}) |
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,19 @@ | ||
import { useReachBottom as _useReachBottom } from './useReachBottom' | ||
import { useEffect, useRef } from 'react' | ||
import { useReachBottom as useTaroReachBottom } from '@tarojs/taro' | ||
|
||
export const useReachBottom: typeof _useReachBottom = ( | ||
callback, | ||
// 小程序下该项在页面配置中的 onReachBottomDistance 设置 | ||
_offset, | ||
) => { | ||
const ref = useRef<any>() | ||
|
||
// 立即触发一次回调 | ||
useEffect(callback, []) | ||
|
||
useTaroReachBottom(callback) | ||
|
||
// 小程序下始终相对于窗口,ref 无实际作用,仅起兼容作用 | ||
return ref | ||
} |