import './index';

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

        beforeEach(ngModule('ticket'));

        beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$window_) => {
            $window = _$window_;
            $httpBackend = _$httpBackend_;
            $scope = $rootScope.$new();
            $scope.model = {
                refresh: () => {}
            };
            controller = $componentController('vnTicketExpedition', {$element: null, $scope});
            controller.$.model.data = [
                {id: 1},
                {id: 2},
                {id: 3}
            ];
            const modelData = controller.$.model.data;
            modelData[0].checked = true;
            modelData[1].checked = true;
        }));

        describe('onDialogAccept()', () => {
            it('should perform a DELETE query', () => {
                jest.spyOn($scope.model, 'refresh');

                const id = 1;

                $httpBackend.expectDELETE(`Expeditions/${id}`).respond(200);
                controller.onDialogAccept(id);
                $httpBackend.flush();

                expect($scope.model.refresh).toHaveBeenCalledWith();
            });
        });

        describe('showLog()', () => {
            it('should show the popover status log', () => {
                controller.$.statusLog = {show: () => {}};
                jest.spyOn(controller.$.statusLog, 'show');

                const expedition = {id: 1};

                const event = new MouseEvent('click', {
                    view: $window,
                    bubbles: true,
                    cancelable: true
                });
                controller.showLog(event, expedition);

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

        describe('onRemove()', () => {
            it('should make a query and then call to the model refresh() method', () => {
                jest.spyOn($scope.model, 'refresh');

                const expectedParams = {expeditionIds: [1, 2]};
                $httpBackend.expect('POST', 'Expeditions/deleteExpeditions', expectedParams).respond(200);
                controller.onRemove();
                $httpBackend.flush();

                expect($scope.model.refresh).toHaveBeenCalledWith();
            });
        });

        describe('createTicket()', () => {
            it('should make a query and then call to the $state go() method', () => {
                jest.spyOn(controller.$state, 'go').mockReturnThis();

                const landed = Date.vnNew();
                const ticket = {
                    clientFk: 1101,
                    landed: landed,
                    addressFk: 121,
                    agencyModeFk: 1,
                    warehouseFk: 1
                };
                const routeId = null;
                controller.ticket = ticket;

                const ticketToTransfer = {id: 28};

                const expectedParams = {
                    clientId: 1101,
                    landed: landed,
                    warehouseId: 1,
                    addressId: 121,
                    agencyModeId: 1,
                    routeId: null,
                    expeditionIds: [1, 2]
                };
                $httpBackend.expect('POST', 'Expeditions/moveExpeditions', expectedParams).respond(ticketToTransfer);
                controller.createTicket(ticket.landed, routeId);
                $httpBackend.flush();

                expect(controller.$state.go).toHaveBeenCalledWith('ticket.card.summary', {id: ticketToTransfer.id});
            });
        });
    });
});