Skip to content

Commit a76b712

Browse files
authored
Feat/use orientation hook (#1625)
* Create useOrientation hook * Update generated types
1 parent 46fac24 commit a76b712

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed

generatedTypes/src/hooks/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as useToggleValue } from './useToggleValue';
22
export { default as useDidUpdate } from './useDidUpdate';
3+
export { default as useOrientation } from './useOrientation';
34
export { default as useScrollEnabler } from './useScrollEnabler';
45
export { default as useScrollReached } from './useScrollReached';
56
export { default as useScrollToItem } from './useScrollToItem';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface UseOrientationProps {
2+
onOrientationChange?: Function;
3+
}
4+
declare const useOrientation: ({ onOrientationChange }?: UseOrientationProps) => {
5+
orientation: import("../../helpers/Constants").orientations;
6+
};
7+
export default useOrientation;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"@react-native-community/datetimepicker": "^3.4.6",
7070
"@react-native-community/netinfo": "^5.6.2",
7171
"@react-native-picker/picker": "^1.9.4",
72-
"@testing-library/react-hooks": "^3.4.2",
72+
"@testing-library/react-hooks": "^7.0.2",
7373
"@testing-library/react-native": "^7.2.0",
7474
"@types/hoist-non-react-statics": "^3.3.1",
7575
"@types/lodash": "^4.0.0",

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {default as useToggleValue} from './useToggleValue';
22
export {default as useDidUpdate} from './useDidUpdate';
3+
export {default as useOrientation} from './useOrientation';
34
export {default as useScrollEnabler} from './useScrollEnabler';
45
export {default as useScrollReached} from './useScrollReached';
56
export {default as useScrollToItem} from './useScrollToItem';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {renderHook, act} from '@testing-library/react-hooks';
2+
3+
let Constants;
4+
let useOrientation;
5+
let orientationChangeListeners;
6+
7+
describe('useOrientation hook', () => {
8+
beforeEach(() => {
9+
jest.mock('../../../helpers/Constants');
10+
Constants = require('../../../helpers/Constants').default;
11+
useOrientation = require('../index').default;
12+
13+
orientationChangeListeners = [];
14+
15+
Constants.addDimensionsEventListener = jest.fn(callback => {
16+
orientationChangeListeners.push(callback);
17+
});
18+
});
19+
20+
it('should return current orientation', () => {
21+
act(() => {
22+
fakeOrientationChange('landscape');
23+
});
24+
const {result} = renderHook(() => {
25+
const {orientation} = useOrientation();
26+
27+
return {orientation};
28+
});
29+
30+
expect(result.current.orientation).toBe('landscape');
31+
});
32+
33+
it('should update orientation when it change', () => {
34+
act(() => {
35+
fakeOrientationChange('landscape');
36+
});
37+
const {result} = renderHook(() => {
38+
const {orientation} = useOrientation();
39+
40+
return {orientation};
41+
});
42+
43+
act(() => {
44+
fakeOrientationChange('portrait');
45+
});
46+
47+
expect(result.current.orientation).toBe('portrait');
48+
});
49+
50+
it('should invoke given change callback when orientation changes', () => {
51+
const changeCallback = jest.fn();
52+
renderHook(() => {
53+
const {orientation} = useOrientation({onOrientationChange: changeCallback});
54+
return {orientation};
55+
});
56+
57+
act(() => {
58+
fakeOrientationChange('landscape');
59+
});
60+
61+
expect(changeCallback).toHaveBeenCalledTimes(1);
62+
});
63+
});
64+
65+
function fakeOrientationChange(orientation = 'portrait') {
66+
Constants.orientation = orientation;
67+
orientationChangeListeners.forEach(callback => {
68+
callback(orientation);
69+
});
70+
}

src/hooks/useOrientation/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {useEffect, useCallback, useState} from 'react';
2+
import {Constants} from 'helpers';
3+
import useDidUpdate from '../useDidUpdate';
4+
5+
interface UseOrientationProps {
6+
onOrientationChange?: Function;
7+
}
8+
9+
const useOrientation = ({onOrientationChange}: UseOrientationProps = {}) => {
10+
const [orientation, setOrientation] = useState(Constants.orientation);
11+
12+
const orientationChangeListener = useCallback(() => {
13+
setOrientation(Constants.orientation);
14+
}, []);
15+
16+
useEffect(() => {
17+
Constants.addDimensionsEventListener(orientationChangeListener);
18+
return () => Constants.removeDimensionsEventListener(orientationChangeListener);
19+
}, []);
20+
21+
useDidUpdate(() => {
22+
onOrientationChange?.(orientation);
23+
// eslint-disable-next-line react-hooks/exhaustive-deps
24+
}, [orientation]);
25+
26+
return {
27+
orientation
28+
};
29+
};
30+
31+
export default useOrientation;

0 commit comments

Comments
 (0)