import { _ as _asyncToGenerator, r as regenerator } from '../../index-qSfOyGhk.js';
import { _ as _extends } from '../../extends-C4cduGrv.js';
import { _ as _objectWithoutProperties } from '../../objectWithoutProperties-TXwlfNxG.js';
import React__default from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { n as node, r as rest } from '../../index-DVWixydF.js';
import InfiniteScroll from '../index.js';
import '../../typeof-CgC7Bq6K.js';
import '../../toConsumableArray-dKQYsfpV.js';
import '../../slicedToArray-BB-AwwkK.js';
import '../../createClass-DT8Y0mhp.js';
import '../../setPrototypeOf-Bchah3-D.js';
import '../../_commonjsHelpers-_1FafXWI.js';
import 'http';
import 'https';
import 'util';
import 'tty';
import 'os';
import 'timers';
import '../../defineProperty-CEd-kZQW.js';
import 'stream';
import 'url';
import 'zlib';
import 'prop-types';

var _excluded = ["requestPath", "usePage"];

var InfiniteScrollComponent = function InfiniteScrollComponent(_ref) {
  var requestPath = _ref.requestPath,
      usePage = _ref.usePage,
      props = _objectWithoutProperties(_ref, _excluded);

  var fetchImages = React__default.useMemo(function () {
    return function (page) {
      // use page # if provided, if set as boolean true, use the current page passed to callback
      var path = !usePage ? requestPath : typeof usePage === 'number' ? "".concat(requestPath, "?page=").concat(usePage) : "".concat(requestPath, "?page=").concat(page);
      return fetch(path).then(function (res) {
        return res.json();
      });
    };
  }, [requestPath, usePage]);
  return /*#__PURE__*/React__default.createElement(InfiniteScroll, _extends({}, props, {
    loadFunction: fetchImages
  }), function (item) {
    return /*#__PURE__*/React__default.createElement("div", {
      key: item.id,
      "data-testid": "note"
    }, item.name);
  });
};

describe('InfiniteScroll', function () {
  var server = node.setupServer(rest.get("/reindeers?page=1", function (req, res, ctx) {
    return res(ctx.json([{
      id: 1,
      name: 'Dasher',
      skill: 'Sewing'
    }, {
      id: 2,
      name: 'Dancer',
      skill: 'Dancing'
    }, {
      id: 3,
      name: 'Prancer',
      skill: 'Prancing'
    }, {
      id: 4,
      name: 'Vixen',
      skill: 'Magic'
    }, {
      id: 5,
      name: 'Comet',
      skill: 'Good with kids'
    }]));
  }), rest.get("/reindeers?page=2", function (req, res, ctx) {
    return res(ctx.json([{
      id: 6,
      name: 'Cupid',
      skill: 'Bringing people together'
    }, {
      id: 7,
      name: 'Donner',
      skill: 'Singing'
    }, {
      id: 8,
      name: 'Blitzen',
      skill: 'Can electrify others'
    }, {
      id: 9,
      name: 'Rudolph',
      skill: 'Glowing nose'
    }, {
      id: 10,
      name: 'Olive',
      skill: 'Good at hide-and-seek'
    }]));
  }), rest.get('/elves?page=1', function (req, res, ctx) {
    return res(ctx.json([]));
  }));
  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();
  });
  it('loads container into the document', function () {
    render( /*#__PURE__*/React__default.createElement(InfiniteScrollComponent, {
      requestPath: "/reindeers",
      "data-testid": "infinite",
      pageLimit: 2,
      usePage: true
    }));
    var infiniteFeed = screen.getByTestId('infinite');
    expect(infiniteFeed).toBeInTheDocument();
  });
  it('loads initial container items into the document', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
    var infiniteFeed;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(InfiniteScrollComponent, {
              requestPath: "/reindeers",
              "data-testid": "infinite",
              pageLimit: 2,
              usePage: true
            }));
            _context.next = 3;
            return screen.findAllByTestId('note');

          case 3:
            infiniteFeed = _context.sent;
            return _context.abrupt("return", expect(infiniteFeed).toHaveLength(5));

          case 5:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('displays empty message when there are no results', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
    var emptyMessage;
    return regenerator.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(InfiniteScrollComponent, {
              requestPath: "/elves",
              "data-testid": "infinite",
              emptyMessage: /*#__PURE__*/React__default.createElement("h1", {
                "aria-label": "empty"
              }, "Empty")
            }));
            _context2.next = 3;
            return screen.findByRole('heading', {
              name: 'empty'
            });

          case 3:
            emptyMessage = _context2.sent;
            expect(emptyMessage).toBeInTheDocument();
            expect(emptyMessage).toHaveTextContent('Empty');

          case 6:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('shows error message on failed requests', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3() {
    var errorMessage, emptyMessage;
    return regenerator.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(InfiniteScrollComponent, {
              requestPath: "/santa",
              "data-testid": "infinite",
              errorMessage: /*#__PURE__*/React__default.createElement("h1", {
                "aria-label": "error"
              }, "Broken")
            }));
            _context3.next = 3;
            return screen.findByRole('heading', {
              name: 'error'
            });

          case 3:
            errorMessage = _context3.sent;
            _context3.next = 6;
            return screen.queryByRole('heading', {
              name: 'empty'
            });

          case 6:
            emptyMessage = _context3.sent;
            expect(errorMessage).toBeInTheDocument();
            expect(emptyMessage).not.toBeInTheDocument();
            expect(errorMessage).toHaveTextContent('Broken');

          case 10:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
  it('calls onError for failed requests', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4() {
    var fn, errorMessage;
    return regenerator.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            fn = jest.fn();
            render( /*#__PURE__*/React__default.createElement(InfiniteScrollComponent, {
              requestPath: "/santa",
              "data-testid": "infinite",
              onError: fn,
              errorMessage: /*#__PURE__*/React__default.createElement("h1", {
                "aria-label": "error"
              }, "Broken")
            }));
            _context4.next = 4;
            return screen.findByRole('heading', {
              name: 'error'
            });

          case 4:
            errorMessage = _context4.sent;
            expect(errorMessage).toBeInTheDocument();
            expect(fn).toHaveBeenCalled();

          case 7:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }))); // Would love to test this more (scroll behavior) but due to limitations of jsdom as stated here:
  // https://github.com/testing-library/react-testing-library/issues/671#issuecomment-629468376
  // my options are quite limited. If these limitations get lifted in the future, revisit!
});
