"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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 _reactHooks = require("@testing-library/react-hooks");

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

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

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

var _stepDefaults = require("../../ui/compositions/Tutorials/Modal/constants/stepDefaults");

var ApiActions = _interopRequireWildcard(require("./apiActions"));

var _fetchMock = _interopRequireDefault(require("fetch-mock"));

require("isomorphic-fetch");

var tutorials = [{
  _id: '222',
  backgroundColor: '#fff',
  description: 'First title card desc',
  emoji: '',
  hero: true,
  published: true,
  title: 'First title'
}, {
  _id: '443',
  backgroundColor: '#fff',
  description: 'Second title card desc',
  emoji: '',
  hero: false,
  published: false,
  title: 'Second Title'
}];
var getClientRectSpy = jest.fn(function () {
  return {
    width: 100
  };
});
Range.prototype.getBoundingClientRect = getClientRectSpy;
Range.prototype.getClientRects = getClientRectSpy;
describe('Tutorial', function () {
  describe('Defaults', function () {
    it('should display empty state by default', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var node;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _fetchMock["default"].get('http://localhost:3000/tutorials/data', {
                tutorials: [],
                hero: {},
                moduleEnabled: true
              });

              node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], {
                baseUrl: 'http://localhost:3000',
                confirmationTarget: 'body',
                isAdmin: true,
                modalTarget: 'body'
              }));
              _context2.next = 4;
              return (0, _reactHooks.act)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
                return _regenerator["default"].wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.next = 2;
                        return Promise.resolve(node);

                      case 2:
                        _context.next = 4;
                        return new Promise(function (resolve) {
                          return setImmediate(resolve);
                        });

                      case 4:
                        node.update();

                      case 5:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              })));

            case 4:
              node.update();
              expect(node.find(_compositions.TutorialObstacle)).toHaveLength(1);

              _fetchMock["default"].done();

            case 7:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  describe('View and Data', function () {
    var node;
    beforeAll(function () {
      _fetchMock["default"].get('http://localhost:3000/tutorials/data', {
        tutorials: tutorials,
        hero: tutorials[0],
        moduleEnabled: true
      }, {
        overwriteRoutes: true
      });

      _fetchMock["default"].get('http://localhost:3000/tutorials/endpoints', {
        categoryId: 'id',
        children: [],
        title: 'page title'
      });
    });
    beforeEach(function () {
      node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], {
        baseUrl: 'http://localhost:3000',
        confirmationTarget: 'body',
        isAdmin: true,
        modalTarget: 'body'
      }));
    });
    afterAll(function () {
      _fetchMock["default"].restore();

      _fetchMock["default"].reset();
    });
    it('should not display button bar contents if not admin', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
      var n2, buttonBar;
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              n2 = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], {
                baseUrl: 'http://localhost:3000',
                confirmationTarget: 'body',
                isAdmin: false,
                modalTarget: 'body'
              }));
              _context4.next = 3;
              return (0, _reactHooks.act)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
                return _regenerator["default"].wrap(function _callee3$(_context3) {
                  while (1) {
                    switch (_context3.prev = _context3.next) {
                      case 0:
                        _context3.next = 2;
                        return Promise.resolve(n2);

                      case 2:
                        _context3.next = 4;
                        return new Promise(function (resolve) {
                          return setImmediate(resolve);
                        });

                      case 4:
                        n2.update();

                      case 5:
                      case "end":
                        return _context3.stop();
                    }
                  }
                }, _callee3);
              })));

            case 3:
              buttonBar = n2.find(_compositions.TutorialCard).find('CardButtonBar');
              expect(buttonBar.find('.TutorialCard-Toolbar-Arrow')).toHaveLength(0);

            case 5:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
    it('should call getTutorials on init', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
      var n2, cards;
      return _regenerator["default"].wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              n2 = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], {
                baseUrl: 'http://localhost:3000',
                confirmationTarget: 'body',
                isAdmin: false,
                modalTarget: 'body'
              }));
              _context6.next = 3;
              return (0, _reactHooks.act)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
                return _regenerator["default"].wrap(function _callee5$(_context5) {
                  while (1) {
                    switch (_context5.prev = _context5.next) {
                      case 0:
                        _context5.next = 2;
                        return Promise.resolve(n2);

                      case 2:
                        _context5.next = 4;
                        return new Promise(function (resolve) {
                          return setImmediate(resolve);
                        });

                      case 4:
                        n2.update();

                      case 5:
                      case "end":
                        return _context5.stop();
                    }
                  }
                }, _callee5);
              })));

            case 3:
              cards = n2.find(_compositions.TutorialCard).find('CardButtonBar');
              expect(cards).toHaveLength(2);

            case 5:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
    describe('confirmation modal', function () {
      var n2;
      var card;
      beforeEach( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8() {
        return _regenerator["default"].wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                n2 = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], {
                  baseUrl: 'http://localhost:3000',
                  confirmationTarget: 'body',
                  isAdmin: true,
                  modalTarget: 'body'
                }));
                _context8.next = 3;
                return (0, _reactHooks.act)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
                  return _regenerator["default"].wrap(function _callee7$(_context7) {
                    while (1) {
                      switch (_context7.prev = _context7.next) {
                        case 0:
                          _context7.next = 2;
                          return Promise.resolve(n2);

                        case 2:
                          _context7.next = 4;
                          return new Promise(function (resolve) {
                            return setImmediate(resolve);
                          });

                        case 4:
                          n2.update();

                        case 5:
                        case "end":
                          return _context7.stop();
                      }
                    }
                  }, _callee7);
                })));

              case 3:
                card = n2.find('TutorialCard').first();

              case 4:
              case "end":
                return _context8.stop();
            }
          }
        }, _callee8);
      })));
      it('should open on publish', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9() {
        var modal, title;
        return _regenerator["default"].wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                card.prop('publish')({
                  _id: '123'
                });
                _context9.next = 3;
                return (0, _reactHooks.act)(function () {
                  return n2.update();
                });

              case 3:
                modal = n2.find('.TutorialConfirmation');
                title = modal.find('h4').first();
                expect(title.text()).toStrictEqual('Are you sure you want to unpublish this Recipe?');

              case 6:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      })));
      it('should open on delete', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
        var modal, title;
        return _regenerator["default"].wrap(function _callee10$(_context10) {
          while (1) {
            switch (_context10.prev = _context10.next) {
              case 0:
                card.prop('deleteTutorial')({
                  _id: '123'
                });
                _context10.next = 3;
                return (0, _reactHooks.act)(function () {
                  return n2.update();
                });

              case 3:
                modal = n2.find('.TutorialConfirmation');
                title = modal.find('h4').first();
                expect(title.text()).toStrictEqual('Are you sure you want to delete this Recipe?');

              case 6:
              case "end":
                return _context10.stop();
            }
          }
        }, _callee10);
      })));
      it('should open on makeHero', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11() {
        var modal, title;
        return _regenerator["default"].wrap(function _callee11$(_context11) {
          while (1) {
            switch (_context11.prev = _context11.next) {
              case 0:
                card.prop('makeHero')({
                  _id: '123',
                  published: false
                });
                _context11.next = 3;
                return (0, _reactHooks.act)(function () {
                  return n2.update();
                });

              case 3:
                modal = n2.find('.TutorialConfirmation');
                title = modal.find('h4').first();
                expect(title.text()).toStrictEqual('Are you sure you want to feature an unpublished Recipe?');

              case 6:
              case "end":
                return _context11.stop();
            }
          }
        }, _callee11);
      })));
      it('should call api if tutorial is published', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12() {
        var modal, title;
        return _regenerator["default"].wrap(function _callee12$(_context12) {
          while (1) {
            switch (_context12.prev = _context12.next) {
              case 0:
                _fetchMock["default"].post('http://localhost:3000/tutorials/123/makeHero', {
                  success: true
                });

                card.prop('makeHero')({
                  _id: '123',
                  published: true
                });
                _context12.next = 4;
                return (0, _reactHooks.act)(function () {
                  return n2.update();
                });

              case 4:
                modal = n2.find('.TutorialConfirmation');
                title = modal.find('h4').first();
                expect(title.text()).toStrictEqual('Title');

              case 7:
              case "end":
                return _context12.stop();
            }
          }
        }, _callee12);
      })));
    });
    it('should handle module toggle', function () {
      _fetchMock["default"].post('http://localhost:3000/tutorials/toggleModule', {
        success: true
      });

      var toggle = node.find(_components.Toggle).find('input');
      var spy = jest.spyOn(ApiActions, 'toggleModule');
      toggle.simulate('change', {
        test: true
      });
      expect(spy).toHaveBeenCalledTimes(1);
    });
    it('should set state when modal is opened from creation', function () {
      var button = node.find('.TutorialToolbar').find('button').at(1);
      button.simulate('click');
      var modal = node.find(_compositions.TutorialModal);
      expect(modal.prop('action')).toStrictEqual('Create');
    });
    it('should call closeTutorialModal correctly', function () {
      var modal = node.find(_compositions.TutorialModal);
      modal.prop('closeTutorialModal')();
      var m2 = node.find(_compositions.TutorialModal);
      expect(m2.prop('open')).toStrictEqual(false);
    });
    it('should call for tutorial data when a slug is passed', function () {
      _fetchMock["default"].get('http://localhost:3000/tutorials/test/data', {
        tutorial: _stepDefaults.DEFAULT_TUTORIAL
      });

      var spy = jest.spyOn(ApiActions, 'getTutorialBySlug');
      (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], {
        baseUrl: 'http://localhost:3000',
        confirmationTarget: 'body',
        isAdmin: false,
        modalTarget: 'body',
        slug: 'test'
      }));
      expect(spy).toHaveBeenCalledTimes(1);
    });
  });
});