"use strict";

var _react = require("@testing-library/react");
var _reactRouterDom = require("react-router-dom");
var _RegistrationPage = _interopRequireDefault(require("./RegistrationPage"));
var _ListPage = _interopRequireDefault(require("./ListPage"));
var moduleApi = _interopRequireWildcard(require("../module/module"));
var _renderWithRouter2 = require("../test/renderWithRouter");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const renderRegistrationWithRoutes = function () {
  let route = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '/register';
  return (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactRouterDom.Routes, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
      path: "/register",
      element: /*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {})
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactRouterDom.Route, {
      path: "/list",
      element: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {})
    })]
  }), {
    route
  });
};
const fillValidForm = () => {
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'Dupont'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('email'), {
    target: {
      value: 'jean.dupont@email.com'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('dateDeNaissance'), {
    target: {
      value: '1990-01-01'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('ville'), {
    target: {
      value: 'Paris'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('codePostal'), {
    target: {
      value: '75001'
    }
  });
};
beforeEach(() => {
  localStorage.clear();
});
afterEach(() => {
  jest.useRealTimers();
});
test('desactive le bouton tant que les champs requis ne sont pas valides', () => {
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  expect(_react.screen.getByTestId('submit')).toBeDisabled();
  fillValidForm();
  expect(_react.screen.getByTestId('submit')).toBeEnabled();
  _react.fireEvent.change(_react.screen.getByTestId('email'), {
    target: {
      value: ''
    }
  });
  expect(_react.screen.getByTestId('submit')).toBeDisabled();
});
test('affiche les erreurs de champs et un toast global lors d une soumission invalide', () => {
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'Dupont'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('email'), {
    target: {
      value: 'email-invalide'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('dateDeNaissance'), {
    target: {
      value: '2009-01-01'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('ville'), {
    target: {
      value: 'Paris9'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('codePostal'), {
    target: {
      value: '7500'
    }
  });
  const form = _react.screen.getByTestId('submit').closest('form');
  _react.fireEvent.submit(form);
  expect(_react.screen.getByTestId('email-error')).toBeInTheDocument();
  expect(_react.screen.getByTestId('dateOfBirth-error')).toBeInTheDocument();
  expect(_react.screen.getByTestId('ville-error')).toBeInTheDocument();
  expect(_react.screen.getByTestId('codePostal-error')).toBeInTheDocument();
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('Veuillez corriger les erreurs du formulaire.');
  expect(localStorage.getItem('registrations')).toBeNull();
});
test('soumet un formulaire valide, redirige vers la liste et met en evidence la nouvelle ligne', () => {
  renderRegistrationWithRoutes();
  fillValidForm();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('list-page')).toBeInTheDocument();
  expect(_react.screen.getByTestId('registration-item')).toHaveTextContent('Jean Dupont');
  expect(_react.screen.getByTestId('registration-item')).toHaveClass('registration-item-highlight');
  expect(JSON.parse(localStorage.getItem('registrations'))).toEqual([{
    nom: 'Dupont',
    prenom: 'Jean',
    email: 'jean.dupont@email.com',
    dateOfBirth: '1990-01-01',
    ville: 'Paris',
    codePostal: '75001'
  }]);
});
test('gere un changement de champ inconnu sans planter', () => {
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      name: 'unknownField',
      value: 'foo'
    }
  });
  expect(_react.screen.getByTestId('submit')).toBeDisabled();
});
test('affiche un toast d erreur quand handleSubmit leve une erreur avec message', () => {
  const submitSpy = jest.spyOn(moduleApi, 'handleSubmit').mockImplementation(() => {
    throw new Error('Erreur de sauvegarde');
  });
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  fillValidForm();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('Erreur de sauvegarde');
  submitSpy.mockRestore();
});
test('affiche un toast d erreur generique quand handleSubmit leve sans message', () => {
  const submitSpy = jest.spyOn(moduleApi, 'handleSubmit').mockImplementation(() => {
    throw {};
  });
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  fillValidForm();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('Une erreur est survenue');
  submitSpy.mockRestore();
});
test('masque le toast d erreur apres le delai', () => {
  jest.useFakeTimers();
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'Dupont'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('email'), {
    target: {
      value: 'email-invalide'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('dateDeNaissance'), {
    target: {
      value: '1990-01-01'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('ville'), {
    target: {
      value: 'Paris'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('codePostal'), {
    target: {
      value: '75001'
    }
  });
  _react.fireEvent.submit(_react.screen.getByTestId('submit').closest('form'));
  expect(_react.screen.getByTestId('error-toast')).toBeInTheDocument();
  (0, _react.act)(() => {
    jest.advanceTimersByTime(3000);
  });
  expect(_react.screen.queryByTestId('error-toast')).not.toBeInTheDocument();
});
test('annule le delai du toast au demontage du composant', () => {
  jest.useFakeTimers();
  const _renderWithRouter = (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {})),
    unmount = _renderWithRouter.unmount;
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'Dupont'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('email'), {
    target: {
      value: 'email-invalide'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('dateDeNaissance'), {
    target: {
      value: '1990-01-01'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('ville'), {
    target: {
      value: 'Paris'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('codePostal'), {
    target: {
      value: '75001'
    }
  });
  _react.fireEvent.submit(_react.screen.getByTestId('submit').closest('form'));
  expect(_react.screen.getByTestId('error-toast')).toBeInTheDocument();
  unmount();
  expect(() => {
    (0, _react.act)(() => {
      jest.advanceTimersByTime(3000);
    });
  }).not.toThrow();
});
test('affiche les erreurs nom et prenom pour des valeurs invalides', () => {
  (0, _renderWithRouter2.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_RegistrationPage.default, {}));
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'Dupont1'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean2'
    }
  });
  expect(_react.screen.getByTestId('nom-error')).toBeInTheDocument();
  expect(_react.screen.getByTestId('prenom-error')).toBeInTheDocument();
});