'use strict';

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _enzyme = require('enzyme');

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

var _DisplayTable2 = _interopRequireDefault(_DisplayTable);

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

var items = [{
	id: 1,
	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: 2,
	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' }];

describe('DisplayTable', function () {
	it('should trigger sort on new column', function () {
		// given
		var sort = {
			field: 'name',
			isDescending: false,
			onChange: jest.fn()
		};
		var props = {
			items: items,
			columns: columns,
			sort: sort
		};

		// when
		var list = _react2.default.createElement(_DisplayTable2.default, props);
		var wrapper = (0, _enzyme.mount)(list);
		wrapper.find('th').at(2).find('button').simulate('click');

		// then
		expect(sort.onChange).toBeCalled();
		expect(sort.onChange.mock.calls[0][1]).toEqual({
			field: 'author',
			isDescending: false
		});
	});

	it('should trigger sort on current column with reverse direction', function () {
		// given
		var sort = {
			field: 'name',
			isDescending: true,
			onChange: jest.fn()
		};
		var props = {
			items: items,
			columns: columns,
			sort: sort
		};

		// when
		var list = _react2.default.createElement(_DisplayTable2.default, props);
		var wrapper = (0, _enzyme.mount)(list);
		wrapper.find('th').at(1).find('button').simulate('click');

		// then
		expect(sort.onChange).toBeCalled();
		expect(sort.onChange.mock.calls[0][1]).toEqual({
			field: 'name',
			isDescending: false
		});
	});
});

describe('CellContent', function () {
	it('should render', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.CellContent, {
			column: { key: 'test' },
			item: { test: 'hello' }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
	it('should render undefined', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.CellContent, {
			column: { key: 'test' },
			item: {}
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
	it('should render array', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.CellContent, {
			column: { key: 'test' },
			item: { test: [{ label: 'action' }] }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
	it('should render title', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.CellContent, {
			column: { key: 'test' },
			item: { test: 'hello' },
			isTitle: true,
			id: 'my-id',
			titleProps: { foo: 'bar' }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
});

describe('RowRenderer', function () {
	it('should render', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.RowRenderer, {
			columns: [{ key: 'foo' }, { key: 'baz' }],
			item: { foo: 'bar', baz: 'hello' },
			titleProps: { key: 'foo' }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
});

describe('ColumnHeader', function () {
	it('should render', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.ColumnHeader, {
			column: { key: 'test', label: 'Test' }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
	it('should render an invisible screen reader compatible column header', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.ColumnHeader, {
			column: { key: 'test', label: 'Test', hideHeader: true }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
	it('should render with sort', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.ColumnHeader, {
			column: { key: 'test', label: 'Test' },
			sort: { field: 'test', onChange: jest.fn() }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
});

describe('ListHeaders', function () {
	it('should render', function () {
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_DisplayTable.ListHeaders, {
			columns: [{ item: 'value' }],
			sort: { foo: 'bar' }
		}));
		expect(wrapper.node).toMatchSnapshot();
	});
});