"use strict";

var _react = require("@testing-library/react");
var _ListPage = _interopRequireDefault(require("./ListPage"));
var moduleApi = _interopRequireWildcard(require("../module/module"));
var _renderWithRouter3 = 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 }; }
beforeEach(() => {
  localStorage.clear();
});
afterEach(() => {
  jest.useRealTimers();
});
test('met en evidence uniquement la ligne a l index highlightIndex', () => {
  localStorage.setItem('registrations', JSON.stringify([{
    nom: 'Martin',
    prenom: 'Alice',
    email: 'alice.martin@email.com',
    dateOfBirth: '1991-04-20',
    ville: 'Lyon',
    codePostal: '69001'
  }, {
    nom: 'Martin',
    prenom: 'Alice',
    email: 'alice.martin@email.com',
    dateOfBirth: '1992-05-21',
    ville: 'Lyon',
    codePostal: '69002'
  }]));
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}), {
    route: '/list',
    state: {
      highlightIndex: 1
    }
  });
  const items = _react.screen.getAllByTestId('registration-item');
  expect(items[0]).not.toHaveClass('registration-item-highlight');
  expect(items[1]).toHaveClass('registration-item-highlight');
});
test('fait defiler vers la ligne mise en evidence quand scrollIntoView est disponible', () => {
  const scrollIntoViewMock = jest.fn();
  window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;
  localStorage.setItem('registrations', JSON.stringify([{
    nom: 'Martin',
    prenom: 'Alice',
    email: 'alice.martin@email.com',
    dateOfBirth: '1991-04-20',
    ville: 'Lyon',
    codePostal: '69001'
  }]));
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}), {
    route: '/list',
    state: {
      highlightIndex: 0
    }
  });
  expect(scrollIntoViewMock).toHaveBeenCalledWith({
    behavior: 'smooth',
    block: 'nearest'
  });
  delete window.HTMLElement.prototype.scrollIntoView;
});
test('retire la mise en evidence apres le delai', () => {
  jest.useFakeTimers();
  localStorage.setItem('registrations', JSON.stringify([{
    nom: 'Martin',
    prenom: 'Alice',
    email: 'alice.martin@email.com',
    dateOfBirth: '1991-04-20',
    ville: 'Lyon',
    codePostal: '69001'
  }]));
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}), {
    route: '/list',
    state: {
      highlightIndex: 0
    }
  });
  expect(_react.screen.getByTestId('registration-item')).toHaveClass('registration-item-highlight');
  (0, _react.act)(() => {
    jest.advanceTimersByTime(4000);
  });
  expect(_react.screen.getByTestId('registration-item')).not.toHaveClass('registration-item-highlight');
});
test('affiche les inscriptions deja enregistrees', () => {
  localStorage.setItem('registrations', JSON.stringify([{
    nom: 'Martin',
    prenom: 'Alice',
    email: 'alice.martin@email.com',
    dateOfBirth: '1991-04-20',
    ville: 'Lyon',
    codePostal: '69001'
  }]));
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}));
  expect(_react.screen.queryByTestId('no-registrations')).not.toBeInTheDocument();
  expect(_react.screen.getByTestId('registration-item')).toHaveTextContent('Alice Martin');
});
test('affiche un message quand aucune inscription n existe', () => {
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}));
  expect(_react.screen.getByTestId('no-registrations')).toBeInTheDocument();
});
test('affiche un toast d erreur quand le parsing localStorage echoue', () => {
  localStorage.setItem('registrations', '{invalid json');
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}));
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('unable to parse saved registrations');
});
test('affiche un toast d erreur generique quand getRegistrations leve sans message', () => {
  const registrationsSpy = jest.spyOn(moduleApi, 'getRegistrations').mockImplementation(() => {
    throw {};
  });
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}));
  expect(_react.screen.getByTestId('error-toast')).toHaveTextContent('Une erreur est survenue');
  registrationsSpy.mockRestore();
});
test('masque le toast d erreur apres le delai', () => {
  jest.useFakeTimers();
  localStorage.setItem('registrations', '{invalid json');
  (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}));
  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 de mise en evidence au demontage du composant', () => {
  jest.useFakeTimers();
  localStorage.setItem('registrations', JSON.stringify([{
    nom: 'Martin',
    prenom: 'Alice',
    email: 'alice.martin@email.com',
    dateOfBirth: '1991-04-20',
    ville: 'Lyon',
    codePostal: '69001'
  }]));
  const _renderWithRouter = (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {}), {
      route: '/list',
      state: {
        highlightIndex: 0
      }
    }),
    unmount = _renderWithRouter.unmount;
  unmount();
  expect(() => {
    (0, _react.act)(() => {
      jest.advanceTimersByTime(4000);
    });
  }).not.toThrow();
});
test('annule le delai du toast au demontage du composant', () => {
  jest.useFakeTimers();
  localStorage.setItem('registrations', '{invalid json');
  const _renderWithRouter2 = (0, _renderWithRouter3.renderWithRouter)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_ListPage.default, {})),
    unmount = _renderWithRouter2.unmount;
  expect(_react.screen.getByTestId('error-toast')).toBeInTheDocument();
  unmount();
  expect(() => {
    (0, _react.act)(() => {
      jest.advanceTimersByTime(3000);
    });
  }).not.toThrow();
});