"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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

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

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

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

var _immutable = require("immutable");

var _baseRecord = _interopRequireDefault(require("../baseRecord"));

var _instructureValidations = require("instructure-validations");

describe('BaseRecord', function () {
  var TestRecord =
  /*#__PURE__*/
  function (_BaseRecord) {
    (0, _inherits2.default)(TestRecord, _BaseRecord);

    function TestRecord() {
      (0, _classCallCheck2.default)(this, TestRecord);
      return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(TestRecord).apply(this, arguments));
    }

    return TestRecord;
  }((0, _baseRecord.default)({
    title: '',
    nestedValue: (0, _immutable.fromJS)({
      here: ''
    })
  }));

  TestRecord.validations = {
    title: [(0, _instructureValidations.rule)('presence', {
      message: 'title cannot be blank'
    })],
    nestedValue: {
      here: [(0, _instructureValidations.rule)('presence', {
        message: 'here cannot be blank'
      })]
    }
  };
  describe('.create', function () {
    var MockRecord1 =
    /*#__PURE__*/
    function (_BaseRecord2) {
      (0, _inherits2.default)(MockRecord1, _BaseRecord2);

      function MockRecord1() {
        (0, _classCallCheck2.default)(this, MockRecord1);
        return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(MockRecord1).apply(this, arguments));
      }

      return MockRecord1;
    }((0, _baseRecord.default)({
      id: '0'
    }));

    var MockRecord2 =
    /*#__PURE__*/
    function (_BaseRecord3) {
      (0, _inherits2.default)(MockRecord2, _BaseRecord3);

      function MockRecord2() {
        (0, _classCallCheck2.default)(this, MockRecord2);
        return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(MockRecord2).apply(this, arguments));
      }

      return MockRecord2;
    }((0, _baseRecord.default)({
      id: '0'
    }));

    it('it caches attributes previously seen',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var attrs, rec1, rec2;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              attrs = (0, _immutable.fromJS)({
                id: '0'
              });
              rec1 = MockRecord1.create(attrs);
              rec2 = MockRecord1.create(attrs);
              expect(rec1 === rec2).to.be.true();

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
    it('creates new records for new attributes',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var rec1, rec2;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              rec1 = MockRecord1.create((0, _immutable.fromJS)({
                id: '0'
              }));
              rec2 = MockRecord1.create((0, _immutable.fromJS)({
                id: '0'
              }));
              expect(rec1 === rec2).to.be.false();

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
    it('caches each class separately',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      var attrs, rec1, rec2;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              attrs = (0, _immutable.fromJS)({
                id: '0'
              });
              rec1 = MockRecord1.create(attrs);
              rec2 = MockRecord2.create(attrs);
              expect(rec1 === rec2).to.be.false();

            case 4:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
  });
  describe('#getErrors', function () {
    var aTestRecord = new TestRecord();
    it('performs validations correctly',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee4() {
      var expected;
      return _regenerator.default.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              expected = {
                title: ['title cannot be blank'],
                nestedValue: {
                  here: ['here cannot be blank']
                }
              };
              expect(aTestRecord.getErrors().equals((0, _immutable.fromJS)(expected))).to.be.true();

            case 2:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
    it('takes an optional object to validate instead of `this`',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee5() {
      var otherRecord, expected;
      return _regenerator.default.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              otherRecord = new TestRecord({
                title: 'a',
                nestedValue: {
                  here: 'b'
                }
              });
              expected = {
                title: ['title cannot be blank'],
                nestedValue: {
                  here: ['here cannot be blank']
                }
              };
              expect(otherRecord.getErrors(aTestRecord).equals((0, _immutable.fromJS)(expected))).to.be.true();

            case 3:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5);
    })));
  });
  describe('#errorsIn', function () {
    it('gets the nested errors',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee6() {
      var record;
      return _regenerator.default.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              record = new TestRecord();
              expect(record.errorsIn(['nestedValue', 'here']).toJS()).to.eql(['here cannot be blank']);

            case 2:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
    it('returns a list by default',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee7() {
      var record;
      return _regenerator.default.wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              record = new TestRecord();
              expect(record.errorsIn(['asdf'])).to.eq((0, _immutable.fromJS)([]));

            case 2:
            case "end":
              return _context7.stop();
          }
        }
      }, _callee7);
    })));
  });
  describe('errorsFor', function () {
    it('gets the error',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee8() {
      var record;
      return _regenerator.default.wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              record = new TestRecord();
              expect(record.errorsFor('title').toJS()).to.eql(['title cannot be blank']);

            case 2:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    })));
  });
  describe('isValid', function () {
    it('is true for empty errors',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee9() {
      var record;
      return _regenerator.default.wrap(function _callee9$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              record = new TestRecord({
                title: 'a',
                nestedValue: {
                  here: 'b'
                }
              });
              expect(record.isValid()).to.be.true();

            case 2:
            case "end":
              return _context9.stop();
          }
        }
      }, _callee9);
    })));
    it('is false when errors are present',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee10() {
      var record;
      return _regenerator.default.wrap(function _callee10$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              record = new TestRecord({
                title: 'a',
                nestedValue: {
                  here: ''
                }
              });
              expect(record.isValid()).to.be.false();

            case 2:
            case "end":
              return _context10.stop();
          }
        }
      }, _callee10);
    })));
    it('is true when nested errors are empty',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee11() {
      var record;
      return _regenerator.default.wrap(function _callee11$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              record = new TestRecord({
                title: 'a',
                nestedValue: {
                  here: 'b'
                }
              });

              record.getErrors = function () {
                return (0, _immutable.fromJS)({
                  title: []
                });
              };

              expect(record.isValid()).to.be.true();

            case 3:
            case "end":
              return _context11.stop();
          }
        }
      }, _callee11);
    })));
  });
  describe('validData', function () {
    it('gets the valid data',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee12() {
      var record;
      return _regenerator.default.wrap(function _callee12$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              record = new TestRecord({
                title: 'a'
              });
              expect(record.validData().toJS()).to.eql({
                title: 'a'
              });

            case 2:
            case "end":
              return _context12.stop();
          }
        }
      }, _callee12);
    })));
  });
});