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 '@testing-library/jest-dom/extend-expect';
import 'jest-styled-components';
import { ColorNeutral200 } from '@defencedigital/design-tokens';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Checkbox } from '.';
import { CHECKBOX_RADIO_VARIANT } from '../CheckboxRadioBase';
describe('Checkbox', function () {
  var field;
  var label;
  var checkbox;
  var onChangeSpy;
  beforeEach(function () {
    label = '';
    onChangeSpy = jest.fn();
    field = {
      name: 'example1',
      value: 'false',
      onChange: onChangeSpy,
      onBlur: jest.fn()
    };
  });
  describe('when a field has no errors, a label and a value', function () {
    var input;
    beforeEach(function () {
      label = 'My Label 1';
      field.value = 'false';
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        name: field.name,
        value: field.value,
        label: label,
        onChange: field.onChange
      }));
      input = checkbox.getByTestId('checkbox-input');
    });
    it('should render a field with a label', function () {
      expect(checkbox.getByTestId('checkbox-label')).toHaveTextContent('My Label 1');
    });
    it('should not render a description', function () {
      expect(checkbox.queryAllByTestId('checkbox-description')).toHaveLength(0);
    });
    it('should populate the field value', function () {
      expect(input).toHaveAttribute('value', 'false');
    });
    it('should not initially render as checked', function () {
      expect(input).not.toBeChecked();
    });
    describe('and tab is pressed', function () {
      beforeEach(function () {
        return userEvent.tab();
      });
      it('focuses the input', function () {
        expect(input).toHaveFocus();
      });
      describe('and space is pressed', function () {
        beforeEach(function () {
          return userEvent.keyboard('[Space]');
        });
        it('checks the input', function () {
          expect(input).toBeChecked();
        });
        it('calls onChange once', function () {
          expect(field.onChange).toHaveBeenCalledTimes(1);
        });
        describe('and space is pressed again', function () {
          beforeEach(function () {
            onChangeSpy.mockReset();
            return userEvent.keyboard('[Space]');
          });
          it('unchecks the input', function () {
            expect(input).not.toBeChecked();
          });
          it('calls onChange once', function () {
            expect(field.onChange).toHaveBeenCalledTimes(1);
          });
        });
      });
    });
    it('should render a container', function () {
      expect(checkbox.getByTestId('checkbox')).toHaveStyleRule('border', "1px solid ".concat(ColorNeutral200));
      expect(checkbox.getByTestId('checkbox')).toHaveStyleRule('border-radius', '15px');
    });
  });
  describe('when a field has `defaultChecked` prop set', function () {
    beforeEach(function () {
      label = 'My Label 1';
      field.value = 'false';
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        name: field.name,
        value: field.value,
        label: label,
        onChange: field.onChange,
        defaultChecked: true
      }));
    });
    it('should initially render as checked', function () {
      expect(checkbox.getByTestId('checkbox-input')).toBeChecked();
    });
  });
  describe('when a field has `checked` prop set', function () {
    beforeEach(function () {
      label = 'My Label 1';
      field.value = 'false';
      var StateWrapper = function StateWrapper() {
        var _useState = useState(true),
          _useState2 = _slicedToArray(_useState, 2),
          isChecked = _useState2[0],
          setIsChecked = _useState2[1];
        return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Checkbox, {
          name: field.name,
          value: field.value,
          label: label,
          onChange: field.onChange,
          checked: isChecked
        }), /*#__PURE__*/React.createElement("button", {
          onClick: function onClick(_) {
            return setIsChecked(false);
          }
        }, "Click Me!"));
      };
      checkbox = render( /*#__PURE__*/React.createElement(StateWrapper, null));
    });
    it('should initially render as checked', function () {
      expect(checkbox.getByTestId('checkbox-input')).toBeChecked();
    });
    describe('and the button is clicked, changing the `isChecked` state', function () {
      beforeEach(function () {
        return userEvent.click(checkbox.getByTestId('change-checked-state'));
      });
      it('should uncheck the checkbox', function () {
        expect(checkbox.getByTestId('checkbox-input')).not.toBeChecked();
      });
    });
  });
  describe('when an additional class it provided', function () {
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        className: "test",
        name: field.name,
        value: field.value,
        label: label,
        onChange: field.onChange
      }));
    });
    it('should attach the class to the wrapper', function () {
      expect(checkbox.queryByTestId('checkbox')).toHaveClass('test');
    });
  });
  describe('when an id is provided', function () {
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        id: "test",
        name: field.name,
        value: field.value,
        label: label,
        onChange: field.onChange
      }));
    });
    it('should attach the id to the field', function () {
      expect(checkbox.queryByTestId('checkbox-input')).toHaveAttribute('id', 'test');
    });
    it('should associate the label to the field with the custom id', function () {
      expect(checkbox.queryByTestId('checkbox-label')).toHaveAttribute('for', 'test');
    });
  });
  describe('when an arbitrary prop is provided', function () {
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        "data-arbitrary": "arbitrary",
        name: field.name,
        value: field.value,
        label: label,
        onChange: field.onChange
      }));
    });
    it('should drill the arbitrary prop', function () {
      expect(checkbox.queryByTestId('checkbox-input')).toHaveAttribute('data-arbitrary', 'arbitrary');
    });
  });
  describe('when a field has a description', function () {
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        description: "Description",
        label: "Label",
        name: field.name
      }));
    });
    it('should render a description', function () {
      expect(checkbox.getByTestId('checkbox-description')).toHaveTextContent('Description');
    });
  });
  describe('when a field has a description that is arbitrary JSX', function () {
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        description: /*#__PURE__*/React.createElement("div", null, "Arbitrary content"),
        label: "Label",
        name: field.name
      }));
    });
    it('should render a arbitrary description', function () {
      expect(checkbox.getByText('Arbitrary content')).toBeInTheDocument();
    });
  });
  describe('when a field does not have a container', function () {
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        label: "Label",
        name: field.name,
        variant: CHECKBOX_RADIO_VARIANT.NO_CONTAINER
      }));
    });
    it('should not render a container', function () {
      expect(checkbox.getByTestId('checkbox')).not.toHaveStyleRule('border', "1px solid ".concat(ColorNeutral200));
      expect(checkbox.getByTestId('checkbox')).not.toHaveStyleRule('border-radius', '15px');
    });
  });
  describe('when the default `id` is used and input is toggled', function () {
    var initialId;
    beforeEach(function () {
      checkbox = render( /*#__PURE__*/React.createElement(Checkbox, {
        label: "label",
        name: "name"
      }));
      var input = checkbox.getByTestId('checkbox-input');
      initialId = input.id;
      return userEvent.click(checkbox.getByTestId('checkbox'));
    });
    it('does not generate a new `id`', function () {
      expect(checkbox.getByTestId('checkbox-input')).toHaveAttribute('id', initialId);
    });
  });
});