function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { IconLayers, IconAnchor, IconShare } from '@defencedigital/icon-library';
import { render, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Dropdown } from './Dropdown';
var options = [{
  label: 'Option 1',
  value: '1'
}, {
  label: 'Option 2',
  value: '2'
}, {
  label: 'Option 3',
  value: '3'
}];
describe('Dropdown', function () {
  var wrapper;
  var onSelectSpy;
  describe('all props', function () {
    beforeEach(function () {
      onSelectSpy = jest.fn();
      wrapper = render( /*#__PURE__*/React.createElement(Dropdown, {
        onSelect: onSelectSpy,
        options: options,
        label: "Dropdown label",
        labelIcon: /*#__PURE__*/React.createElement(IconLayers, null)
      }));
    });
    it('should render the label icon', function () {
      expect(wrapper.getByTestId('placeholder-icon')).toBeInTheDocument();
    });
    it('should render the label', function () {
      expect(wrapper.getByTestId('placeholder-label')).toHaveTextContent('Dropdown label');
    });
    it('should set the ARIA attributes on the input', function () {
      expect(wrapper.container.querySelector('input')).toHaveAttribute('aria-label', 'Dropdown label');
    });
    describe('when the dropdown is clicked', function () {
      beforeEach(function () {
        fireEvent.focus(wrapper.getByRole('textbox'));
        fireEvent.keyDown(wrapper.getByRole('textbox'), {
          key: 'ArrowDown',
          code: 40
        });
      });
      it('should render three options', function () {
        expect(wrapper.getByText('Option 1')).toBeInTheDocument();
        expect(wrapper.getByText('Option 2')).toBeInTheDocument();
        expect(wrapper.getByText('Option 3')).toBeInTheDocument();
      });
      it('should not render label end adornments', function () {
        expect(wrapper.queryAllByTestId('dropdown-label-icon-visibility')).toHaveLength(0);
        expect(wrapper.queryAllByTestId('dropdown-label-icon-hidden')).toHaveLength(0);
      });
      describe('when the second option is clicked', function () {
        beforeEach(function () {
          wrapper.getByText('Option 2').click();
        });
        it('should call the `onSelect` callback once', function () {
          expect(onSelectSpy).toHaveBeenCalledTimes(1);
          expect(onSelectSpy).toHaveBeenCalledWith('2');
        });
      });
    });
  });
  describe('minimal props', function () {
    beforeEach(function () {
      onSelectSpy = jest.fn();
      wrapper = render( /*#__PURE__*/React.createElement(Dropdown, {
        onSelect: onSelectSpy,
        options: options,
        label: "Dropdown label"
      }));
    });
    it('should render the label', function () {
      expect(wrapper.getByTestId('placeholder-label')).toBeInTheDocument();
    });
    it('should not render the label icon', function () {
      expect(wrapper.queryAllByTestId('placeholder-icon')).toHaveLength(0);
    });
  });
  describe('with option icons', function () {
    beforeEach(function () {
      onSelectSpy = jest.fn();
      var optionsWithIcons = [_objectSpread(_objectSpread({}, options[0]), {}, {
        icon: /*#__PURE__*/React.createElement(IconAnchor, null),
        isHidden: true
      }), _objectSpread(_objectSpread({}, options[1]), {}, {
        icon: /*#__PURE__*/React.createElement(IconAnchor, null),
        isVisible: true
      }), _objectSpread(_objectSpread({}, options[3]), {}, {
        icon: /*#__PURE__*/React.createElement(IconAnchor, null),
        rightContent: /*#__PURE__*/React.createElement(IconShare, null)
      })];
      wrapper = render( /*#__PURE__*/React.createElement(Dropdown, {
        onSelect: onSelectSpy,
        options: optionsWithIcons,
        label: "Dropdown label"
      }));
      fireEvent.focus(wrapper.getByRole('textbox'));
      fireEvent.keyDown(wrapper.getByRole('textbox'), {
        key: 'ArrowDown',
        code: 40
      });
    });
    it('should render the label icons', function () {
      expect(wrapper.getAllByTestId('dropdown-label-icon')).toHaveLength(3);
    });
    it('should render the label end adornments', function () {
      expect(wrapper.getByTestId('dropdown-label-icon-visibility')).toBeInTheDocument();
      expect(wrapper.getByTestId('dropdown-label-icon-hidden')).toBeInTheDocument();
      expect(wrapper.getByTestId('dropdown-label-icon-share')).toBeInTheDocument();
    });
  });
  describe('when onChange is omitted and the dropdown is clicked', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Dropdown, {
        options: options,
        label: "Dropdown label"
      }));
      return userEvent.click(wrapper.getByRole('textbox'));
    });
    it('does not throw an error when an option is clicked', function () {
      expect(function () {
        return wrapper.getByText('Option 1').click();
      }).not.toThrow();
    });
  });
});