react-native-ui-lib
Version:
<p align="center"> <img src="https://user-images.githubusercontent.com/1780255/105469025-56759000-5ca0-11eb-993d-3568c1fd54f4.png" height="250px" style="display:block"/> </p> <p align="center">UI Toolset & Components Library for React Native</p> <p a
71 lines (55 loc) • 1.77 kB
JavaScript
import {renderHook, act} from '@testing-library/react-hooks';
let Constants;
let useOrientation;
let orientationChangeListeners;
describe('useOrientation hook', () => {
beforeEach(() => {
jest.mock('../../../commons/Constants');
Constants = require('../../../commons/Constants').default;
useOrientation = require('../index').default;
orientationChangeListeners = [];
Constants.addDimensionsEventListener = jest.fn(callback => {
orientationChangeListeners.push(callback);
});
});
it('should return current orientation', () => {
act(() => {
fakeOrientationChange('landscape');
});
const {result} = renderHook(() => {
const {orientation} = useOrientation();
return {orientation};
});
expect(result.current.orientation).toBe('landscape');
});
it('should update orientation when it change', () => {
act(() => {
fakeOrientationChange('landscape');
});
const {result} = renderHook(() => {
const {orientation} = useOrientation();
return {orientation};
});
act(() => {
fakeOrientationChange('portrait');
});
expect(result.current.orientation).toBe('portrait');
});
it('should invoke given change callback when orientation changes', () => {
const changeCallback = jest.fn();
renderHook(() => {
const {orientation} = useOrientation({onOrientationChange: changeCallback});
return {orientation};
});
act(() => {
fakeOrientationChange('landscape');
});
expect(changeCallback).toHaveBeenCalledTimes(1);
});
});
function fakeOrientationChange(orientation = 'portrait') {
Constants.orientation = orientation;
orientationChangeListeners.forEach(callback => {
callback(orientation);
});
}