"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _react = require("@testing-library/react");
var _react2 = _interopRequireDefault(require("react"));
var _testUtils = require("../../test-utils");
var _testkit = require("@wix/fe-essentials-standalone/testkit");
var _internal = require("@wix/bex-core/testkit/internal");
var _usePickerStandalone = require("./usePickerStandalone");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/hooks/usePickerStandalone/usePickerStandalone.spec.tsx";
describe('usePickerStandalone', () => {
  _testkit.testkit.beforeAndAfter();
  let driver;
  beforeEach(() => {
    driver = new _internal.WixPatternsContainerDriver();
  });
  it('makes `filters` config option optional when there are no filters', () => {
    let searchValue = null;
    const App = () => {
      const {
        picker
      } = (0, _usePickerStandalone.usePickerStandalone)({
        queryName: 'test1',
        fetchData: async () => ({
          items: []
        }),
        fetchErrorMessage: () => null,
        itemKey: item => item.id,
        itemName: item => item.name,
        defaultDestinationUrl: `https://manage.wix.com`
      });
      searchValue = picker.collection.filters.search.value;
      return null;
    };
    (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_testUtils.CairoTestApp, {
      driver: driver,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 42,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react2.default.createElement(App, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 43,
        columnNumber: 9
      }
    })));
    expect(searchValue).toBe('');
  });
  it('makes `itemKey` & `itemName` config options optional when an item has an {id: string, name: string}', () => {
    const App = () => {
      const {
        picker
      } = (0, _usePickerStandalone.usePickerStandalone)({
        queryName: 'test2',
        fetchData: async () => ({
          items: []
        }),
        fetchErrorMessage: () => null,
        defaultDestinationUrl: `https://manage.wix.com`
      });
      const item = {
        id: 'foo',
        name: 'bar'
      };
      return /*#__PURE__*/_react2.default.createElement(_react2.default.Fragment, null, picker.collection.itemKey(item), picker.collection.itemName(item));
    };
    const {
      container
    } = (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_testUtils.CairoTestApp, {
      driver: driver,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 71,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react2.default.createElement(App, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 72,
        columnNumber: 9
      }
    })));
    expect(container.textContent).toBe('foobar');
  });
  it("makes `itemKey` config option required when an item doesn't extend {id: string}", () => {
    const App = () => {
      // @ts-expect-error - T does not extend { id: string }
      const {
        picker
      } = (0, _usePickerStandalone.usePickerStandalone)({
        queryName: 'test3',
        fetchData: async () => ({
          items: []
        }),
        itemName: item => item.name2,
        fetchErrorMessage: () => null,
        defaultDestinationUrl: `https://manage.wix.com`
      });
      try {
        return /*#__PURE__*/_react2.default.createElement(_react2.default.Fragment, null, picker.collection.itemKey({
          id2: 'foo',
          name2: 'bar'
        }));
      } catch (e) {
        return /*#__PURE__*/_react2.default.createElement(_react2.default.Fragment, null, e == null ? void 0 : e.message);
      }
    };
    const {
      container
    } = (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_testUtils.CairoTestApp, {
      driver: driver,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 99,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react2.default.createElement(App, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 100,
        columnNumber: 9
      }
    })));
    expect(container.textContent).toBe('Collection config must specify itemKey, or item must have one of properties: id, _id');
  });
  it("makes `itemName` config option required when an item doesn't extend {id: string}", () => {
    const App = () => {
      // @ts-expect-error - T does not extend { name: string }
      const {
        picker
      } = (0, _usePickerStandalone.usePickerStandalone)({
        queryName: 'test3',
        fetchData: async () => ({
          items: []
        }),
        itemKey: item => item.id2,
        fetchErrorMessage: () => null,
        defaultDestinationUrl: `https://manage.wix.com`,
        noItemsAvailableDestinationUrl: 'https://wix.com'
      });
      try {
        return /*#__PURE__*/_react2.default.createElement(_react2.default.Fragment, null, picker.collection.itemName({
          id2: 'foo',
          name2: 'bar'
        }));
      } catch (e) {
        return /*#__PURE__*/_react2.default.createElement(_react2.default.Fragment, null, e == null ? void 0 : e.message);
      }
    };
    const {
      container
    } = (0, _react.render)(/*#__PURE__*/_react2.default.createElement(_testUtils.CairoTestApp, {
      driver: driver,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 130,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react2.default.createElement(App, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 131,
        columnNumber: 9
      }
    })));
    expect(container.textContent).toBe('Collection config must specify itemName, or item must have property: name');
  });
});
//# sourceMappingURL=usePickerStandalone.spec.js.map