"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _2 = _interopRequireDefault(require("."));

var _compositions = require("../../ui/compositions");

var _components = require("../../ui/components");

// Helper function returns a promise that resolves after all other promise mocks,
// even if they are chained like Promise.resolve().then(...)
// Technically: this is designed to resolve on the next macrotask
function tick() {
  return new Promise(function (resolve) {
    setTimeout(resolve, 0);
  });
}

var auditlog = {
  _id: '5f1f1088f335620189768998',
  description: 'Edited \n page "Versions"',
  project: 'guides',
  link: 'https://dash.readme.io/project/guides/v2.0/docs/versions',
  filter: {
    project: 'Product Guide',
    user: 'Andy Trattner\nandy@readme.io',
    page: 'Versions',
    version: '2.0',
    action: 'EDIT'
  },
  createdAt: '2020-07-27T17:36:29.752+0000'
};
var changelog = {
  _id: '5f0cddddc53967004d2fad39',
  link: 'https://docs.readme.com/guides/changelog/2020-07-13-v3-5-0',
  // must generate on the backend
  metadata: {
    image: [],
    title: '',
    description: ''
  },
  title: 'Owlet Weekly Update (Week of July 13)',
  slug: '2020-07-13-v3-5-0',
  body: "> 🎉\n> Welcome to another update!\n> We have exciting news to share.\n> This week, we updated the [ReadMe API](https://docs.readme.com/developers/reference/intro)\n> To include poems with every err'!\n\nAlas, I'm not nearly as good at writing poems as Greg (our CEO) is — who literally wrote over 30 unique poems for each and every error code that is returned from the [ReadMe HTTP API](https://docs.readme.com/developers/reference/intro)! 🖋️\n\nIn other non-poetry-related (but nevertheless exciting) news, we also shipped some exciting updates to our icons, made a ton of usability improvements to our GitHub integration, and lots more. More information below! :owlbert-reading:\n\n## ✨",
  type: 'added',
  hidden: false,
  pendingAlgoliaPublish: false,
  user: '560efaf7a8e6680d00b2e3f9',
  createdAt: '2020-07-14T22:19:00.000Z',
  project: '542334961972e21d7dae4e00',
  __v: 0
};
describe('Staging', function () {
  describe('Defaults', function () {
    it('should display empty state by default', function () {
      var node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2["default"], {
        awaiting: [],
        deploy: function deploy() {},
        deployed: [],
        deployments: [],
        modalTarget: 'body'
      }));
      expect(node.find('.Staging')).toHaveLength(1);
      expect(node.find(_compositions.DashHeader)).toHaveLength(1);
      expect(node.find(_components.Button)).toHaveLength(3);
      expect(node.find(_components.Modal)).toHaveLength(1);
      expect(node.find(_compositions.StagingFeed)).toHaveLength(2);
      expect(node.find('.StagingFeed-Timeline-Day-Deployed_empty')).toHaveLength(1);
      expect(node.containsMatchingElement( /*#__PURE__*/_react["default"].createElement("div", null, "No deployment history"))).toBe(true);
    });
    it('should display logs if logs exist', function () {
      var node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2["default"], {
        awaiting: [{
          date: new Date('2020-07-19'),
          changelogs: [],
          auditlogs: [auditlog]
        }],
        deploy: function deploy() {},
        deployed: [{
          date: new Date('2020-07-01'),
          changelogs: [changelog],
          auditlogs: []
        }],
        deployments: [new Date('2020-07-01')],
        loadMore: function loadMore() {},
        modalTarget: 'body'
      }));
      expect(node.find(_compositions.Log)).toHaveLength(2);
      expect(node.find('.Log-Link')).toHaveLength(2);
      expect(node.find('.Log-Link').at(0).text().trim()).toBe('page "Versions"');
      expect(node.find('.Log-Link').at(1).text().trim()).toBe("What's New");
      expect(node.find('.StagingFeed_deployed')).toHaveLength(1); // .find() results in both React nodes & DOM nodes - the below tests were resulting in length = 2
      // using .hostNodes() filters so that only the DOM nodes come through
      // https://github.com/enzymejs/enzyme/issues/836

      expect(node.find('.Log_Owlet').hostNodes()).toHaveLength(1);
      expect(node.find('.Log_AuditLog').hostNodes()).toHaveLength(1);
    });
  });
  describe('Staging modal', function () {
    it('should open modal when deploy button is clicked', function () {
      var node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2["default"], {
        awaiting: [{
          date: new Date('2020-07-19'),
          changelogs: [],
          auditlogs: [auditlog]
        }],
        deploy: function deploy() {},
        deployed: [{
          date: new Date('2020-07-01'),
          changelogs: [changelog],
          auditlogs: []
        }],
        deployments: [],
        loadMore: function loadMore() {},
        modalTarget: 'body'
      })); // before opening the modal

      var deployButton = node.findWhere(function (n) {
        return n.hasClass('Button') && n.text() === 'Deploy';
      });
      expect(deployButton).toHaveLength(1);
      deployButton.simulate('click'); // after opening the modal

      expect(node.find('.Modal-Header').text()).toBe('Deploying Changes');
      expect(node.findWhere(function (n) {
        return n.hasClass('Button') && n.text() === 'Deploy';
      })).toHaveLength(2);
      expect(node.find('.Modal-Body').text()).toContain('Deploy your staged changes');
    });
    it('should show success message after successful deploy', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var deployMock, node, deployButton, modal, modalDeployButton;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              deployMock = jest.fn(function () {
                return new Promise(function (resolve) {
                  resolve();
                });
              });
              node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2["default"], {
                awaiting: [{
                  date: new Date('2020-07-19'),
                  changelogs: [],
                  auditlogs: [auditlog]
                }],
                deploy: deployMock,
                deployed: [{
                  date: new Date('2020-07-01'),
                  changelogs: [changelog],
                  auditlogs: []
                }],
                deployments: [new Date('2020-07-01')],
                loadMore: function loadMore() {},
                modalTarget: 'body'
              })); // before opening the modal

              deployButton = node.findWhere(function (n) {
                return n.hasClass('Button') && n.text() === 'Deploy';
              });
              expect(deployButton).toHaveLength(1);
              deployButton.simulate('click'); // after opening the modal

              expect(node.find('.Modal-Header').text()).toBe('Deploying Changes');
              modal = node.find(_components.Modal);
              modalDeployButton = modal.findWhere(function (n) {
                return n.hasClass('Button') && n.text() === 'Deploy';
              });
              expect(modalDeployButton).toHaveLength(1);
              modalDeployButton.simulate('click');
              _context.next = 12;
              return tick();

            case 12:
              // successful deploy
              expect(deployMock).toHaveBeenCalledTimes(1);
              expect(node.find('.Modal-Header').text()).toContain('Hoot Hoot Hooray');

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
    it('should show error message after failed deploy', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var deployMock, node, deployButton, modal, modalDeployButton;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              deployMock = jest.fn(function () {
                return new Promise(function (_, reject) {
                  // eslint-disable-next-line prefer-promise-reject-errors
                  reject({
                    data: {
                      errors: {
                        $all: 'Some error'
                      }
                    }
                  });
                });
              });
              node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2["default"], {
                awaiting: [{
                  date: new Date('2020-07-19'),
                  changelogs: [],
                  auditlogs: [auditlog]
                }],
                deploy: deployMock,
                deployed: [{
                  date: new Date('2020-07-01'),
                  changelogs: [changelog],
                  auditlogs: []
                }],
                deployments: [new Date('2020-07-01')],
                loadMore: function loadMore() {},
                modalTarget: 'body'
              })); // before opening the modal

              deployButton = node.findWhere(function (n) {
                return n.hasClass('Button') && n.text() === 'Deploy';
              });
              expect(deployButton).toHaveLength(1);
              deployButton.simulate('click'); // after opening the modal

              expect(node.find('.Modal-Header').text()).toBe('Deploying Changes');
              modal = node.find(_components.Modal);
              modalDeployButton = modal.findWhere(function (n) {
                return n.hasClass('Button') && n.text() === 'Deploy';
              });
              expect(modalDeployButton).toHaveLength(1);
              modalDeployButton.simulate('click');
              _context2.next = 12;
              return tick();

            case 12:
              // unsuccessful deploy
              expect(deployMock).toHaveBeenCalledTimes(1);
              expect(node.find('.Modal-Header').text()).toContain('Error');

            case 14:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  it('disables the loadMore button within <Staging> if deployments is empty', function () {
    var node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_2["default"], {
      awaiting: [],
      deploy: function deploy() {},
      deployed: [],
      deployments: [],
      modalTarget: 'body'
    }));
    expect(node.find('.Staging-LoadMore button')).toHaveLength(1);
    expect(node.find('.Staging-LoadMore button').getDOMNode()).toBeDisabled();
  });
});