import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { render, fireEvent } from '@testing-library/react';
import { Dialog } from '.';
describe('Modal', function () {
  var wrapper;
  var title;
  var description;
  var onConfirm;
  var onCancel;
  var isOpen;
  beforeEach(function () {
    title = 'Dialog Title';
    description = 'Dialog description.';
    isOpen = true;
    onConfirm = jest.fn();
    onCancel = jest.fn();
  });
  describe('when props are provided for a standard dialog component', function () {
    describe('and it is set to be initially open', function () {
      beforeEach(function () {
        wrapper = render( /*#__PURE__*/React.createElement(Dialog, {
          "data-arbitrary": "arbitrary",
          title: title,
          description: description,
          onConfirm: onConfirm,
          onCancel: onCancel,
          isOpen: isOpen
        }));
      });
      it('should spread arbitrary props', function () {
        expect(wrapper.getByTestId('dialog')).toHaveAttribute('data-arbitrary', 'arbitrary');
      });
      it('should set the `aria-labelledby` attribute to the ID of the title', function () {
        var titleId = wrapper.getByTestId('dialog-title').getAttribute('id');
        expect(wrapper.getByTestId('dialog')).toHaveAttribute('aria-labelledby', titleId);
      });
      it('should render the title', function () {
        expect(wrapper.getByTestId('dialog-title')).toHaveTextContent(title);
      });
      it('should render the description', function () {
        expect(wrapper.getByTestId('dialog-description')).toHaveTextContent(description);
      });
      describe('and the primary button is clicked', function () {
        beforeEach(function () {
          fireEvent(wrapper.getByTestId('modal-primary'), new MouseEvent('click', {
            bubbles: true,
            cancelable: true
          }));
        });
        it('should invoke the onConfirm callback', function () {
          expect(onConfirm).toHaveBeenCalled();
        });
      });
      describe('and the secondary button is clicked', function () {
        beforeEach(function () {
          fireEvent(wrapper.getByTestId('modal-secondary'), new MouseEvent('click', {
            bubbles: true,
            cancelable: true
          }));
        });
        it('should invoke the onCancel callback', function () {
          expect(onCancel).toHaveBeenCalled();
        });
      });
    });
  });
  describe('when aria-label but no title is set', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Dialog, {
        "aria-label": "Accessible name",
        "data-arbitrary": "arbitrary",
        description: description,
        onConfirm: onConfirm,
        onCancel: onCancel,
        isOpen: isOpen
      }));
    });
    it('does not set the `aria-labelledby` attribute', function () {
      expect(wrapper.getByTestId('dialog')).not.toHaveAttribute('aria-labelledby');
    });
    it('sets the `aria-label` attribute', function () {
      expect(wrapper.getByTestId('dialog')).toHaveAttribute('aria-label', 'Accessible name');
    });
  });
  describe('when the Dialog description changes', function () {
    var initialTitleId;
    var initialDescriptionId;
    var ExampleDialog = function ExampleDialog(_ref) {
      var exampleDescription = _ref.exampleDescription;
      return /*#__PURE__*/React.createElement(Dialog, {
        title: "Example title",
        description: exampleDescription,
        isOpen: true
      });
    };
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(ExampleDialog, {
        exampleDescription: "initial content"
      }));
      initialTitleId = wrapper.getByTestId('dialog-title').id;
      initialDescriptionId = wrapper.getByTestId('modal-body').id;
      wrapper.rerender( /*#__PURE__*/React.createElement(ExampleDialog, {
        exampleDescription: "new content"
      }));
    });
    it('does not generate new a new title `id`', function () {
      expect(wrapper.getByTestId('dialog-title')).toHaveAttribute('id', initialTitleId);
    });
    it('does not generate new a new description `id`', function () {
      expect(wrapper.getByTestId('modal-body')).toHaveAttribute('id', initialDescriptionId);
    });
  });
});