import './index.js';

describe('Travel Component vnTravelDescriptorMenu', () => {
    let controller;
    let $httpBackend;
    beforeEach(ngModule('travel'));

    beforeEach(inject(($componentController, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        const $element = angular.element('<vn-travel-descriptor-menu></vn-travel-descriptor-menu>');
        controller = $componentController('vnTravelDescriptorMenu', {$element});
        controller._travelId = 5;
    }));

    describe('onCloneAccept()', () => {
        it('should call state.go with the travel data', () => {
            jest.spyOn(controller.$state, 'go').mockReturnValue('ok');

            controller.travel = {
                ref: 'the ref',
                agencyModeFk: 'the agency',
                shipped: 'the shipped date',
                landed: 'the landing date',
                warehouseInFk: 'the receiver warehouse',
                warehouseOutFk: 'the sender warehouse'
            };

            controller.onCloneAccept();

            const params = JSON.stringify({
                ref: controller.travel.ref,
                agencyModeFk: controller.travel.agencyModeFk,
                shipped: controller.travel.shipped,
                landed: controller.travel.landed,
                warehouseInFk: controller.travel.warehouseInFk,
                warehouseOutFk: controller.travel.warehouseOutFk
            });

            expect(controller.$state.go).toHaveBeenCalledWith('travel.create', {'q': params});
        });
    });

    describe('onDeleteAccept()', () => {
        it('should perform a delete query', () => {
            jest.spyOn(controller.$state, 'go').mockReturnValue('ok');
            controller.travelId = 1;

            $httpBackend.when('GET', `Travels/${controller.travelId}`).respond(200);
            $httpBackend.when('GET', `Travels/${controller.travelId}/getEntries`).respond(200);
            $httpBackend.expect('DELETE', `Travels/${controller.travelId}`).respond(200);
            controller.onDeleteAccept();
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith('travel.index');
        });
    });

    describe('onCloneWithEntriesAccept()', () => {
        it('should make an HTTP query and then call to the $state.go method with the returned id', () => {
            jest.spyOn(controller.$state, 'go').mockReturnValue('ok');

            $httpBackend.expect('POST', `Travels/${controller.travelId}/cloneWithEntries`).respond(200, 9);
            controller.onCloneWithEntriesAccept();
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith('travel.card.basicData', {
                id: jasmine.any(Number)
            });
        });
    });
});