'use strict';

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _enzyme = require('enzyme');

var _ = require('..');

var _2 = _interopRequireDefault(_);

var _input = require('../../input');

var _input2 = _interopRequireDefault(_input);

var _button = require('../../button');

var _button2 = _interopRequireDefault(_button);

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

describe('Form.Ref', function () {
    var Test = function Test(_ref) {
        var onRef = _ref.onRef,
            show = _ref.show;

        var _Form$useForm = _2['default'].useForm(),
            _Form$useForm2 = (0, _slicedToArray3['default'])(_Form$useForm, 1),
            form = _Form$useForm2[0];

        var removeRef = _react2['default'].useRef();
        var testRef = _react2['default'].useRef();
        var listRef = _react2['default'].useRef();
        return _react2['default'].createElement(
            _2['default'],
            { form: form, initialValues: { list: ['light'] } },
            show && _react2['default'].createElement(
                _2['default'].Item,
                { name: 'remove', label: 'remove' },
                _react2['default'].createElement(_input2['default'], { ref: removeRef })
            ),
            _react2['default'].createElement(
                _2['default'].Item,
                { name: 'test', label: 'test' },
                _react2['default'].createElement(_input2['default'], { ref: testRef })
            ),
            _react2['default'].createElement(
                _2['default'].List,
                { name: 'list' },
                function (fields) {
                    return fields.map(function (field) {
                        return _react2['default'].createElement(
                            _2['default'].Item,
                            field,
                            _react2['default'].createElement(_input2['default'], { ref: listRef })
                        );
                    });
                }
            ),
            _react2['default'].createElement(
                _button2['default'],
                { className: 'ref-item', onClick: function onClick() {
                        onRef(form.getFieldInstance('test'), testRef.current);
                    } },
                'Form.Item'
            ),
            _react2['default'].createElement(
                _button2['default'],
                { className: 'ref-list', onClick: function onClick() {
                        onRef(form.getFieldInstance(['list', 0]), listRef.current);
                    } },
                'Form.List'
            ),
            _react2['default'].createElement(
                _button2['default'],
                { className: 'ref-remove', onClick: function onClick() {
                        onRef(form.getFieldInstance('remove'), removeRef.current);
                    } },
                'Removed'
            )
        );
    };
    it('should ref work', function () {
        var onRef = jest.fn();
        var wrapper = (0, _enzyme.mount)(_react2['default'].createElement(Test, { onRef: onRef, show: true }));
        wrapper.find('.ref-item').last().simulate('click');
        expect(onRef).toHaveBeenCalled();
        expect(onRef.mock.calls[0][0]).toBe(onRef.mock.calls[0][1]);
        onRef.mockReset();
        wrapper.find('.ref-list').last().simulate('click');
        expect(onRef).toHaveBeenCalled();
        expect(onRef.mock.calls[0][0]).toBe(onRef.mock.calls[0][1]);
        onRef.mockReset();
        wrapper.setProps({ show: false });
        wrapper.update();
        wrapper.find('.ref-remove').last().simulate('click');
        expect(onRef).toHaveBeenCalledWith(undefined, null);
    });
}); /* eslint-disable react/jsx-key */