describe('Component vnField', () => {
    let controller;
    let $element;

    beforeEach(ngModule('vnCore'));

    beforeEach(inject(($compile, $rootScope) => {
        $element = $compile(`<vn-textfield></vn-textfield>`)($rootScope);
        controller = $element.controller('vnTextfield');
    }));

    afterEach(() => {
        $element.remove();
    });

    describe('field get/set', () => {
        it('should do nothing when trying to set the same value again', () => {
            controller.field = '';
            jest.spyOn(controller, 'validateValue');

            controller.field = '';

            expect(controller.validateValue).toHaveBeenCalledTimes(0);
            expect(controller.classList).not.toContain('not-empty');
        });

        it('should add the class no empty and then call validateValue()', () => {
            controller.field = '';
            jest.spyOn(controller, 'validateValue');

            controller.field = 'someField';

            expect(controller.validateValue).toHaveBeenCalledTimes(1);
            expect(controller.classList).toContain('not-empty');
        });
    });

    describe('refreshHint()', () => {
        it('should add the class invalid if there is an error in the controller', () => {
            controller._error = true;

            controller.refreshHint();

            expect(controller.classList).toContain('invalid');
        });
    });

    describe('onFocus()', () => {
        it('should add the class focus', () => {
            controller.onFocus(true);

            expect(controller.classList).toContain('focused');
        });

        it('should not add the class focus', () => {
            controller.onFocus(false);

            expect(controller.classList).not.toContain('focuses');
        });
    });

    describe('buildInput()', () => {
        it('should build an input based on the received type', () => {
            controller.buildInput('number');

            expect(controller.input.tagName).toEqual('INPUT');
            expect(controller.input.type).toEqual('number');
        });
    });

    describe('validateValue()', () => {
        it('should do nothing if there is no new error to show', () => {
            jest.spyOn(controller, 'refreshHint');
            controller.inputError = 'old validation message';
            controller.buildInput('number');
            controller.input.setCustomValidity('old validation message');

            controller.validateValue();

            expect(controller.refreshHint).not.toHaveBeenCalled();
            expect(controller.inputError).toEqual('old validation message');
        });

        it('should update the input error and call refreshHint', () => {
            jest.spyOn(controller, 'refreshHint');
            controller.inputError = 'OLD validation message';
            controller.buildInput('number');
            controller.input.setCustomValidity('NEW validation message');

            controller.validateValue();

            expect(controller.refreshHint).toHaveBeenCalled();
            expect(controller.inputError).toEqual('NEW validation message');
        });
    });
});