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

describe('claim', () => {
    describe('Component vnClaimAction', () => {
        let controller;
        let $httpBackend;
        let $state;

        beforeEach(ngModule('claim'));

        beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_) => {
            $httpBackend = _$httpBackend_;
            $state = _$state_;
            $state.params.id = 1;

            controller = $componentController('vnClaimAction', {$state});
            controller.claim = {ticketFk: 1};
            controller.$.model = {refresh: () => {}};
            controller.$.addSales = {
                hide: () => {},
                show: () => {}
            };
            controller.$.lastTicketsModel = crudModel;
            controller.$.lastTicketsPopover = {
                hide: () => {},
                show: () => {}
            };
            controller.card = {reload: () => {}};
        }));

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

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

        describe('getClaimedSales()', () => {
            it('should make a query and set salesToClaim', () => {
                controller.claim.id = 1;
                $httpBackend.expectGET(`/claim/api/ClaimBeginnings/1`).respond(200, 1);
                controller.getClaimedSales();
                $httpBackend.flush();

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

        describe('addClaimedSale(saleFk)', () => {
            it('should make a post and call refresh, hide and showSuccess', () => {
                spyOn(controller.$.model, 'refresh');
                spyOn(controller.$.addSales, 'hide');
                spyOn(controller.vnApp, 'showSuccess');
                $httpBackend.expectPOST(`claim/api/ClaimEnds/`).respond({});
                controller.addClaimedSale(1);
                $httpBackend.flush();

                expect(controller.$.model.refresh).toHaveBeenCalledWith();
                expect(controller.$.addSales.hide).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
            });
        });

        describe('deleteClaimedSale(id)', () => {
            it('should make a delete and call refresh and showSuccess', () => {
                spyOn(controller.$.model, 'refresh');
                spyOn(controller.vnApp, 'showSuccess');
                $httpBackend.expectDELETE(`claim/api/ClaimEnds/1`).respond({});
                controller.deleteClaimedSale(1);
                $httpBackend.flush();

                expect(controller.$.model.refresh).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
            });
        });

        describe('setClaimDestination(id, claimDestinationFk)', () => {
            it('should make a patch and call refresh and showSuccess', () => {
                spyOn(controller.vnApp, 'showSuccess');
                $httpBackend.expectPATCH(`claim/api/ClaimEnds/`).respond({});
                controller.setClaimDestination(1, 1);
                $httpBackend.flush();

                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
            });
        });

        describe('calculateTotals()', () => {
            it('should calculate the total price of the items claimed', () => {
                controller.salesClaimed = [
                    {sale: {quantity: 5, price: 2, discount: 0}},
                    {sale: {quantity: 10, price: 2, discount: 0}},
                    {sale: {quantity: 10, price: 2, discount: 0}}
                ];
                controller.calculateTotals();

                expect(controller.claimedTotal).toEqual(50);
            });
        });

        describe('importToNewRefundTicket()', () => {
            it('should perform a post query and add lines from a new ticket', () => {
                spyOn(controller.$.model, 'refresh');
                spyOn(controller.vnApp, 'showSuccess');
                $httpBackend.expect('POST', `claim/api/ClaimBeginnings/1/importToNewRefundTicket`).respond({});
                controller.importToNewRefundTicket();
                $httpBackend.flush();

                expect(controller.$.model.refresh).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
            });
        });

        describe('showLastTickets()', () => {
            it('should get a list of tickets and call lastTicketsPopover show() method', () => {
                spyOn(controller.$.lastTicketsModel, 'refresh');
                spyOn(controller.$.lastTicketsPopover, 'show');
                controller.showLastTickets({});

                expect(controller.$.lastTicketsModel.refresh).toHaveBeenCalledWith();
                expect(controller.$.lastTicketsPopover.show).toHaveBeenCalledWith();
            });
        });

        describe('importTicketLines()', () => {
            it('should perform a post query and add lines from an existent ticket', () => {
                spyOn(controller.$.model, 'refresh');
                spyOn(controller.vnApp, 'showSuccess');
                spyOn(controller.$.lastTicketsPopover, 'hide');
                let data = {claimFk: 1, ticketFk: 1};
                $httpBackend.expect('POST', `/claim/api/ClaimEnds/importTicketSales`, data).respond({});
                controller.importTicketLines(1);
                $httpBackend.flush();

                expect(controller.$.model.refresh).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
                expect(controller.$.lastTicketsPopover.hide).toHaveBeenCalledWith();
            });
        });

        describe('regularize()', () => {
            it('should perform a post query and reload the claim card', () => {
                spyOn(controller.card, 'reload');
                spyOn(controller.vnApp, 'showSuccess');

                let data = {claimFk: $state.params.id};
                $httpBackend.expect('POST', `/claim/api/Claims/regularizeClaim`, data).respond({});
                controller.regularize();
                $httpBackend.flush();

                expect(controller.card.reload).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
            });
        });
    });
});