"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _react2 = require("@testing-library/react");

var _utils = require("ts-jest/utils");

var _index = _interopRequireDefault(require("../index"));

var _utils2 = require("../../../tests/utils");

var _handleFileInfo = _interopRequireDefault(require("../utils/handleFileInfo"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

jest.mock('../utils/handleFileInfo');
var mHandleFileInfo = (0, _utils.mocked)(_handleFileInfo.default);
describe('file picker', function () {
  it('render correctly', function () {
    var props = {
      onChange: jest.fn(),
      children: /*#__PURE__*/_react.default.createElement("button", null, "add")
    };
    var wrapper = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, props));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
});
describe('file picker event', function () {
  var props = {
    accept: 'image/jpg, image/jpeg, image/gif, image/png',
    onChange: jest.fn(),
    quality: 0.3,
    children: /*#__PURE__*/_react.default.createElement("button", null, "add")
  };
  var mCreateObjectURL = jest.fn();
  var file = new File([''], './test.jpg', {
    type: 'image/jpeg'
  });
  var mFileDetail = {
    file: file,
    fileType: file.type,
    fileName: file.name,
    fileSize: file.size,
    thumbnail: 'data:,'
  };
  beforeAll(function () {
    (0, _utils2.mockCreateObjectURL)(mCreateObjectURL);
  });
  afterAll(function () {
    (0, _utils2.mockResetCreateObjectURL)();
  });
  afterEach(function () {
    jest.clearAllMocks();
  });
  it('select image & onBeforeSelect is not func', function () {
    var _props = _objectSpread(_objectSpread({}, props), {}, {
      multiple: true,
      onBeforeSelect: false
    });

    mHandleFileInfo.mockImplementation(function (_, callback) {
      callback(mFileDetail);
    });

    var _render = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, _props)),
        container = _render.container;

    var mClickEvent = {
      preventDefault: jest.fn()
    };
    var fileInput = container.querySelectorAll('.za-file-picker__input')[0];

    _react2.fireEvent.change(fileInput, mClickEvent);

    expect(mClickEvent.preventDefault).not.toBeCalled();

    _react2.fireEvent.change(fileInput, {
      target: {
        files: [file]
      }
    });

    expect(props.onChange).toBeCalledWith([mFileDetail]);
    expect(props.onChange).toBeCalledTimes(1);
  });
  it('should handle change event if files is an array of File instance', function () {
    var getFileInfo;
    mHandleFileInfo.mockImplementation(function (_, callback) {
      getFileInfo = callback;
      callback(mFileDetail);
    });

    var _render2 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, props)),
        container = _render2.container;

    var fileInput = container.querySelectorAll('.za-file-picker__input')[0];

    _react2.fireEvent.change(fileInput, {
      target: {
        files: [file]
      }
    });

    expect(mHandleFileInfo).toBeCalledWith({
      file: file,
      quality: 0.3
    }, getFileInfo);
    expect(props.onChange).toBeCalledWith(mFileDetail);
  });
  it('click children', function () {
    var _render3 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, props)),
        container = _render3.container;

    var fileInput = container.querySelectorAll('.za-file-picker__input')[0];

    _react2.fireEvent.change(fileInput, {
      target: {
        files: [file]
      }
    });

    expect(props.onChange).toBeCalled();
  });
});
describe('file picker disabled', function () {
  var props = {
    accept: 'image/jpg, image/jpeg, image/gif, image/png',
    onChange: jest.fn(),
    onBeforeSelect: jest.fn(),
    children: /*#__PURE__*/_react.default.createElement("button", null, "foo")
  };
  it('on before select return false', function () {
    var _props = _objectSpread(_objectSpread({}, props), {}, {
      onBeforeSelect: jest.fn(function () {
        return false;
      })
    });

    var _render4 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, _props)),
        container = _render4.container;

    var fileInput = container.querySelectorAll('.za-file-picker__input')[0];

    _react2.fireEvent.change(fileInput);

    expect(props.onChange).not.toHaveBeenCalled();
  });
  it('disabled', function () {
    var _props = _objectSpread(_objectSpread({}, props), {}, {
      disabled: true
    });

    var _render5 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, _props)),
        container = _render5.container;

    var fileInput = container.querySelectorAll('.za-file-picker__input')[0];

    _react2.fireEvent.change(fileInput);

    expect(props.onChange).not.toHaveBeenCalled();
  });
});