'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 _CellTitle = require('../CellTitle');

var _CellTitle2 = _interopRequireDefault(_CellTitle);

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

var _VirtualizedList2 = _interopRequireDefault(_VirtualizedList);

var _gridrow = require('./gridrow');

var _constants = require('./constants');

var _collection = require('../collection');

var _collection2 = _interopRequireDefault(_collection);

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

describe('gridrow', function () {
	describe('#getCellRenderer', function () {
		it('should return cell renderer from content field props', function () {
			// given
			var cellRenderer = function cellRenderer() {
				return _react2.default.createElement('div', null);
			};
			var field = {
				props: { cellRenderer: cellRenderer }
			};

			// when
			var renderer = (0, _gridrow.getCellRenderer)(field);

			// then
			expect(renderer).toBe(cellRenderer);
		});
	});

	describe('#getId', function () {
		it('should return id from parent props', function () {
			// given
			var parent = {
				props: { id: 'my-id' }
			};

			// when
			var id = (0, _gridrow.getId)(parent);

			// then
			expect(id).toBe('my-id');
		});
	});

	describe('#getColumnData', function () {
		it('should return column data from field, enhanced with id from parent props', function () {
			// given
			var parent = {
				props: { id: 'my-id' }
			};
			var field = {
				props: {
					columnData: { info: 'lol' }
				}
			};

			// when
			var columnData = (0, _gridrow.getColumnData)(parent, field);

			// then
			expect(columnData).toEqual({
				id: 'my-id',
				info: 'lol'
			});
		});
	});

	describe('#getDataKey', function () {
		it('should return data property key from content field props', function () {
			// given
			var field = {
				props: {
					dataKey: 'lol'
				}
			};

			// when
			var dataKey = (0, _gridrow.getDataKey)(field);

			// then
			expect(dataKey).toEqual('lol');
		});
	});

	describe('#getLabel', function () {
		it('should return label from content field props', function () {
			// given
			var field = {
				props: {
					label: 'lol'
				}
			};

			// when
			var label = (0, _gridrow.getLabel)(field);

			// then
			expect(label).toEqual('lol');
		});
	});

	describe('#getRowData', function () {
		it('should return row data from parent props', function () {
			// given
			var parent = {
				props: { collection: _collection2.default }
			};

			// when
			var rowData = (0, _gridrow.getRowData)(parent, 1);

			// then
			expect(rowData).toBe(_collection2.default[1]);
		});
	});

	describe('#getCellData', function () {
		it('should return cellDataGetter result', function () {
			// given
			var expectedResult = { cell: 'data' };
			var cellDataGetter = jest.fn();
			cellDataGetter.mockReturnValueOnce(expectedResult);

			var columnData = { custom: 'lol' };
			var parent = {
				props: {
					id: 'my-item-id',
					collection: _collection2.default
				}
			};

			var field = {
				props: {
					cellDataGetter: cellDataGetter,
					columnData: columnData,
					dataKey: 'name'
				}
			};

			// when
			var cellData = (0, _gridrow.getCellData)(field, parent, 1);

			// then
			expect(cellDataGetter).toBeCalledWith({
				columnData: { id: 'my-item-id', custom: 'lol' },
				dataKey: 'name',
				rowData: _collection2.default[1]
			});
			expect(cellData).toBe(expectedResult);
		});
	});

	describe('#extractSpecialFields', function () {
		it('should extract title and selection content fields', function () {
			// given
			var fields = [_react2.default.createElement(_VirtualizedList2.default.Content, {
				label: 'Id',
				dataKey: 'id',
				width: 50
			}), _react2.default.createElement(_VirtualizedList2.default.Content, _extends({
				label: 'Name',
				dataKey: 'name',
				width: 350
			}, _CellTitle2.default)), _react2.default.createElement(_VirtualizedList2.default.Content, {
				label: '',
				dataKey: 'actions',
				width: 120
			}), _react2.default.createElement(_VirtualizedList2.default.Content, {
				id: _constants.internalIds.rowSelector,
				label: '',
				dataKey: '',
				width: 50
			})];
			var parent = {
				props: { children: fields }
			};

			// when
			var result = (0, _gridrow.extractSpecialFields)(parent);

			// then
			var children = _react2.default.Children.toArray(fields);
			expect(result).toEqual({
				titleField: children[1],
				selectionField: children[3],
				otherFields: [children[0], children[2]]
			});
		});
	});

	describe('#renderCell', function () {
		it('should render content field', function () {
			// given
			/* eslint-disable react/prop-types */
			function cellRenderer(props) {
				return _react2.default.createElement(
					'div',
					null,
					'cellData: ',
					props.cellData,
					'columnData: ',
					JSON.stringify(props.columnData),
					'dataKey: ',
					props.dataKey,
					'rowData: ',
					JSON.stringify(props.rowData),
					'rowIndex: ',
					props.rowIndex
				);
			}
			/* eslint-enable react/prop-types */
			var field = _react2.default.createElement(_VirtualizedList2.default.Content, {
				label: 'Id',
				dataKey: 'id',
				width: 50,
				cellRenderer: cellRenderer,
				columnData: { custom: 'lol' }
			});
			var parent = {
				props: { id: 'my-id', collection: _collection2.default }
			};

			// when
			var result = (0, _gridrow.renderCell)(1, parent, field);

			// then
			expect(result).toMatchSnapshot();
		});
	});
});