/* eslint max-len: ["error", { "code": 150 }]*/
import './index';

describe('Route', () => {
    let controller;
    let $httpBackend;
    let $scope;

    beforeEach(ngModule('route'));

    beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        $scope = $rootScope.$new();
        const $element = angular.element('<vn-route-ticket-popup></vn-route-ticket-popup>');
        const $transclude = {
            $$boundTransclude: {
                $$slots: []
            }
        };
        controller = $componentController('vnRouteTicketPopup', {$element, $scope, $transclude});
        controller.route = {id: 1};
        controller.$.model = {
            refresh: () => {},
            remove: () => {}
        };
        controller.card = {reload: () => {}};
    }));

    describe('unlink()', () => {
        it('should call the route unlink endpoint with the agency and zone ids', () => {
            controller.$.model = {refresh: jest.fn()};
            jest.spyOn(controller.vnApp, 'showSuccess');
            jest.spyOn(controller, 'hide');

            controller.route = {
                agencyModeFk: 1
            };

            const ticket = {
                zoneFk: 2,
            };
            const params = {
                agencyModeId: controller.route.agencyModeFk,
                zoneId: ticket.zoneFk,
            };

            $httpBackend.expectPOST(`Routes/unlink`, params).respond('ok');
            controller.unlinkZone(ticket);
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.hide).toHaveBeenCalled();
            expect(controller.$.model.refresh).toHaveBeenCalledWith();
        });
    });

    describe('setTicketsRoute()', () => {
        it('should perform a POST query to add tickets to the route', () => {
            controller.$.model = {refresh: jest.fn()};
            jest.spyOn(controller.vnApp, 'showSuccess');
            jest.spyOn(controller, 'hide');

            controller.route = {id: 111};

            controller.possibleTickets = [
                {id: 2, checked: false},
                {id: 3, checked: true},
                {id: 4, checked: false},
                {id: 5, checked: true},
            ];

            $httpBackend.whenPOST(`Routes/${controller.route.id}/updateVolume`).respond(200);
            $httpBackend.expectPOST('Tickets/crud').respond();
            controller.setTicketsRoute();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.hide).toHaveBeenCalled();
            expect(controller.$.model.refresh).toHaveBeenCalledWith();
        });
    });
});