zarm
Version:
基于 React 的移动端UI库
97 lines (82 loc) • 2.61 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _reactHooks = require("@testing-library/react-hooks");
var _ = _interopRequireDefault(require(".."));
function mockScreenOrientation(value) {
Object.defineProperty(window.screen, 'orientation', {
value: value,
writable: true
});
}
function mockOrientation(value) {
Object.defineProperty(window, 'orientation', {
value: value,
writable: true
});
} // The screen orientation values table
// https://www.w3.org/TR/screen-orientation/#dfn-screen-orientation-values-table
describe('useOrientation', function () {
test('should set angle and type when mounted if window.screen.orientation exists', function () {
mockScreenOrientation({
angle: 180,
type: 'portrait-secondary'
});
var _renderHook = (0, _reactHooks.renderHook)(function () {
return (0, _.default)();
}),
result = _renderHook.result;
expect(result.current).toEqual({
angle: 180,
type: 'portrait-secondary'
});
});
test('should set angle and type when mounted if window.orientation exists', function () {
mockScreenOrientation(undefined);
mockOrientation(90);
var _renderHook2 = (0, _reactHooks.renderHook)(function () {
return (0, _.default)();
}),
result = _renderHook2.result;
expect(result.current).toEqual({
angle: 90,
type: ''
});
});
test('should use the default angle and type if browser does not support orientation API', function () {
mockScreenOrientation(undefined);
mockOrientation(undefined);
var _renderHook3 = (0, _reactHooks.renderHook)(function () {
return (0, _.default)();
}),
result = _renderHook3.result;
expect(result.current).toEqual({
angle: 0,
type: 'portrait-primary'
});
});
test('should use the default angle and type if browser does not support orientation API', function () {
mockScreenOrientation({
angle: 180,
type: 'portrait-secondary'
});
var _renderHook4 = (0, _reactHooks.renderHook)(function () {
return (0, _.default)();
}),
result = _renderHook4.result;
expect(result.current).toEqual({
angle: 180,
type: 'portrait-secondary'
});
(0, _reactHooks.act)(function () {
mockScreenOrientation({
angle: 0,
type: 'landscape-primary'
});
window.dispatchEvent(new Event('orientationchange'));
});
expect(result.current).toEqual({
angle: 0,
type: 'landscape-primary'
});
});
});
;