import './index.js';

describe('Ticket', () => {
    describe('Component vnTicketSale', () => {
        let controller;
        let $element;
        let $scope;
        let $httpBackend;

        let ticket = {
            id: 1,
            clientFk: 1,
            shipped: 1,
            client: {salesPersonFk: 1}
        };
        let sales = [
            {
                id: 1,
                quantity: 5,
                price: 23.5,
                discount: 0
            }, {
                id: 4,
                quantity: 20,
                price: 5.5,
                discount: 0
            }
        ];

        beforeEach(() => {
            ngModule('item');
            ngModule('ticket');
        });

        beforeEach(angular.mock.inject(($compile, $rootScope, $state, _$httpBackend_) => {
            $state.params.id = ticket.id;

            $scope = $rootScope.$new();
            $scope.ticket = ticket;

            $httpBackend = _$httpBackend_;
            $httpBackend.whenGET(/api\/Tickets\/1\/getSales.*/).respond(sales);
            $httpBackend.whenGET(`/ticket/api/Tickets/1/getVAT`).respond(200, 10.5);

            $element = $compile('<vn-ticket-sale ticket="ticket"></vn-ticket-sale>')($scope);
            controller = $element.controller('vnTicketSale');
            controller.card = {reload: () => {}};

            $httpBackend.flush();
        }));

        afterEach(() => {
            $scope.$destroy();
            $element.remove();
        });

        describe('createClaim()', () => {
            it('should perform a query and call windows open', () => {
                spyOn(controller.$state, 'go');

                let res = {id: 1};
                $httpBackend.expectPOST(`claim/api/Claims/createFromSales`).respond(res);
                controller.createClaim();
                $httpBackend.flush();

                expect(controller.$state.go).toHaveBeenCalledWith('claim.card.basicData', {id: 1});
            });
        });

        describe('total/VAT/subTotal properties', () => {
            it('should fill total, VAT and subTotal', () => {
                expect(controller.subTotal).toEqual(227.5);
                expect(controller.VAT).toEqual(10.5);
                expect(controller.total).toEqual(238);
            });
        });

        describe('isChecked() getter', () => {
            it('should set isChecked value to true when one of the instances has the value checked to true', () => {
                controller.sales[0].checked = true;

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

        describe('getCheckedLines()', () => {
            it('should make an array of the instances with the property checked true()', () => {
                let sale = controller.sales[1];
                sale.checked = true;
                let expectedResult = [{id: sale.id, instance: 1}];

                expect(controller.getCheckedLines()).toEqual(expectedResult);
            });
        });

        describe('onStateOkClick()', () => {
            it('should perform a get and then call a function', () => {
                let filter = {where: {code: 'OK'}, fields: ['id']};
                filter = encodeURIComponent(JSON.stringify(filter));
                let res = [{id: 3}];
                spyOn(controller, 'onStateChange');

                $httpBackend.whenGET(`/ticket/api/States?filter=${filter}`).respond(res);
                controller.onStateOkClick();
                $httpBackend.flush();

                expect(controller.onStateChange).toHaveBeenCalledWith(3);
            });
        });

        describe('onStateChange()', () => {
            it('should perform a post and then call a function', () => {
                $httpBackend.expectPOST(`/ticket/api/TicketTrackings/changeState`).respond();
                controller.onStateChange(3);
                $httpBackend.flush();
            });
        });

        describe('onRemoveLinesClick()', () => {
            it('should call getCheckedLines, call removeInstances, and make a query', () => {
                controller.sales[1].checked = true;

                $httpBackend.whenPOST(`/ticket/api/Sales/removes`).respond();
                controller.onRemoveLinesClick('ACCEPT');
                $httpBackend.flush();

                expect(controller.sales.length).toEqual(1);
            });
        });

        describe('unmarkAsReserved()', () => {
            it('should call setReserved with false', () => {
                spyOn(controller, 'setReserved');

                controller.unmarkAsReserved(false);

                expect(controller.setReserved).toHaveBeenCalledWith(false);
            });
        });

        describe('markAsReserved()', () => {
            it('should call setReserved with true', () => {
                spyOn(controller, 'setReserved');

                controller.markAsReserved(true);

                expect(controller.setReserved).toHaveBeenCalledWith(true);
            });
        });

        describe('setReserved()', () => {
            it('should call getCheckedLines, $.index.accept and make a query ', () => {
                controller.sales[1].checked = true;
                let expectedRequest = {
                    sales: [{
                        id: sales[1].id,
                        instance: 1
                    }],
                    ticketFk: ticket.id,
                    reserved: false
                };

                $httpBackend.expectPOST(`/ticket/api/Sales/reserve`, expectedRequest).respond();
                controller.unmarkAsReserved(false);
                $httpBackend.flush();
            });
        });
    });
});