'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _enzyme = require('enzyme');

var _VirtualizedList = require('../../VirtualizedList');

var _VirtualizedList2 = _interopRequireDefault(_VirtualizedList);

var _ListToVirtualizedList = require('./ListToVirtualizedList.component');

var _ListToVirtualizedList2 = _interopRequireDefault(_ListToVirtualizedList);

var _CellActions = require('../../VirtualizedList/CellActions');

var _CellActions2 = _interopRequireDefault(_CellActions);

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

var props = {
	id: 'mylistid',
	items: [{ id: 3, label: 'my item', myactions: [{ foo: 'bar' }] }],
	columns: [{ key: 'id', label: 'Id' }, { key: 'label', label: 'Label' }, { key: 'myactions', label: 'Actions' }],
	titleProps: {
		key: 'label',
		extra: 'Extra'
	}
};

describe('ListToVirtualizedList', function () {
	it('should map props', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, props));
		expect(wrapper.props().collection).toBe(props.items);
		expect(wrapper.props().type).toBe('TABLE');
		var columns = wrapper.find(_VirtualizedList2.default.Content);
		expect(columns.length).toBe(3);
		columns.forEach(function (element) {
			var eProps = element.props();
			if (eProps.label === 'Id') {
				expect(eProps.dataKey).toBe('id');
			} else if (eProps.label === 'Label') {
				expect(eProps.dataKey).toBe('label');
				expect(eProps.columnData.extra).toBe('Extra');
			} else if (eProps.label === 'Actions') {
				expect(eProps.dataKey).toBe('myactions');
			} else {
				expect(false).toBe(true);
			}
		});
	});

	it('should support displayMode', function () {
		var table = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, _extends({}, props, { displayMode: 'table' }))).props();
		expect(table.type).toBe('TABLE');

		var large = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, _extends({}, props, { displayMode: 'large' }))).props();
		expect(large.type).toBe('LARGE');
	});

	it('should add actionsKey to titleProps', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, props));
		wrapper.find(_VirtualizedList2.default.Content).forEach(function (element) {
			var eProps = element.props();
			if (eProps.columnData) {
				expect(eProps.columnData.actionsKey).toBe('actions');
			}
		});
	});

	it('should find supposedActions based on items', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, props));
		wrapper.find(_VirtualizedList2.default.Content).forEach(function (element) {
			var eProps = element.props();
			if (eProps.label === 'Actions') {
				expect(eProps.cellRenderer).toBe(_CellActions2.default.cellRenderer);
			}
		});
	});

	it('should adapt sort info', function () {
		// when
		var ascVirtualizedProps = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, _extends({}, props, {
			sort: { field: 'name', isDescending: false }
		}))).props();
		var descVirtualizedProps = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, _extends({}, props, {
			sort: { field: 'name', isDescending: true }
		}))).props();

		// then
		expect(ascVirtualizedProps.sortBy).toBe('name');
		expect(ascVirtualizedProps.sortDirection).toBe(_VirtualizedList.SORT_BY.ASC);
		expect(descVirtualizedProps.sortDirection).toBe(_VirtualizedList.SORT_BY.DESC);
	});

	it('should adapt sort onChange', function () {
		// given
		var onChange = jest.fn();
		var virtualizedProps = (0, _enzyme.shallow)(_react2.default.createElement(_ListToVirtualizedList2.default, _extends({}, props, {
			sort: { field: 'name', isDescending: false, onChange: onChange }
		}))).props();

		// when
		virtualizedProps.sort({ sortBy: 'name', sortDirection: _VirtualizedList.SORT_BY.DESC });

		// then
		expect(onChange).toBeCalledWith(null, { field: 'name', isDescending: true });
	});
});