import './tooltip';

describe('Component vnTooltip', () => {
    let $element;
    let tooltip;
    let tooltipCtrl;
    let $parent;
    let window;

    beforeEach(ngModule('vnCore'));

    beforeEach(inject(($rootScope, $compile, $document) => {
        let scope = $rootScope.$new();
        $element = $compile(`<vn-tooltip class="text">test</span></vn-tooltip>`)(scope);
        $document.find('body').append($element);
        tooltip = $element[0];
        tooltipCtrl = tooltip.$ctrl;
        window = tooltipCtrl.window;
        $parent = angular.element('<div/>');

        $document.find('body').append($parent);

        $parent[0].getBoundingClientRect = () => {};

        jest.spyOn(tooltip, 'getBoundingClientRect').mockReturnValue({bottom: 0, height: 40, left: 0, right: 0, top: 0, width: 40});
    }));

    describe('show()', () => {
        it(`should check that tooltip is visible into the screen`, () => {
            jest.spyOn($parent[0], 'getBoundingClientRect').mockReturnValue({
                height: window.innerHeight - 120,
                width: window.innerWidth - 120,
                top: 60,
                left: 60,
            });

            expect(tooltip.classList).not.toContain('show');

            tooltipCtrl.show($parent[0]);

            let tooltipStyle = tooltip.style;

            let tooltipTop = parseInt(tooltipStyle['top']);
            let tooltipLeft = parseInt(tooltipStyle['left']);

            expect(tooltip.classList).toContain('show');

            expect(tooltipTop).toBeLessThanOrEqual(window.innerHeight);
            expect(tooltipTop).toBeGreaterThanOrEqual(0);
            expect(tooltipLeft).toBeLessThanOrEqual(window.innerWidth);
            expect(tooltipLeft).toBeGreaterThanOrEqual(0);
        });
    });

    describe('hide()', () => {
        it('should check that tooltip is not visible', () => {
            jest.spyOn($parent[0], 'getBoundingClientRect').mockReturnValue({
                height: window.innerHeight,
                width: window.innerWidth - 80,
                top: 0,
                left: 60,
            });
            tooltipCtrl.show($parent[0]);

            expect(tooltip.classList).toContain('show');
            tooltipCtrl.hide();

            expect(tooltip.classList).not.toContain('show');
        });
    });

    describe('relocate()', () => {
        it('should reallocate tooltip to the left', () => {
            jest.spyOn($parent[0], 'getBoundingClientRect').mockReturnValue({
                height: window.innerHeight,
                width: window.innerWidth - 80,
                top: 0,
                left: 60,
            });

            tooltipCtrl.position = 'left';
            tooltipCtrl.show($parent[0]);

            let tooltipStyle = tooltip.style;

            let tooltipTop = parseInt(tooltipStyle['top']);
            let tooltipLeft = parseInt(tooltipStyle['left']);

            expect(tooltipTop).toEqual((window.innerHeight / 2) - (tooltipCtrl.margin * 2));
            expect(tooltipLeft).toEqual(tooltipCtrl.margin);
        });

        it('should reallocate tooltip on bottom', () => {
            let parentHeight = window.innerHeight - 80;
            jest.spyOn($parent[0], 'getBoundingClientRect').mockReturnValue({
                height: parentHeight,
                width: window.innerWidth,
                top: 0,
                left: 0,
            });

            tooltipCtrl.position = 'bottom';
            tooltipCtrl.show($parent[0]);

            let tooltipStyle = tooltip.style;

            let tooltipTop = parseInt(tooltipStyle['top']);
            let tooltipLeft = parseInt(tooltipStyle['left']);

            expect(tooltipLeft).toEqual((window.innerWidth / 2) - (tooltipCtrl.margin * 2));
            expect(tooltipTop).toEqual(parentHeight + tooltipCtrl.margin);
        });

        it(`should reallocate tooltip on right`, () => {
            let parentWidth = window.innerWidth - 80;
            jest.spyOn($parent[0], 'getBoundingClientRect').mockReturnValue({
                height: window.innerHeight,
                width: parentWidth,
                top: 0,
                left: 0,
            });

            tooltipCtrl.position = 'right';
            tooltipCtrl.show($parent[0]);

            let tooltipStyle = tooltip.style;

            let tooltipTop = parseInt(tooltipStyle['top']);
            let tooltipLeft = parseInt(tooltipStyle['left']);

            expect(tooltipLeft).toEqual(parentWidth + tooltipCtrl.margin);
            expect(tooltipTop).toEqual((window.innerHeight / 2) - (tooltipCtrl.margin * 2));
        });

        it(`should reallocate tooltip on top`, () => {
            jest.spyOn($parent[0], 'getBoundingClientRect').mockReturnValue({
                height: window.innerHeight - 80,
                width: window.innerWidth,
                top: 60,
                left: 0,
            });

            tooltipCtrl.position = 'top';
            tooltipCtrl.show($parent[0]);

            let tooltipStyle = tooltip.style;

            let tooltipTop = parseInt(tooltipStyle['top']);
            let tooltipLeft = parseInt(tooltipStyle['left']);

            expect(tooltipLeft).toEqual((window.innerWidth / 2) - (tooltipCtrl.margin * 2));
            expect(tooltipTop).toEqual(tooltipCtrl.margin);
        });
    });
});