"use strict";

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

var _enzyme = _interopRequireWildcard(require("enzyme"));

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

var _enzymeAdapterReact = _interopRequireDefault(require("enzyme-adapter-react-16"));

var _BlankSlate = require("../../BlankSlate");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

_enzyme.default.configure({
  adapter: new _enzymeAdapterReact.default()
});

describe('List View', function () {
  it('should render items as a list view', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [{
        name: 'CJ'
      }, {
        name: 'Affiliate'
      }],
      renderContent: function renderContent(item) {
        return _react.default.createElement("div", null, item.name);
      }
    }));
    var texts = wrapper.find('div.vs-list-item').map(function (element) {
      return element.text();
    });
    expect(texts).toEqual(['CJ', 'Affiliate']);
  });
  it('should not render items with clickable style when there is no onClick handler', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [{
        name: 'CJ'
      }, {
        name: 'Affiliate'
      }],
      renderContent: function renderContent(item) {
        return _react.default.createElement("div", null, item.name);
      }
    }));
    expect(wrapper.find('div.vs-list-item-clickable')).toHaveLength(0);
  });
  it('should render clickable list items when there is an onClick handler', function () {
    var onClick = jest.fn();
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [{
        name: 'CJ'
      }, {
        name: 'Affiliate'
      }],
      renderContent: function renderContent(item) {
        return _react.default.createElement("div", null, item.name);
      },
      onClick: onClick
    }));
    expect(wrapper.find('div.vs-list-item-clickable')).toHaveLength(2);
  });
  it('should call onClick when I click on a list item', function () {
    var onClick = jest.fn();
    var item = {
      name: 'CJ'
    };
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [item],
      renderContent: function renderContent(item) {
        return _react.default.createElement("div", null, item.name);
      },
      onClick: onClick
    }));
    wrapper.find('div.vs-list-item-clickable').simulate('click');
    expect(onClick.mock.calls).toEqual([[item]]);
  });
  it('should not break if I click on a list item without onClick handler', function () {
    var item = {
      name: 'CJ'
    };
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [item],
      renderContent: function renderContent(item) {
        return _react.default.createElement("div", null, item.name);
      }
    }));
    wrapper.find('div.vs-list-item').simulate('click');
  });
  it('should render header if there is a header render props', function () {
    var item = {
      name: 'CJ'
    };
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [item],
      renderContent: function renderContent() {
        return null;
      },
      renderHeader: function renderHeader() {
        return _react.default.createElement("div", null, "header");
      },
      renderFooter: function renderFooter() {
        return _react.default.createElement("div", null, "footer");
      }
    }));
    expect(wrapper.find('div.vs-list-view-header').text()).toEqual('header');
    expect(wrapper.find('div.vs-list-view-footer').text()).toEqual('footer');
  });
  it('should render spinner', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      isLoading: true,
      renderContent: function renderContent() {
        return null;
      },
      renderHeader: function renderHeader() {
        return _react.default.createElement("div", null, "header");
      },
      renderFooter: function renderFooter() {
        return _react.default.createElement("div", null, "footer");
      }
    }));
    expect(wrapper.find('div.vs-list-item')).toHaveLength(0);
    expect(wrapper.find('i.fa-spinner')).toHaveLength(1);
  });
  it('should render a BlankSlate when data is empty', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [],
      renderFooter: function renderFooter() {
        return _react.default.createElement("div", null, "footer");
      }
    }));
    expect(wrapper.find(_BlankSlate.BlankSlate)).toHaveLength(1);
  });
  it('should not render footer when data is empty', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [],
      renderFooter: function renderFooter() {
        return _react.default.createElement("div", null, "footer");
      }
    }));
    expect(wrapper.find('div.vs-list-view-footer')).toHaveLength(0);
  });
  it('should render custom empty state', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [],
      isLoading: true,
      renderContent: function renderContent() {
        return null;
      },
      renderHeader: function renderHeader() {
        return _react.default.createElement("div", null, "header");
      },
      renderFooter: function renderFooter() {
        return _react.default.createElement("div", null, "footer");
      },
      renderEmptyState: function renderEmptyState() {
        return _react.default.createElement("div", {
          className: "emptyState"
        });
      }
    }));
    expect(wrapper.find('div.emptyState')).toHaveLength(1);
  });
  it('should render arbitrary data-id prop', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_.default, {
      data: [{
        name: 'CJ'
      }, {
        name: 'Affiliate'
      }],
      renderContent: function renderContent(item) {
        return _react.default.createElement("div", null, item.name);
      },
      "data-id": "test"
    }));
    var texts = wrapper.find('div.vs-list-view-container[data-id="test"]').find('div.vs-list-item').map(function (element) {
      return element.text();
    });
    expect(texts).toEqual(['CJ', 'Affiliate']);
  });
});