import { act, renderHook } from '@testing-library/react-hooks';
import useOrientation from '..';

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 = renderHook(function () {
      return useOrientation();
    }),
        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 = renderHook(function () {
      return useOrientation();
    }),
        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 = renderHook(function () {
      return useOrientation();
    }),
        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 = renderHook(function () {
      return useOrientation();
    }),
        result = _renderHook4.result;

    expect(result.current).toEqual({
      angle: 180,
      type: 'portrait-secondary'
    });
    act(function () {
      mockScreenOrientation({
        angle: 0,
        type: 'landscape-primary'
      });
      window.dispatchEvent(new Event('orientationchange'));
    });
    expect(result.current).toEqual({
      angle: 0,
      type: 'landscape-primary'
    });
  });
});