function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import '@testing-library/jest-dom/extend-expect';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { Button } from '../Button';
import { Drawer } from '.';
import { useOpenClose } from '../../hooks';
describe('Drawer', function () {
  var wrapper;
  var onCloseSpy;
  var children;
  describe('default props', function () {
    beforeEach(function () {
      children = /*#__PURE__*/React.createElement("h1", null, "Arbitrary JSX");
      wrapper = render( /*#__PURE__*/React.createElement(Drawer, null, children));
    });
    it('does not apply the `is-open` class', function () {
      expect(wrapper.getByTestId('drawer-wrapper').classList).not.toContain('is-open');
    });
    it('renders the arbitrary JSX in the correct place', function () {
      expect(wrapper.getByTestId('drawer-content').innerHTML).toContain(renderToStaticMarkup(children));
    });
  });
  describe('and the `isOpen` attribute and `onClose` callback are provided', function () {
    beforeEach(function () {
      children = /*#__PURE__*/React.createElement("h1", null, "Arbitrary JSX");
      onCloseSpy = jest.fn();
      wrapper = render( /*#__PURE__*/React.createElement(Drawer, {
        isOpen: true,
        onClose: onCloseSpy
      }, children));
    });
    it('applies the correct open state style', function () {
      expect(wrapper.getByTestId('drawer-wrapper')).toHaveStyleRule('margin-right', '0');
    });
    describe('and a user clicks on the close button', function () {
      beforeEach(function () {
        fireEvent(wrapper.getByTestId('drawer-close'), new MouseEvent('click', {
          bubbles: true,
          cancelable: true
        }));
      });
      it('invokes the `onClose` callback', function () {
        expect(onCloseSpy).toHaveBeenCalled();
      });
      it('applies the correct closed state style', function () {
        expect(wrapper.getByTestId('drawer-wrapper')).toHaveStyleRule('margin-right', '-280px');
      });
    });
  });
  describe('when the state is being updated externally', function () {
    describe('and the drawer is instantiated as closed', function () {
      beforeEach(function () {
        var DrawerWithUpdate = function DrawerWithUpdate() {
          var _useOpenClose = useOpenClose(false),
            open = _useOpenClose.open,
            toggle = _useOpenClose.toggle;
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
            onClick: toggle
          }, open ? 'Hide' : 'Show'), /*#__PURE__*/React.createElement(Drawer, {
            isOpen: open,
            onClose: onCloseSpy
          }, children));
        };
        wrapper = render( /*#__PURE__*/React.createElement(DrawerWithUpdate, null));
      });
      it('applies the correct closed state style', function () {
        expect(wrapper.getByTestId('drawer-wrapper')).toHaveStyleRule('margin-right', '-280px');
      });
      describe('and the button is clicked', function () {
        beforeEach(function () {
          wrapper.getByText('Show').click();
          return waitFor(function () {
            return wrapper.queryAllByText('Hide');
          });
        });
        it('applies the correct open state style', function () {
          expect(wrapper.getByTestId('drawer-wrapper')).toHaveStyleRule('margin-right', '0');
        });
        describe('and the button is clicked again', function () {
          beforeEach(function () {
            wrapper.getByText('Hide').click();
            return waitFor(function () {
              return wrapper.queryAllByText('Show');
            });
          });
          it('applies the correct closed state style', function () {
            expect(wrapper.getByTestId('drawer-wrapper')).not.toHaveStyleRule('margin-right', '0');
          });
        });
      });
    });
  });
  describe('when a `ref` prop is specified', function () {
    beforeEach(function () {
      var DrawerWithRef = function DrawerWithRef() {
        var _useState = useState('Not set'),
          _useState2 = _slicedToArray(_useState, 2),
          content = _useState2[0],
          setContent = _useState2[1];
        return /*#__PURE__*/React.createElement(Drawer, {
          ref: function ref(el) {
            if (el) {
              setContent(el.getAttribute('data-testid'));
            }
          }
        }, content);
      };
      wrapper = render( /*#__PURE__*/React.createElement(DrawerWithRef, null));
    });
    it('has set the `ref` on the wrapper', function () {
      expect(wrapper.getByTestId('drawer-content')).toHaveTextContent('drawer-wrapper');
    });
  });
  describe('when an arbitrary prop is specified', function () {
    beforeEach(function () {
      children = /*#__PURE__*/React.createElement("h1", null, "Arbitrary JSX");
      wrapper = render( /*#__PURE__*/React.createElement(Drawer, {
        "data-arbitrary": "arbitrary"
      }, children));
    });
    it('should spread arbitrary props', function () {
      expect(wrapper.getByTestId('drawer-wrapper')).toHaveAttribute('data-arbitrary', 'arbitrary');
    });
  });
});