import './index.js';

describe('Item', () => {
    describe('Component vnItemDescriptorPopover', () => {
        let $httpBackend;
        let $scope;
        let controller;
        let $element;
        let $timeout;

        beforeEach(ngModule('item'));

        beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$timeout_) => {
            $httpBackend = _$httpBackend_;
            $timeout = _$timeout_;
            $element = angular.element(`<div></div>`);
            $scope = $rootScope.$new();
            $scope.popover = {relocate: () => {}, show: () => {}};
            controller = $componentController('vnItemDescriptorPopover', {$scope, $element});
        }));

        describe('itemFk()', () => {
            it(`should not apply any changes if the received id is the same stored in _itemFk`, () => {
                controller.item = 'I exist!';
                controller._itemFk = 1;
                spyOn(controller, 'getCard');
                controller.itemFk = 1;

                expect(controller.item).toEqual('I exist!');
                expect(controller._itemFk).toEqual(1);
                expect(controller.getCard).not.toHaveBeenCalled();
            });

            it(`should set the received id into _itemFk, set the item to null and then call getCard()`, () => {
                controller.item = `Please don't`;
                controller._itemFk = 1;
                spyOn(controller, 'getCard');
                controller.itemFk = 999;

                expect(controller.item).toBeNull();
                expect(controller._itemFk).toEqual(999);
                expect(controller.getCard).toHaveBeenCalledWith();
            });
        });

        describe('item()', () => {
            it(`should save the item into _item and then call relocate()`, () => {
                spyOn(controller.$.popover, 'relocate');
                controller.item = `i'm the item!`;
                $timeout.flush();

                expect(controller._item).toEqual(`i'm the item!`);
                expect(controller.$.popover.relocate).toHaveBeenCalledWith();
            });
        });

        describe('show()', () => {
            it(`should call the show()`, () => {
                spyOn(controller.$.popover, 'show');
                controller.show();

                expect(controller.$.popover.show).toHaveBeenCalledWith();
            });
        });

        describe('getCard()', () => {
            it(`should perform a get query to store the item data into the controller`, () => {
                controller.itemFk = 1;
                controller.canceler = null;
                let response = {};
                $httpBackend.when('GET', `/item/api/Items/${controller._itemFk}/getCard`).respond(response);
                $httpBackend.expect('GET', `/item/api/Items/${controller._itemFk}/getCard`);
                controller.getCard();
                $httpBackend.flush();

                expect(controller.item).toEqual(response);
            });
        });
    });
});