'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 _enzyme = require('enzyme');

var _Filter = require('./Filter.component');

var _Filter2 = _interopRequireDefault(_Filter);

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

jest.useFakeTimers();

var defaultProps = {};
beforeEach(function () {
	defaultProps = {
		docked: false,
		onBlur: jest.fn(),
		onFilter: jest.fn(),
		onToggle: jest.fn(),
		ref: 'inputFilter'
	};
});

describe('Filter', function () {
	it('should call onToggle on search icon click', function () {
		// given
		var props = _extends({}, defaultProps, {
			docked: true
		});
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('button').simulate('click');

		// then
		expect(props.onToggle).toBeCalled();
	});

	it('should call onToggle on cross icon click', function () {
		// given
		var props = _extends({}, defaultProps);
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('button').simulate('click');

		// then
		expect(props.onToggle).toBeCalled();
	});

	it('should call onFilter when input value change', function () {
		// given
		var props = _extends({}, defaultProps);
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('input').simulate('change');

		// then
		expect(props.onFilter).toBeCalled();
	});

	it('should call onBlur on input blur', function () {
		// given
		var props = _extends({}, defaultProps);
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('input').simulate('blur');

		// then
		expect(props.onBlur).toBeCalled();
	});

	it('should call onToggle on ESC keydown', function () {
		// given
		var props = _extends({}, defaultProps);
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('input').simulate('keydown', { keyCode: 27 });

		// then
		expect(props.onToggle).toBeCalled();
	});

	it('should call onToggle on ENTER keydown', function () {
		// given
		var props = _extends({}, defaultProps);
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('input').simulate('keydown', { keyCode: 13 });

		// then
		expect(props.onBlur).toBeCalled();
	});

	it('should call onFilter with debounce options', function () {
		// given
		var initialTimeoutCount = setTimeout.mock.calls.length;
		var debounceTimeout = 300;
		var props = _extends({}, defaultProps, {
			debounceTimeout: debounceTimeout
		});
		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('input').simulate('change');

		// then
		expect(setTimeout.mock.calls.length).toBe(initialTimeoutCount + 1);
		expect(setTimeout.mock.calls[0][1]).toBe(debounceTimeout);
	});

	it('should call onFilter with debounceMinLength options', function () {
		// given
		var initialTimeoutCount = setTimeout.mock.calls.length;
		var debounceTimeout = 300;
		var props = _extends({}, defaultProps, {
			debounceTimeout: debounceTimeout,
			debounceMinLength: 3
		});
		var underMinLengthEvent = { target: { value: '2' } };
		var overMinLengthEvent = { target: { value: 'toto' } };

		var filterInstance = (0, _enzyme.mount)(_react2.default.createElement(_Filter2.default, props));

		// when
		filterInstance.find('input').simulate('change', underMinLengthEvent);

		// then
		expect(setTimeout.mock.calls.length).toBe(initialTimeoutCount);

		// when
		filterInstance.find('input').simulate('change', overMinLengthEvent);

		// then
		expect(setTimeout.mock.calls.length).toBe(initialTimeoutCount + 1);
		expect(setTimeout.mock.calls[0][1]).toBe(debounceTimeout);
	});
});