"use strict";

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

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

var _Store2 = _interopRequireDefault(require("./Store"));

// jshint esversion: 6
// helpers
var checkAsync = function checkAsync(done, fn) {
  try {
    fn();
    done();
  } catch (e) {
    done();
  }
};

var checkUsers = function checkUsers(list) {
  list = list.map(function (item) {
    return item.last;
  }).sort();

  for (var _len = arguments.length, users = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    users[_key - 1] = arguments[_key];
  }

  expect(list).toEqual(users.sort());
};

var checkUsersAsync = function checkUsersAsync(done, list) {
  for (var _len2 = arguments.length, users = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
    users[_key2 - 2] = arguments[_key2];
  }

  checkAsync(done, function () {
    checkUsers.apply(void 0, [list].concat(users));
  });
};

var UsersStore =
/*#__PURE__*/
function (_Store) {
  (0, _inheritsLoose2.default)(UsersStore, _Store);

  function UsersStore() {
    return _Store.apply(this, arguments) || this;
  }

  return UsersStore;
}(_Store2.default);

var getUsersStore = function getUsersStore(props) {
  if (props === void 0) {
    props = {};
  }

  var predefinedData = [{
    id: 'user1',
    first: 'James',
    last: 'Alpha',
    age: 54,
    email: 'silence@yandex.ru',
    phone: '(800) 478-5822'
  }, {
    id: 'user2',
    first: 'Stephan',
    last: 'Bravo',
    age: 37,
    email: 'bear@gmail.com',
    phone: '1-866-800-2102'
  }, {
    id: 'user3',
    first: 'David',
    last: 'Charlie',
    age: 14,
    email: 'drdavid@mail.ru',
    phone: '18004002102'
  }, {
    id: 'user4',
    first: 'Mike',
    last: 'Delta',
    age: 29,
    email: 'fish@gmail.com',
    phone: '1 (800) 544-2102'
  }];
  var fields = [{
    id: 'id',
    type: 'string',
    isPrimaryKey: true
  }, {
    id: 'first',
    type: 'string'
  }, {
    id: 'last',
    type: 'string'
  }, {
    id: 'age',
    type: 'number',
    comparator: function comparator(a, b) {
      return b - a;
    }
  }, {
    id: 'email',
    type: 'string',
    fieldFilterHandler: function fieldFilterHandler(value, cb) {
      return cb(/@(.+)\./.exec(value)[1]);
    }
  }, {
    id: 'phone',
    type: 'string',
    textFilterHandler: function textFilterHandler(value, cb) {
      return cb(value.replace(/[-)(\s]/g, ''));
    }
  }, {
    id: 'underage',
    type: 'boolean',
    getter: function getter(item) {
      return item.age < 18;
    }
  }];
  var store = new UsersStore(Object.assign({}, props, {
    fields: fields,
    key: 'UserStore'
  }));
  store.setItems(predefinedData);
  return store;
};

describe('Store', function () {
  it('custom getter', function () {
    var store = getUsersStore();
    var underageUsers = store.getData().filter(function (item) {
      return item.underage;
    });
    checkUsers(underageUsers, 'Charlie');
  });
  it('paging', function (done) {
    var store = getUsersStore();
    store.setInitialSorting('last', 'ASC');
    store.setPageSize(2);
    store.onChange(function (data) {
      return checkUsersAsync(done, data, 'Charlie', 'Delta');
    });
    store.setCurrentPage(2);
  });
  describe('sorting', function () {
    it('by ASC', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkAsync(done, function () {
          return expect(data).to.be.ascendingBy('first');
        });
      });
      store.sortBy('first', 'ASC');
    });
    it('by DESC', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkAsync(done, function () {
          return expect(data).to.be.descendingBy('first');
        });
      });
      store.sortBy('first', 'DESC');
    });
    it('ascending order by default', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkAsync(done, function () {
          return expect(data).to.be.ascendingBy('first');
        });
      });
      store.sortBy('first');
    });
    it('with custom comparator', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkAsync(done, function () {
          return expect(data).to.be.ascendingBy('age');
        });
      });
      store.sortBy('age', 'DESC');
    });
  });
  describe('filtering', function () {
    it('by text', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkUsersAsync(done, data, 'Alpha', 'Bravo');
      });
      store.filterByText('pha');
    });
    it('by text with custom text filter handler', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkUsersAsync(done, data, 'Alpha', 'Charlie');
      });
      store.filterByText('8004');
    });
    it('by field', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkUsersAsync(done, data, 'Alpha');
      });
      store.filterByField('first', 'James');
    });
    it('by field with custom field filter handler', function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkUsersAsync(done, data, 'Bravo', 'Delta');
      });
      store.filterByField('email', 'gmail');
    });
    it("by field with action ('in')", function (done) {
      var store = getUsersStore();
      store.onChange(function (data) {
        return checkUsersAsync(done, data, 'Alpha', 'Bravo');
      });
      store.filterByField('last', 'in', ['Alpha', 'Echo', 'Bravo']);
    });
  });
  describe('cloning', function () {
    it('should keep inheritance chain', function () {
      var store = getUsersStore();
      var clonedStore = store.clone();
      expect(clonedStore instanceof UsersStore).toBeTruthy();
    });
    it('should provide a new store with the same data as source', function (done) {
      var store = new _Store2.default({
        fields: {
          id: {
            type: Number,
            primaryKey: true
          },
          name: String
        }
      });
      store.setItems([{
        id: 1,
        name: 'Beta'
      }, {
        id: 2,
        name: 'Gamma'
      }, {
        id: 3,
        name: 'Alpha'
      }]);
      store.filterByField('name', 'in', ['Alpha', 'Gamma']);
      store.sortBy('name', 'ASC'); // TODO UIA-59320 Unable to use sinon.useFakeTimers to test store

      setTimeout(function () {
        var clonedStore = store.clone(); // clone after filtering/sorting applied

        var data = clonedStore.getData();
        expect(data.length).toBe(2);
        expect(data[0].name).toBe('Alpha');
        expect(data[1].name).toBe('Gamma');
        done();
      }, 1000);
    });
    it('should not affects the listeners of cloned store', function (done) {
      var store = new _Store2.default({
        fields: {
          id: {
            type: Number,
            primaryKey: true
          },
          name: String
        }
      });
      store.setItems([{
        id: 1,
        name: 'Beta'
      }, {
        id: 2,
        name: 'Gamma'
      }, {
        id: 3,
        name: 'Alpha'
      }]);
      var callbackForSource = jest.fn();
      var callbackForCloned = jest.fn();
      store.onChange(callbackForSource);
      var clonedStore = store.clone();
      clonedStore.onChange(callbackForCloned);
      clonedStore.sortBy('name', 'ASC'); // TODO UIA-59320 Unable to use sinon.useFakeTimers to test store

      setTimeout(function () {
        expect(callbackForSource).not.toHaveBeenCalled();
        expect(callbackForCloned).toHaveBeenCalled();
        done();
      }, 1000);
    });
  });
});