// jshint esversion: 6
'use strict';

import React from 'react';
import {mount} from '../../../build/enzyme';

import {InputCounter} from './InputCounter';
import {InputCounterControl} from './InputCounterControl';

describe('InputCounter', () => {
    const opts = {
        minValue: 7,
        maxValue: 51
    };

    it('provide minValue in case of empty-string input', () => {
        const instance = mount(<InputCounterControl {...opts} />).instance();
        expect(instance.getValidatedValue('')).toBe(opts.minValue);
    });

    it('check value for lower boundary', () => {
        const instance = mount(<InputCounterControl {...opts} />).instance();
        expect(instance.getValidatedValue(-Infinity)).toBe(opts.minValue);
    });

    it('check value for upper boundary', () => {
        const instance = mount(<InputCounterControl {...opts} />).instance();
        expect(instance.getValidatedValue(Infinity)).toBe(opts.maxValue);
    });

    describe('ui', () => {
        it('check value for upper boundary', () => {
            const wrapper = mount(<InputCounter value={opts.maxValue} {...opts} />);
            expect(wrapper.findByTestAutomation('incrementButton').get(0).props.disabled).toBe(
                true
            );
        });

        it('check value for lower boundary', () => {
            const wrapper = mount(<InputCounter value={opts.minValue} {...opts} />);
            expect(wrapper.findByTestAutomation('decrementButton').get(0).props.disabled).toBe(
                true
            );
        });

        it('increase value by one', done => {
            const value = 9;
            const _opts = {
                ...opts,
                value,
                onChange(v) {
                    expect(v).toBe(value + 1);
                    done();
                }
            };
            const wrapper = mount(<InputCounter {..._opts} />);
            wrapper
                .findByTestAutomation('incrementButton')
                .hostNodes()
                .simulate('click');
        });

        it('decrease value by one', done => {
            const value = 22;
            const _opts = {
                ...opts,
                value,
                onChange(v) {
                    expect(v).toBe(value - 1);
                    done();
                }
            };
            const wrapper = mount(<InputCounter {..._opts} />);
            wrapper
                .findByTestAutomation('decrementButton')
                .hostNodes()
                .simulate('click');
        });
    });
});
