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

describe('claim', () => {
    describe('Component vnClaimDetail', () => {
        let $scope;
        let controller;
        let $httpBackend;

        beforeEach(ngModule('claim'));

        beforeEach(inject(($componentController, _$httpBackend_, $rootScope) => {
            $scope = $rootScope.$new();
            $scope.descriptor = {
                show: () => {}
            };
            $httpBackend = _$httpBackend_;
            $httpBackend.whenGET('Claims/ClaimBeginnings').respond({});
            $httpBackend.whenGET(`Tickets/1/isEditable`).respond(true);
            $httpBackend.whenGET(`Claims/2/isEditable`).respond(true);
            const $element = angular.element('<vn-claim-detail></vn-claim-detail>');
            controller = $componentController('vnClaimDetail', {$element, $scope});
            controller.claim = {
                ticketFk: 1,
                id: 2}
            ;
            controller.salesToClaim = [{saleFk: 1}, {saleFk: 2}];
            controller.salesClaimed = [{id: 1, sale: {}}];
            controller.$.model = crudModel;
            controller.$.addSales = {
                hide: () => {},
                show: () => {}
            };
            controller.$.editPopover = {
                hide: () => {}
            };
            jest.spyOn(controller.aclService, 'hasAny').mockReturnValue(true);
        }));

        describe('openAddSalesDialog()', () => {
            it('should call getClaimableFromTicket and $.addSales.show', () => {
                jest.spyOn(controller, 'getClaimableFromTicket');
                jest.spyOn(controller.$.addSales, 'show');
                controller.openAddSalesDialog();

                expect(controller.getClaimableFromTicket).toHaveBeenCalledWith();
                expect(controller.$.addSales.show).toHaveBeenCalledWith();
            });
        });

        describe('getClaimableFromTicket()', () => {
            it('should make a query and set salesToClaim', () => {
                $httpBackend.expectGET(`Sales/getClaimableFromTicket?ticketFk=1`).respond(200, 1);
                controller.getClaimableFromTicket();
                $httpBackend.flush();

                expect(controller.salesToClaim).toEqual(1);
            });
        });

        describe('addClaimedSale(index)', () => {
            it('should make a post and call refresh, hide and showSuccess', () => {
                jest.spyOn(controller.$.addSales, 'hide');
                jest.spyOn(controller.$state, 'go');
                $httpBackend.expectPOST(`ClaimBeginnings/`).respond({});
                controller.addClaimedSale(1);
                $httpBackend.flush();

                expect(controller.$.addSales.hide).toHaveBeenCalledWith();
                expect(controller.$state.go).toHaveBeenCalledWith('claim.card.development');
            });
        });

        describe('deleteClaimedSale()', () => {
            it('should make a delete and call refresh and showSuccess', () => {
                const claimedIndex = 1;
                controller.claimedIndex = claimedIndex;
                jest.spyOn(controller.$.model, 'remove');
                jest.spyOn(controller.$.model, 'save');
                jest.spyOn(controller.vnApp, 'showSuccess');

                controller.deleteClaimedSale();

                expect(controller.$.model.remove).toHaveBeenCalledWith(claimedIndex);
                expect(controller.$.model.save).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            });
        });

        describe('setClaimedQuantity(id, claimedQuantity)', () => {
            it('should make a patch and call refresh and showSuccess', () => {
                jest.spyOn(controller.vnApp, 'showSuccess');
                $httpBackend.expectPATCH(`ClaimBeginnings/`).respond({});
                controller.setClaimedQuantity(1, 1);
                $httpBackend.flush();

                expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            });
        });

        describe('calculateTotals()', () => {
            it('should set paidTotal and claimedTotal to 0 if salesClaimed has no data', () => {
                controller.salesClaimed = [];
                controller.calculateTotals();

                expect(controller.paidTotal).toEqual(0);
                expect(controller.claimedTotal).toEqual(0);
            });
        });

        describe('updateDiscount()', () => {
            it('should perform a query if the new discount differs from the claim discount', () => {
                controller.saleClaimed = {sale: {
                    discount: 5,
                    id: 7,
                    ticketFk: 1,
                    price: 2,
                    quantity: 10}};
                controller.newDiscount = 10;

                jest.spyOn(controller.vnApp, 'showSuccess');
                jest.spyOn(controller, 'calculateTotals');
                jest.spyOn(controller, 'clearDiscount');
                jest.spyOn(controller.$.editPopover, 'hide');

                $httpBackend.when('POST', 'Tickets/1/updateDiscount').respond({});
                controller.updateDiscount();
                $httpBackend.flush();

                expect(controller.calculateTotals).toHaveBeenCalledWith();
                expect(controller.clearDiscount).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalled();
                expect(controller.$.editPopover.hide).toHaveBeenCalledWith();
            });
        });

        describe('isTicketEditable()', () => {
            it('should check if the ticket assigned to the claim is editable', () => {
                controller.isTicketEditable();
                $httpBackend.flush();

                expect(controller.isEditable).toBeTruthy();
            });
        });
    });
});