"use strict";

var _react = require("@testing-library/react");
var _LegacyPage = _interopRequireDefault(require("./LegacyPage"));
var moduleApi = _interopRequireWildcard(require("../module/module"));
var _renderWithRouter = 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 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('affiche le formulaire et la liste sur la meme page', () => {
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  expect(_react.screen.getByTestId('legacy-page')).toBeInTheDocument();
  expect(_react.screen.getByTestId('submit')).toBeInTheDocument();
  expect(_react.screen.getByTestId('legacy-registrations-list')).toBeInTheDocument();
  expect(_react.screen.getByTestId('no-registrations')).toBeInTheDocument();
});
test('desactive le bouton tant que les champs requis ne sont pas valides', () => {
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  expect(_react.screen.getByTestId('submit')).toBeDisabled();
  fillValidForm();
  expect(_react.screen.getByTestId('submit')).toBeEnabled();
});
test('affiche les erreurs de champs et un toast global lors d une soumission invalide', () => {
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.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'
    }
  });
  _react.fireEvent.submit(_react.screen.getByTestId('submit').closest('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, affiche un toast de succes, vide les champs et met a jour la liste', () => {
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  fillValidForm();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('success-toast')).toHaveTextContent('Formulaire valide et enregistre.');
  expect(_react.screen.getByTestId('nom')).toHaveValue('');
  expect(_react.screen.getByTestId('prenom')).toHaveValue('');
  expect(_react.screen.getByTestId('email')).toHaveValue('');
  expect(_react.screen.getByTestId('dateDeNaissance')).toHaveValue('');
  expect(_react.screen.getByTestId('ville')).toHaveValue('');
  expect(_react.screen.getByTestId('codePostal')).toHaveValue('');
  expect(_react.screen.getByTestId('registration-item')).toHaveTextContent('Jean Dupont');
  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('accepte les noms avec accents et tirets dans le mode legacy', () => {
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'François'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean-Pierre'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('email'), {
    target: {
      value: 'jean.pierre@email.com'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('dateDeNaissance'), {
    target: {
      value: '1990-01-01'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('ville'), {
    target: {
      value: "L'Haÿ-les-Roses"
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('codePostal'), {
    target: {
      value: '94240'
    }
  });
  expect(_react.screen.getByTestId('submit')).toBeEnabled();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('success-toast')).toBeInTheDocument();
  expect(_react.screen.getByTestId('registration-item')).toHaveTextContent('Jean-Pierre François');
});
test('masque le toast de succes apres le delai', () => {
  jest.useFakeTimers();
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  fillValidForm();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('success-toast')).toBeInTheDocument();
  (0, _react.act)(() => {
    jest.advanceTimersByTime(3000);
  });
  expect(_react.screen.queryByTestId('success-toast')).not.toBeInTheDocument();
});
test('affiche un toast d erreur quand handleSubmit leve une erreur', () => {
  const submitSpy = jest.spyOn(moduleApi, 'handleSubmit').mockImplementation(() => {
    throw new Error('Erreur de sauvegarde');
  });
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.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, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  fillValidForm();
  _react.fireEvent.click(_react.screen.getByTestId('submit'));
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('Une erreur est survenue');
  submitSpy.mockRestore();
});
test('affiche un toast d erreur generique quand le chargement des inscriptions echoue', () => {
  const registrationsSpy = jest.spyOn(moduleApi, 'getRegistrations').mockImplementation(() => {
    throw {};
  });
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('Une erreur est survenue');
  registrationsSpy.mockRestore();
});
test('affiche les erreurs nom et prenom en rouge pour des valeurs invalides', () => {
  (0, _renderWithRouter.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_LegacyPage.default, {}));
  _react.fireEvent.change(_react.screen.getByTestId('nom'), {
    target: {
      value: 'Dupont1'
    }
  });
  _react.fireEvent.change(_react.screen.getByTestId('prenom'), {
    target: {
      value: 'Jean2'
    }
  });
  const nomError = _react.screen.getByTestId('nom-error');
  const prenomError = _react.screen.getByTestId('prenom-error');
  expect(nomError).toBeInTheDocument();
  expect(prenomError).toBeInTheDocument();
  expect(nomError).toHaveClass('field-error');
  expect(prenomError).toHaveClass('field-error');
});