import './textfield.js';

describe('Component vnTextfield', () => {
    let $scope;
    let $attrs;
    let $timeout;
    let $element;
    let controller;

    beforeEach(ngModule('vnCore'));

    beforeEach(angular.mock.inject(($componentController, $rootScope) => {
        $scope = $rootScope.$new();
        $attrs = {};
        $element = angular.element('<div><input><div class="leftIcons"><div class="rightIcons"></div></div>');
        controller = $componentController('vnTextfield', {$scope, $element, $attrs, $timeout, $transclude: () => {}});
    }));

    describe('saveOldValue()', () => {
        it(`should equal oldValue property to the actual input value`, () => {
            controller.value = 'pepino';
            controller.saveOldValue();

            expect(controller.oldValue).toEqual('pepino');
        });
    });

    describe('value() setter', () => {
        it(`should set _value, input.value and hasValue properties to null, '' and false`, () => {
            let testValue = '';
            controller.value = testValue;

            expect(controller._value).toEqual(null);
            expect(controller.input.value).toEqual(testValue);
            expect(controller.hasValue).toEqual(Boolean(testValue));
        });

        it(`should set _value, input.value and hasValue propertiest to test, test and true`, () => {
            let testValue = 'test';
            controller.value = testValue;

            expect(controller._value).toEqual(testValue);
            expect(controller.input.value).toEqual(testValue);
            expect(controller.hasValue).toEqual(Boolean(testValue));
        });

        it(`should add the class not-empty to the div`, () => {
            let testValue = 'test';
            controller.value = testValue;

            expect(controller.element.classList).toContain('not-empty');
        });
    });

    describe('type() setter', () => {
        it(`should set _type to 'text' if theres not given value`, () => {
            controller.type = null;

            expect(controller._type).toEqual('text');
        });
    });

    describe('vnTabIndex() setter', () => {
        it(`should set input.tabindex to a given value`, () => {
            controller.vnTabIndex = 9;

            expect(controller.input.tabindex).toEqual(9);
        });
    });

    describe('clear()', () => {
        it(`should set value property to null, call focus and saveOldValue`, () => {
            spyOn(controller.input, 'focus');
            spyOn(controller, 'saveOldValue');
            controller.clear();

            expect(controller.value).toEqual(null);
            expect(controller.saveOldValue).toHaveBeenCalledWith();
            expect(controller.input.focus).toHaveBeenCalledWith();
        });
    });
});