'use strict';

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _enzyme = require('enzyme');

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

var _ItemTitle = require('./ItemTitle.component');

var _ItemTitle2 = _interopRequireDefault(_ItemTitle);

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

var item = {
	id: 1,
	name: 'Hello world',
	created: '2016-09-22',
	modified: '2016-09-22',
	author: 'Jean-Pierre DUPONT',
	icon: 'fa fa-file-excel-o',
	displayMode: 'input'
};

describe('ItemTitle', function () {
	it('should render text title', function () {
		// given
		var props = {
			id: 'title',
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: undefined, // no display mode in item
				onClick: undefined // no click callback
			}
		};

		// when
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ItemTitle2.default, props));

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

	it('should render button title', function () {
		// given
		var props = {
			id: 'title',
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: undefined, // no display mode in item
				onClick: jest.fn() // provided click callback
			}
		};

		// when
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ItemTitle2.default, props));

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

	it('should render input title', function () {
		// given
		var props = {
			id: 'title',
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: 'displayMode' // item.displayMode is the provided display mode
			}
		};

		// when
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ItemTitle2.default, props));

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

	it('should render icon', function () {
		// given
		var props = {
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				iconKey: 'icon' // item.icon is the icon name
			}
		};

		// when
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ItemTitle2.default, props));

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

	it('should render text with undefined', function () {
		var props = {
			id: 'title',
			className: 'my-title',
			item: {},
			titleProps: {
				key: 'name',
				displayModeKey: undefined, // no display mode in item
				onClick: undefined // no click callback
			}
		};
		var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_ItemTitle2.default, props));
		expect(wrapper.node).toMatchSnapshot();
	});

	it('should trigger callback on button title click (left click)', function () {
		// given
		var props = {
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: undefined, // no display mode in item, default 'text'
				onClick: jest.fn() // provided click callback
			}
		};
		var titleInstance = _react2.default.createElement(_ItemTitle2.default, props);

		// when
		var wrapper = (0, _enzyme.mount)(titleInstance);
		wrapper.find(_reactBootstrap.Button).simulate('click', { button: 0, stopPropagation: function stopPropagation() {} });

		// then
		expect(props.titleProps.onClick).toBeCalledWith(expect.anything(), item);
	});

	it('should trigger callback on button title mousedown (middle-click)', function () {
		// given
		var props = {
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: undefined, // no display mode in item, default 'text'
				onClick: jest.fn() // provided click callback
			}
		};
		var titleInstance = _react2.default.createElement(_ItemTitle2.default, props);

		// when
		var wrapper = (0, _enzyme.mount)(titleInstance);
		wrapper.find(_reactBootstrap.Button).simulate('mouseDown', { button: 1, stopPropagation: function stopPropagation() {} });

		// then
		expect(props.titleProps.onClick).toBeCalledWith(expect.anything(), item);
	});

	it('should trigger callback on input title blur', function () {
		// given
		var props = {
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: 'displayMode',
				onEditSubmit: jest.fn(),
				onEditCancel: jest.fn()
			}
		};
		var titleInstance = _react2.default.createElement(_ItemTitle2.default, props);

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

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

	it('should trigger callback on input title ENTER', function () {
		// given
		var props = {
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: 'displayMode',
				onEditSubmit: jest.fn(),
				onEditCancel: jest.fn()
			}
		};
		var titleInstance = _react2.default.createElement(_ItemTitle2.default, props);

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

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

	it('should trigger callback on input title ESC', function () {
		// given
		var props = {
			className: 'my-title',
			item: item,
			titleProps: {
				key: 'name',
				displayModeKey: 'displayMode',
				onEditSubmit: jest.fn(),
				onEditCancel: jest.fn()
			}
		};
		var titleInstance = _react2.default.createElement(_ItemTitle2.default, props);

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

		// then
		expect(props.titleProps.onEditCancel).toBeCalled();
		var callArgs = props.titleProps.onEditCancel.mock.calls[0];
		expect(callArgs[1]).toBe(item);
	});
});