'use strict';

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactTestRenderer = require('react-test-renderer');

var _reactTestRenderer2 = _interopRequireDefault(_reactTestRenderer);

var _DisplayTable = require('./DisplayTable.component');

var _DisplayTable2 = _interopRequireDefault(_DisplayTable);

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

jest.mock('react-dom');

var columnActions = [{
	label: 'favorite',
	icon: 'talend-star',
	className: 'favorite',
	onClick: jest.fn()
}, {
	label: 'certify',
	icon: 'talend-badge',
	className: 'certify',
	onClick: jest.fn()
}];

var items = [{
	id: 0,
	name: 'Hello world',
	created: '2016-09-22',
	modified: '2016-09-22',
	author: 'Jean-Pierre DUPONT',
	actions: [{
		label: 'edit',
		icon: 'fa fa-edit',
		onClick: jest.fn()
	}, {
		label: 'delete',
		icon: 'fa fa-trash-o',
		onClick: jest.fn()
	}],
	icon: 'fa fa-file-excel-o',
	className: 'item-0-class'
}, {
	id: 1,
	name: 'Foo',
	created: '2016-09-22',
	modified: '2016-09-22',
	author: 'Jean-Pierre DUPONT',
	icon: 'fa fa-file-pdf-o',
	className: 'item-1-class'
}, {
	id: 2,
	name: 'Bar',
	created: '2016-09-22',
	modified: '2016-09-22',
	author: 'Jean-Pierre DUPONT'
}];

var columns = [{ key: 'id', label: 'Id' }, { key: 'name', label: 'Name' }, { key: 'author', label: 'Author' }, { key: 'created', label: 'Created' }, { key: 'modified', label: 'Modified' }];

var sort = {
	field: 'name',
	isDescending: false,
	onChange: jest.fn()
};

function insertActionsColumn(column) {
	// Insert actions columns between 'author' and 'created'
	columns.splice(2, 0, column);
	items[0].columnActions = columnActions;
	return {
		items: items,
		columns: columns
	};
}

describe('DisplayTable', function () {
	it('should render with default title property (name)', function () {
		// given
		var props = {
			items: items,
			columns: columns
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render with defined title property', function () {
		// given
		var props = {
			items: items,
			columns: columns,
			titleProps: {
				key: 'id', // title key defined
				iconKey: 'icon',
				onClick: jest.fn()
			}
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render with id if provided', function () {
		// given
		var props = {
			id: 'table-list',
			items: items,
			columns: columns
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render with item custom className if provided', function () {
		// given
		var props = {
			id: 'table-list',
			items: items,
			columns: columns,
			itemProps: { classNameKey: 'className' }
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render selected list element with defaut \'active\' class', function () {
		// given
		var props = {
			id: 'table-list',
			items: items,
			columns: columns,
			itemProps: { isSelected: function isSelected() {
					return true;
				} }
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render selected list element with custom selectedClass if set', function () {
		// given
		var props = {
			id: 'table-list',
			items: items,
			columns: columns,
			itemProps: { isSelected: function isSelected() {
					return true;
				}, selectedClass: 'something' }
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render column actions', function () {
		// given
		var props = insertActionsColumn({ key: 'columnActions', label: '' });

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render screen reader compatible column actions with invisible header', function () {
		// given
		var props = insertActionsColumn({ key: 'columnActions', label: 'Actions', hideHeader: true });

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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

	it('should render sortable headers', function () {
		// given
		var props = {
			items: items,
			columns: columns,
			sort: sort
		};

		// when
		var wrapper = _reactTestRenderer2.default.create(_react2.default.createElement(_DisplayTable2.default, props)).toJSON();

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