var _templateObject;
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { IconBrightnessLow } from '@defencedigital/icon-library';
import { render, fireEvent } from '@testing-library/react';
import { Button } from './index';
describe('Button', function () {
  var wrapper;
  var onClickSpy;
  var button;
  beforeEach(function () {
    onClickSpy = jest.fn();
  });
  describe('default props', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Button, {
        onClick: onClickSpy
      }, "Click me"));
      button = wrapper.getByLabelText('Click me');
    });
    it('should default the type to "button"', function () {
      expect(button).toHaveAttribute('type', 'button');
    });
    it('should not render an icon', function () {
      expect(wrapper.queryByTestId('button-icon')).toBeNull();
    });
    describe('when the button is clicked', function () {
      beforeEach(function () {
        fireEvent.click(button);
      });
      it('should handle the click event', function () {
        expect(onClickSpy).toHaveBeenCalledTimes(1);
      });
    });
  });
  describe('when the onClick callback has not been specified', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Button, null, "Click me"));
      button = wrapper.getByLabelText('Click me');
    });
    it('does not throw an error when the button is clicked', function () {
      expect(function () {
        return fireEvent.click(button);
      }).not.toThrow();
    });
  });
  describe('when the type is specified', function () {
    it.each(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n      type        | expected\n      ", " | ", "\n      ", " | ", "\n    "])), 'button', 'button', 'submit', 'submit')('should set the type attribute to $type', function (_ref) {
      var type = _ref.type,
        expected = _ref.expected;
      wrapper = render( /*#__PURE__*/React.createElement(Button, {
        onClick: onClickSpy,
        type: type
      }, "Click me"));
      button = wrapper.getByLabelText('Click me');
      expect(button).toHaveAttribute('type', expected);
    });
  });
  describe('when an icon is specified', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Button, {
        icon: /*#__PURE__*/React.createElement(IconBrightnessLow, null)
      }, "Click me"));
    });
    it('should render an icon', function () {
      expect(wrapper.getByTestId('button-icon')).toBeInTheDocument();
    });
    it('should render the icon with an `aria-hidden` attribute', function () {
      expect(wrapper.queryByTestId('button-icon')).toHaveAttribute('aria-hidden', 'true');
    });
  });
  describe('when the button is loading', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Button, {
        icon: /*#__PURE__*/React.createElement(IconBrightnessLow, null),
        isLoading: true
      }, "Click me"));
    });
    it('disables the button', function () {
      expect(wrapper.getByTestId('button')).toBeDisabled();
    });
    it('hides the user-provided icon', function () {
      expect(wrapper.getByTestId('button-icon')).toHaveStyleRule('visibility', 'hidden');
    });
    it('hides the button text', function () {
      expect(wrapper.getByTestId('button-icon')).toHaveStyleRule('visibility', 'hidden');
    });
    it('shows a loading icon with the `aria-hidden` attribute', function () {
      expect(wrapper.getByTestId('loading-icon')).toHaveAttribute('aria-hidden', 'true');
    });
  });
});