import './index.js';
import crudModel from 'core/mocks/crud-model';

describe('Item', () => {
    describe('Component vnItemDiary', () => {
        let $scope;
        let controller;

        beforeEach(ngModule('item'));

        beforeEach(inject(($componentController, $rootScope) => {
            $scope = $rootScope.$new();
            const $element = angular.element('<vn-item-diary></vn-item-diary>');
            controller = $componentController('vnItemDiary', {$element, $scope});
            controller.$.model = crudModel;
            controller.$params = {id: 1};
        }));

        describe('set item()', () => {
            it('should set warehouseFk property based on itemType warehouseFk', () => {
                jest.spyOn(controller.$, '$applyAsync');
                controller.item = {id: 1, itemType: {warehouseFk: 1}};

                expect(controller.$.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
                $scope.$apply();

                expect(controller.warehouseFk).toEqual(1);
                expect(controller.item.id).toEqual(1);
            });

            it(`should set warehouseFk property based on url query warehouseFk`, () => {
                jest.spyOn(controller.$, '$applyAsync');
                controller.$params.warehouseFk = 4;
                controller.item = {id: 1, itemType: {warehouseFk: 1}};

                expect(controller.$.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
                $scope.$apply();

                expect(controller.warehouseFk).toEqual(4);
                expect(controller.item.id).toEqual(1);
            });
        });

        describe('scrollToLine ()', () => {
            it('should assign $location then call anchorScroll using controller value', () => {
                jest.spyOn(controller, '$anchorScroll');
                controller.lineFk = 1;
                controller.scrollToLine('invalidValue');

                $scope.$apply();

                expect(controller.$location.hash()).toEqual(`vnItemDiary-${1}`);
                expect(controller.$anchorScroll).toHaveBeenCalledWith();
            });

            it('should assign $location then call anchorScroll using received value', () => {
                jest.spyOn(controller, '$anchorScroll');
                controller.lineFk = undefined;
                controller.scrollToLine(1);

                $scope.$apply();

                expect(controller.$location.hash()).toEqual(`vnItemDiary-${1}`);
                expect(controller.$anchorScroll).toHaveBeenCalledWith();
            });
        });

        describe('showDescriptor ()', () => {
            it('should call to the entryDescriptor show() method', () => {
                controller.$.entryDescriptor = {};
                controller.$.entryDescriptor.show = jest.fn();

                const $event = new Event('click');
                const target = document.createElement('div');
                target.dispatchEvent($event);
                const data = {id: 1, origin: 1};
                controller.showDescriptor($event, data);

                expect(controller.$.entryDescriptor.show).toHaveBeenCalledWith($event.target, data.origin);
            });

            it('should call to the ticketDescriptor show() method', () => {
                controller.$.ticketDescriptor = {};
                controller.$.ticketDescriptor.show = jest.fn();

                const $event = new Event('click');
                const target = document.createElement('div');
                target.dispatchEvent($event);
                const data = {id: 1, origin: 1, isTicket: true};
                controller.showDescriptor($event, data);

                expect(controller.$.ticketDescriptor.show).toHaveBeenCalledWith($event.target, data.origin);
            });
        });
    });
});