import { _ as _asyncToGenerator, r as regenerator } from '../../index-qSfOyGhk.js';
import { _ as _extends } from '../../extends-C4cduGrv.js';
import React__default from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import TextField from '../../text-field/index.js';
import Button from '../../button/index.js';
import useForm, { Form } from '../index.js';
import { n as node, r as rest } from '../../index-DVWixydF.js';
import '../../typeof-CgC7Bq6K.js';
import '../../label/index.js';
import '../../objectWithoutProperties-TXwlfNxG.js';
import 'prop-types';
import '@reach/visually-hidden';
import 'classnames';
import '../../input/index.js';
import '../../defineProperty-CEd-kZQW.js';
import '../../stack/index.js';
import '../../inline-validation/index.js';
import '../../icon/index.js';
import '@fortawesome/react-fontawesome';
import '../../icon/icons.js';
import '../../icon/library.js';
import '@fortawesome/fontawesome-svg-core';
import '@fortawesome/pro-solid-svg-icons';
import '@fortawesome/pro-light-svg-icons';
import '@fortawesome/free-brands-svg-icons';
import '../../slicedToArray-BB-AwwkK.js';
import '../../auto-id.esm-Cly76abB.js';
import '../../utils.esm-CKyxlHOy.js';
import '../../warning-s9zGJbnw.js';
import '../../_commonjsHelpers-_1FafXWI.js';
import '../../use-progress/index.js';
import '../../use-trailing-debounce/index.js';
import '../../ck-icon/index.js';
import '../../ck-icon/assets/icon-map/index.js';
import '../../floating-ui.react-cWPyyUrN.js';
import '../../toConsumableArray-dKQYsfpV.js';
import 'react-dom';
import '../../button/button-group.js';
import '../../createClass-DT8Y0mhp.js';
import '../../setPrototypeOf-Bchah3-D.js';
import 'http';
import 'https';
import 'util';
import 'tty';
import 'os';
import 'timers';
import 'stream';
import 'url';
import 'zlib';

var server = node.setupServer();
beforeAll(function () {
  return server.listen();
}); // reset any request handlers that are declared as a part of our tests
// (i.e. for testing one-time error scenarios)

afterEach(function () {
  return server.resetHandlers();
}); // clean up once the tests are done

afterAll(function () {
  return server.close();
});

var ExampleForm = function ExampleForm(props) {
  var form = useForm({
    initialData: {
      name: "",
      email: ""
    }
  });
  return /*#__PURE__*/React__default.createElement(Form, _extends({
    form: form
  }, props), /*#__PURE__*/React__default.createElement(TextField, {
    label: "Name",
    name: "name"
  }), /*#__PURE__*/React__default.createElement(TextField, {
    label: "Email",
    name: "email"
  }), /*#__PURE__*/React__default.createElement(Button, null, "Submit"));
};

jest.useFakeTimers();
describe("Form validations", function () {
  test("should validate async", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
    var error;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            server.use(rest.post("/validations", function (req, res, ctx) {
              return res(ctx.json([{
                field: "name",
                message: "name is invalid"
              }]));
            }));
            render( /*#__PURE__*/React__default.createElement(ExampleForm, {
              validation: "demo_validation"
            }));
            userEvent.type(screen.getByLabelText("Name"), "Brendan");
            userEvent.type(screen.getByLabelText("Email"), "brendan@convertkit.com");
            jest.advanceTimersByTime(8000);
            _context.next = 7;
            return screen.findByText("name is invalid");

          case 7:
            error = _context.sent;
            expect(error).toBeInTheDocument();

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
});
