'use strict';

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _enzyme = require('enzyme');

var _reactBootstrap = require('react-bootstrap');

var _ItemEdit = require('./ItemEdit.component');

var _ItemEdit2 = _interopRequireDefault(_ItemEdit);

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

var item = {
	id: 1,
	values: ['toto'],
	index: 0,
	itemProps: {
		key: 'values',
		onSubmitItem: jest.fn(), // provided click callback
		onAbortItem: jest.fn(), // provided click callback
		actions: [{
			label: 'Validate',
			id: 'validate',
			onClick: jest.fn() // provided click callback
		}, {
			label: 'Cancel',
			id: 'cancel',
			onClick: jest.fn() // provided click callback
		}]
	}
};

describe('Item', function () {
	it('should display value with two buttons and trigger callback on button title click', function () {
		// given
		var props = {
			item: item,
			currentEdit: {
				validate: {
					disabled: false
				}
			}
		};
		var itemInstance = _react2.default.createElement(_ItemEdit2.default, props);

		// when
		var wrapper = (0, _enzyme.mount)(itemInstance);
		var buttons = wrapper.find(_reactBootstrap.Button);

		buttons.at(1).simulate('click', { stopPropagation: function stopPropagation() {} });

		// then
		expect(buttons.length).toBe(2);
		expect(props.item.itemProps.actions[1].onClick).toBeCalled();
	});

	it('should trigger callback on input title ENTER', function () {
		// given
		var props = {
			item: item,
			currentEdit: {
				validate: {
					disabled: false
				}
			},
			itemProps: {
				key: 'values',
				onSubmitItem: jest.fn(), // provided click callback
				onAbortItem: jest.fn(), // provided click callback
				actions: [{
					label: 'Validate',
					id: 'validate',
					onClick: jest.fn() // provided click callback
				}, {
					label: 'Cancel',
					id: 'cancel',
					onClick: jest.fn() // provided click callback
				}]
			}
		};

		var itemEditInstance = _react2.default.createElement(_ItemEdit2.default, props);

		// when
		var wrapper = (0, _enzyme.mount)(itemEditInstance);
		wrapper.find('input').simulate('keyDown', { keyCode: 13, target: { value: 'my new title' } });

		// then
		expect(props.item.itemProps.onSubmitItem).toBeCalled();
		var callArgs = props.item.itemProps.onSubmitItem.mock.calls[0];
		expect(callArgs[1]).toEqual({ value: 'my new title', model: props.item, index: 0 });
	});

	it('should trigger callback on input title ESC', function () {
		// given
		var props = {
			item: item,
			currentEdit: {
				validate: {
					disabled: false
				}
			},
			itemProps: {
				key: 'values',
				onSubmitItem: jest.fn(), // provided click callback
				onAbortItem: jest.fn(), // provided click callback
				actions: [{
					label: 'Validate',
					id: 'validate',
					onClick: jest.fn() // provided click callback
				}, {
					label: 'Cancel',
					id: 'cancel',
					onClick: jest.fn() // provided click callback
				}]
			}
		};

		var itemEditInstance = _react2.default.createElement(_ItemEdit2.default, props);

		// when
		var wrapper = (0, _enzyme.mount)(itemEditInstance);
		wrapper.find('input').simulate('keyDown', { keyCode: 27 });

		// then
		expect(props.item.itemProps.onAbortItem).toBeCalled();
		var callArgs = props.item.itemProps.onAbortItem.mock.calls[0];
		expect(callArgs[1]).toEqual({ value: 'toto', model: props.item, index: 0 });
	});
});