-
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
4 changed files
with
94 additions
and
1 deletion.
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,59 @@ | ||
import * as Taro from '@tarojs/taro' | ||
import { AnyFunction } from '../types' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
|
||
describe('useWindowSize.taro', () => { | ||
const getSystemInfoSync: any = jest.fn().mockImplementation(() => ({ | ||
model: 'iPhone 5', | ||
pixelRatio: 2, | ||
windowWidth: 320, | ||
windowHeight: 520, | ||
system: 'iOS 10.0.1', | ||
language: 'zh_CN', | ||
version: '7.0.4', | ||
screenWidth: 320, | ||
screenHeight: 568, | ||
SDKVersion: '2.8.0', | ||
brand: 'devtools', | ||
fontSizeSetting: 16, | ||
batteryLevel: 100, | ||
statusBarHeight: 20, | ||
safeArea: { | ||
right: 320, | ||
bottom: 568, | ||
left: 0, | ||
top: 20, | ||
width: 320, | ||
height: 548, | ||
}, | ||
deviceOrientation: 'portrait', | ||
platform: 'devtools', | ||
})) | ||
const onWindowResize: AnyFunction = jest.fn() | ||
const offWindowResize: AnyFunction = jest.fn() | ||
|
||
beforeAll(() => { | ||
jest.mock( | ||
'@tarojs/taro', | ||
() => | ||
({ | ||
getSystemInfoSync: getSystemInfoSync, | ||
onWindowResize: onWindowResize, | ||
offWindowResize: offWindowResize, | ||
} as typeof Taro), | ||
) | ||
}) | ||
|
||
test('表现正常', async () => { | ||
const { useWindowSize } = await import('./useWindowSize.taro') | ||
const { result } = renderHook(() => useWindowSize()) | ||
|
||
expect(getSystemInfoSync).toBeCalled().toBeCalledTimes(1) | ||
expect(onWindowResize).toBeCalled().toBeCalledTimes(1) | ||
|
||
expect(result.current).toEqual({ | ||
width: 320, | ||
height: 520, | ||
}) | ||
}) | ||
}) |
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 { useWindowSize as _useWindowSize } from './useWindowSize' | ||
import { | ||
getSystemInfoSync, | ||
offWindowResize, | ||
onWindowResize, | ||
} from '@tarojs/taro' | ||
import { useEffect, useState } from 'react' | ||
|
||
const getWindowSize = function (): ReturnType<typeof _useWindowSize> { | ||
const { windowWidth: width, windowHeight: height } = getSystemInfoSync() | ||
return { width, height } | ||
} | ||
|
||
export const useWindowSize: typeof _useWindowSize = function ( | ||
// 不支持 | ||
_initialWidth, | ||
// 不支持 | ||
_initialHeight, | ||
) { | ||
const [size, setSize] = useState(getWindowSize) | ||
useEffect(() => { | ||
const getWindowSizeCallback = () => { | ||
setSize(getWindowSize()) | ||
} | ||
onWindowResize?.(getWindowSizeCallback) | ||
return () => { | ||
offWindowResize?.(getWindowSizeCallback) | ||
} | ||
}, []) | ||
return size | ||
} |
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,2 @@ | ||
/* istanbul ignore file */ | ||
export { useWindowSize } from 'react-use' |